stl and dll, howto? 
Author Message
 stl and dll, howto?

Basically here is my problem, I am returning a std::string which can be
quite large from a dll to the exe. It works fine, until the
std::string's dtor is called.

In the following code, two strings are created in the dll (libbig.cpp)
and returned to the exe (big.cpp). The good string destroys fine and
behaves as expected. The bad string's dtor calls the "user breakpoint at
0xFOOBAR".

So my question is, huh?  And how do I fix this. Is this a known win32
issue buried somewhere in the knowledge base?

Thnx,
Justin

// libbig.h
#ifdef LIBBIG_EXPORTS
#define LIBBIG_API __declspec(dllexport)
#else
#define LIBBIG_API __declspec(dllimport)
#endif
#include <string>
class LIBBIG_API Big
{
public:
        Big(){}
        std::string str(int sz, char c)const;          

Quote:
};

//libbig.cpp
std::string Big::str(int sz, char c)const
{
        std::string rv;
        for(int i=0;i<sz;++i){rv+=c;}
        return rv;

Quote:
}

//big.cpp
#include "../libbig/libbig.h"

int main(int argc, char* argv[])
{
        libbig::Big lbb;
        {
                std::string good = lbb.str(127,'A');
        }
        {
                std::string bad = lbb.str(128,'A');
        }
        return 0;

Quote:
}



Thu, 15 Dec 2005 10:14:09 GMT  
 stl and dll, howto?
Make sure both the DLL and the EXE are linked to the DLL runtime library
(msvcrt.dll or similar).  If the EXE & DLL aren't both linked to the exact
same (debug/non-debug, compiler version, everything must match) runtime DLL,
you'll run into problems trying to pass std::string (and indeed any class
that holds memory allocated from the heap) between them.

If for some reason the two modules cannot use the same runtime library, then
you'll have to redesign so as to avoid passing strings across the DLL/EXE
boundary.

-cd

Quote:

> Basically here is my problem, I am returning a std::string which can
> be quite large from a dll to the exe. It works fine, until the
> std::string's dtor is called.

> In the following code, two strings are created in the dll (libbig.cpp)
> and returned to the exe (big.cpp). The good string destroys fine and
> behaves as expected. The bad string's dtor calls the "user breakpoint
> at 0xFOOBAR".

> So my question is, huh?  And how do I fix this. Is this a known win32
> issue buried somewhere in the knowledge base?

> Thnx,
> Justin

> // libbig.h
> #ifdef LIBBIG_EXPORTS
> #define LIBBIG_API __declspec(dllexport)
> #else
> #define LIBBIG_API __declspec(dllimport)
> #endif
> #include <string>
> class LIBBIG_API Big
> {
> public:
> Big(){}
> std::string str(int sz, char c)const;
> };

> //libbig.cpp
> std::string Big::str(int sz, char c)const
> {
> std::string rv;
> for(int i=0;i<sz;++i){rv+=c;}
> return rv;
> }

> //big.cpp
> #include "../libbig/libbig.h"

> int main(int argc, char* argv[])
> {
> libbig::Big lbb;
> {
> std::string good = lbb.str(127,'A');
> }
> {
> std::string bad = lbb.str(128,'A');
> }
> return 0;
> }



Thu, 15 Dec 2005 11:56:49 GMT  
 stl and dll, howto?
That was it. thanks .. I was hoping it was some oversight on my part.
Had to link to the "Debug Multithreaded DLL"

Justin


Quote:
> Make sure both the DLL and the EXE are linked to the DLL runtime library
> (msvcrt.dll or similar).  If the EXE & DLL aren't both linked to the exact
> same (debug/non-debug, compiler version, everything must match) runtime DLL,
> you'll run into problems trying to pass std::string (and indeed any class
> that holds memory allocated from the heap) between them.

> If for some reason the two modules cannot use the same runtime library, then
> you'll have to redesign so as to avoid passing strings across the DLL/EXE
> boundary.

> -cd


>>Basically here is my problem, I am returning a std::string which can
>>be quite large from a dll to the exe. It works fine, until the
>>std::string's dtor is called.

>>In the following code, two strings are created in the dll (libbig.cpp)
>>and returned to the exe (big.cpp). The good string destroys fine and
>>behaves as expected. The bad string's dtor calls the "user breakpoint
>>at 0xFOOBAR".

>>So my question is, huh?  And how do I fix this. Is this a known win32
>>issue buried somewhere in the knowledge base?

>>Thnx,
>>Justin

>>// libbig.h
>>#ifdef LIBBIG_EXPORTS
>>#define LIBBIG_API __declspec(dllexport)
>>#else
>>#define LIBBIG_API __declspec(dllimport)
>>#endif
>>#include <string>
>>class LIBBIG_API Big
>>{
>>public:
>>Big(){}
>>std::string str(int sz, char c)const;
>>};

>>//libbig.cpp
>>std::string Big::str(int sz, char c)const
>>{
>>std::string rv;
>>for(int i=0;i<sz;++i){rv+=c;}
>>return rv;
>>}

>>//big.cpp
>>#include "../libbig/libbig.h"

>>int main(int argc, char* argv[])
>>{
>>libbig::Big lbb;
>>{
>>std::string good = lbb.str(127,'A');
>>}
>>{
>>std::string bad = lbb.str(128,'A');
>>}
>>return 0;
>>}



Thu, 15 Dec 2005 19:06:05 GMT  
 stl and dll, howto?

Quote:

> If for some reason the two modules cannot use the same runtime library, then
> you'll have to redesign so as to avoid passing strings across the DLL/EXE
> boundary.

Or instead of the default string type, use one with an explicit
allocator, and write the allocator to use a single heap.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)



Thu, 15 Dec 2005 23:33:12 GMT  
 stl and dll, howto?
I know one reason.The vc debug version have it's debug heap tec.The memory
alloc from the dll can not be right free out of the dll module.Bug the
release version dose not have this problem.


Quote:
> Basically here is my problem, I am returning a std::string which can be
> quite large from a dll to the exe. It works fine, until the
> std::string's dtor is called.

> In the following code, two strings are created in the dll (libbig.cpp)
> and returned to the exe (big.cpp). The good string destroys fine and
> behaves as expected. The bad string's dtor calls the "user breakpoint at
> 0xFOOBAR".

> So my question is, huh?  And how do I fix this. Is this a known win32
> issue buried somewhere in the knowledge base?

> Thnx,
> Justin

> // libbig.h
> #ifdef LIBBIG_EXPORTS
> #define LIBBIG_API __declspec(dllexport)
> #else
> #define LIBBIG_API __declspec(dllimport)
> #endif
> #include <string>
> class LIBBIG_API Big
> {
> public:
> Big(){}
> std::string str(int sz, char c)const;
> };

> //libbig.cpp
> std::string Big::str(int sz, char c)const
> {
> std::string rv;
> for(int i=0;i<sz;++i){rv+=c;}
> return rv;
> }

> //big.cpp
> #include "../libbig/libbig.h"

> int main(int argc, char* argv[])
> {
> libbig::Big lbb;
> {
> std::string good = lbb.str(127,'A');
> }
> {
> std::string bad = lbb.str(128,'A');
> }
> return 0;
> }



Thu, 22 Dec 2005 21:31:42 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. HOWTO? Calling VB DLL from C++ DLL

2. STL: Howto know the byte count of write() (like pcount())

3. STL - MSVCP60.DLL and MSCVRT.DLL

4. howto static link atl dll in vc7

5. Howto make a dll in VC++ 6 to be used in VB 6 and VB .Net

6. Howto Import com dll referencing a tlb?

7. Howto make a dll in VC++ 6 to be used in VB 6 and VB .Net

8. HOWTO : Pass UDT between VC++ COM DLL and VC++ exe

9. Howto debug a DLL that kicks off from an EXE

10. vc 5 debugging a dll (howto)

11. HOWTO: Pass VB UDT that contains an array to VC dll

12. HOWTO catch exception from 16-bit DLL

 

 
Powered by phpBB® Forum Software