Re: Structure containing string across DLL boundary
- From: Daniel Devine <ddevine@xxxxxxxxxx>
- Date: Wed, 01 Nov 2006 19:20:40 -0500
Thank you for responding.
A few things.
Exe and DLL both built with static RTL. Both are run in either debug or
release build. Debug build has no issues.
I think I am maintaining the discipline of which you speak.
The DLL allocates the structure with the embedded string. The string is set
to the notification message 'I am doing some work'
The function in the exe is invoked with a pointer to the structure and then
cout << structurePtr->string << endl ;
That is it. Call returns to DLL where structure is deallocated.
Are there memory allocations/deallocations occurring in the console output
call?
The articles on the Web I refer were response Pete Becker of dinkumware gave
on this issue relating to static variables in some STL structures that
caused problems across the DLL/exe boundary but had been fixed in newer
releases of the compiler.
Thanks
Dan Devine
On 11/1/06 6:51 PM, in article ea7vkCh$GHA.2192@xxxxxxxxxxxxxxxxxxxx,
"Stephen Howe" <stephenPOINThoweATtns-globalPOINTcom> wrote:
Structure containing string across DLL boundaryI have seen similar questions
on this forum but not quite the issue I am having.
I have an executable which has a small function that writes to cout the
contents of a structure pointer that is passed in from a callback function
residing in a DLL. Basically, the exe launches the DLL to do processing and
the DLL calls back to the exe to notify it of its progress. It passes in a
pointer to a structure which contains a std:string. Interestingly, the debug
version works well. The Release version outputs garbage to the console and
then the expected exception occurs. Both the exe and DLL are developed with
VS2005 C++.
a problem but has been fixed.I have seen articles from various sources that state that this used to be
Where have you seen those articles? Please give a URL.
This is a problem, there are solutions and within those solution spaces they
are "fixed".
But outside those solution spaces, there is no fix.
If your sources are regurgitating one of the solutions, that is nothing new.
buffer and all is well. I would like to know >> why I can't do what I wouldThe simple solution we have is to replace the std:string with a char
like to do with the string class. Can anyone help or point me to a relevant
article >> on the subject.
Very easy. If your EXE and DLL are built with the standalone C and C++
libraries, then each of them will have
_THEIR OWN COPY_ of the C and or C++ library within. And that means each
copy maintains its own state.
And that in turn means
You cant fopen() a file in an EXE, pass the pointer and then call
fread()/fwrite()/fclose() in the DLL
You cant malloc()/new memory in the EXE, pass the pointer and then call
free()/delete in the DLL
You cant start using strtok in an EXE, and continue using strtok in the DLL
You cant start using std::string in an EXE, and resize the std::string in
the DLL
You seeing a pattern here?
There are two solutions
(i) You build all EXEs/DLLs usign the RTL in DLL form, then it wont matter
as there will only be 1 place that the C/C++ library resides, so all the
above things will work.
(ii) You make it so that any resource allocation, change and deallocation
occurs in 1 module _ONLY_.
In the case above, you should get slapped if you attempted to chang the
string inside the DLL.
It is good discipline. Then you can use the static libraries.
Stephen Howe
.
- Follow-Ups:
- Re: Structure containing string across DLL boundary
- From: Stephen Howe
- Re: Structure containing string across DLL boundary
- From: David Lowndes
- Re: Structure containing string across DLL boundary
- References:
- Re: Structure containing string across DLL boundary
- From: Stephen Howe
- Re: Structure containing string across DLL boundary
- Prev by Date: Re: Structure containing string across DLL boundary
- Next by Date: Re: Structure containing string across DLL boundary
- Previous by thread: Re: Structure containing string across DLL boundary
- Next by thread: Re: Structure containing string across DLL boundary
- Index(es):
Relevant Pages
|