Heap corruption probelm when i try to return std::string from function in dll


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Heap corruption probelm when i try to return std::string from function in dll

  1. #1
    Join Date
    Feb 2009
    Posts
    3

    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;
    
    	}

  2. #2
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    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
    -------------------------------

  3. #3
    Join Date
    Feb 2009
    Posts
    3
    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!!!!!

  4. #4
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    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 09:31 AM.
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

  5. #5
    Join Date
    Dec 2007
    Posts
    401
    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

  1. Switch Statement..return problem
    By XanFox in forum Java
    Replies: 3
    Last Post: 04-14-2011, 10:03 PM
  2. string array in c++
    By emeric in forum C++
    Replies: 11
    Last Post: 07-16-2007, 10:36 PM
  3. Replies: 15
    Last Post: 03-21-2006, 07:53 AM
  4. Replies: 0
    Last Post: 05-17-2002, 05:17 AM
  5. Replies: 1
    Last Post: 02-28-2002, 01: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center