-
Heap corruption probelm when i try to return std::string from function in dll
Hi all,
I am working on vs2005 (vc++8).
I have created one dll in vs2005(vc++8), in that we have one function which returns "std::string" .
when i load this dll in another vs2005(vc++8) application and call the function which returns "std::string",
we get following error:
Windows has triggered a breakpoint in vstestdll.exe.
This may be due to a corruption of the heap, and indicates a bug in vstestdll.exe or any of the DLLs it has loaded.
The output window may have more diagnostic information
For more detail code is as follows:
Code:
dll function :
std::string getDH1(int clo)
{
return std::string("abcdefghijklmnopqr");
}
.exe code:
#define TEST2_API __declspec(dllimport);
TEST2_API std::string getDH1(int clo);
int _tmain(int argc, _TCHAR* argv[])
{
string sam =getDH1(1);
return 0;
}
-
That's a very annoying problem with DLLs.
you basically can not do that.
What you need to do instead is pass a string by reference to the DLL as an output parameter.
The problem is that a DLL manages it's own memory and you get access violations when accessing memory that was allocated inside the DLL from anywhere outside the DLL.
DKyb-------------------------------
Life is a short warm moment -
Death is the long cold rest.
Pink Floyd
-------------------------------
-
We use the OOCI (oracle for call Interface) . In occi One function return the STL String and we have a heap corruption problem while getting STL::string data through OCCI methods.
And then we try to simulate the same problem by writing the same code in our dll and getting the std::string.
Same problem is simulated.
So plz i need to know how come this is happening and how to overcome this problem.
Thanks in advance!!!!!
-
Code:
// DLL Function
TEST2_API bool myFunction(std::string& myOutputString)
{
bool reval = false;
{
// do the (complex) assignment of your string here and set the return value to true
// if successful
myOutputString = "the string .... ";
if(successfullyAssigned)
{
reval = true;
}
}
return reval;
}
/// in your main function:
std::string myString="";
if(myFunction(myString))
{
// use myString
}
this approach will work, coz the string object is managed by the exe rather than the DLL
Last edited by drkybelk; 02-05-2009 at 08:31 AM.
DKyb-------------------------------
Life is a short warm moment -
Death is the long cold rest.
Pink Floyd
-------------------------------
-
allocations made by one module (with its own statically-linked CRT) are being freed by a different module (which has a different copy of the CRT).
the problem can be overcome by building *all* the modules to use the dynamically linked (dll version) of the CRT.
see: http://msdn.microsoft.com/en-us/libr...60(VS.80).aspx
http://msdn.microsoft.com/en-us/libr...yh(VS.80).aspx
even then, one has to be pretty defensive about passing STL containers across module boundaries.
ideally, do not do it at all. (use drybelk's suggestion instead).
if you have to do it (because of an externally imposed constraint), make sure that the same version of the compiler (and standard C++ library) are used to build every module.
in either case, use the same compiler switches and preprocessor definitions everywhere.
even a small difference (in compiler switches or preprocessor definitions) may result in making the memory layouts of objects incompatible.
Similar Threads
-
Replies: 3
Last Post: 04-14-2011, 09:03 PM
-
Replies: 11
Last Post: 07-16-2007, 09:36 PM
-
Replies: 15
Last Post: 03-21-2006, 06:53 AM
-
By Laurence in forum Database
Replies: 0
Last Post: 05-17-2002, 04:17 AM
-
Replies: 1
Last Post: 02-28-2002, 12:54 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks