Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using string as return value from a DLL causes a crash

Status
Not open for further replies.

josyj

Programmer
Apr 4, 2001
1
US
Hello folks,
I have a C++ Dll , which returns a string . The test application crashes after it displays the string. I have the same problems if I use vector<string> or even if I try to pass the string as reference, although a return type of char* or vector<string>* works fine. Since the dll is to be used with a software that doesn't support pointers, I need a solution that doesn't involve pointers ( and no fixed sized arrays ).

Here is how the code looks like -


#include <string>
#include <vector>
using namespace std ;

and somewhere in the dll code, I have something similar to this -
string xxx( )
{
string str(&quot;TestStr&quot;) ;
return str ;
}

The test application does the following -
string tt = xxx() ;
cout << tt.c_str() ;

The test application crashes after this and the following dialog box comes -
Debug assertion failed , file dbgheap.c
Expression: _CrtIsValidHeapPointer(pUserData)

I guess this has something to do with both the dll and the test application trying to free memory. Please remember everything works fine if I use pointers.
Is it something to do with the way VC++ implements the STL ?
I would appreciate if anyone could point out what is the problem.
Thanks,
- JJ
 
I had the same problem. As I believe, there are problems with destructors wich are executing when you not expect. John Fill
 
I am not a C++ programmer, but I was facing this problem in Delphi and I used the Sharemem library and it worked.
I think that the problem is that the memory location of the dll that is loaded into the application memory space is not visible to the application. The problem is possible because of a null pointer

Another problem that I faced while exporting is that the the method that is declared in the test app which mirrors should have the same specifier(cdecl, pascal, stdcall, register, safecall....) as in the dll, otherwise it gives a an access violation...
Hope this helps
 
Make sure that all dlls and exe are compiled in the same way:
All files should be build in the same way: all in debug mode or all in release mode.
You call the constructor of a string in a dll and you call the destructor in another dll or in an exe.
string allocates memory in constructor and deallocates in destructor using operator new and delete on char*.
The implementation of new and delete operator may be different in release mode and in debug mode: in release mode always use malloc and release, in debug mode may use _malloc_dbg and _free_dbg.
This functions are non compatible.
 
I have an idea.
Try to see if the function executes fine in the same program, not in a DLL. Maybe it'll help. John Fill
ivfmd@mail.md
 
Just out of curiosity... what is the program you are using that does not support pointers? If it is visual basic I may be of assistance. Look into BSTR if it is.

Cheers,
Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top