Std::set and passing between DLLs 
Author Message
 Std::set and passing between DLLs

I have a class 'AA' I have exported from DLL 'A' that containes a class
data member of type "std::set<SomeCommonType>" . A function of this
class returns a pointer to this data member. I can iterate successfully
through this set within this DLL at any time. Another DLL 'B', which
imports class 'AA', instantiates an object of class 'AA' and calls the
function that gets a pointer to this std::set data member. However, from
within DLL 'B' iterating successfully through this std::set using the
pointer always fails. Here is an illustration:

------------------------------------------------------------------------------------------------------------------------------------------

In DLL 'A'

#include <set>

class EXPORTIMPORTMACRO AA
{
public:
std::set<SomeCommonType> * GetMySet() {return(&myset);}
private:
std::set<SomeCommonType> myset;

Quote:
};

In DLL 'B'

#include "AAHeaderfile.h"

AA aaobject;
std::set<SomeCommonType> * pset = aaobject.GetMySet();
std::set<SomeCommonType>::iterator it;
for (it = pset -> begin(); it != pset -> end(); ++it) // Always fails
during ++it
  {
  // Do anything
  }

-----------------------------------------------------------------------------------------------------------------------------------------

After debugging this situation I have discovered that the reason that
this fails is because std::set<> contains class static data members
which are used in the iteration process. Evidently both DLL 'A' and DLL
'B' have their own versions of these static data members, and even when
I am iterating through the std::set in DLL 'B' using the pointer passed
by DLL 'A' it is DLL 'B' s version of these static data members which
are used and caused the failure.

Does anyone know of a solution to this problem ? Does it involve somehow
exporting the "std::set<SomeCommonType>" instantiation from DLL 'A' and
importing it into DLL 'B' separately from the exporting and importing of
'AA' ? Is using class static data members for std::set a weakness in its
design ? Any help on this would be greatly appreciated.



Sun, 07 Oct 2001 03:00:00 GMT  
 Std::set and passing between DLLs


Quote:
> I have a class 'AA' I have exported from DLL 'A' that containes a class
> data member of type "std::set<SomeCommonType>" . A function of this
> class returns a pointer to this data member. I can iterate successfully
> through this set within this DLL at any time. Another DLL 'B', which
> imports class 'AA', instantiates an object of class 'AA' and calls the
> function that gets a pointer to this std::set data member. However, from
> within DLL 'B' iterating successfully through this std::set using the
> pointer always fails. Here is an illustration:

What run library are you linking to? If you use STL in a DLL make sure that
you use the C run time lib as a DLL and not as a static lib (which is gene-
rally the default). STL screws up badly if you use it in DLL's and yet link
with static CRT. You will find this option in Project/Settings/'C/C++'/-
Category-"Code Geneartion"/Use run time library. Just change it to multithr-
eaded DLL to be very safe.
You see now the memory allocated will be from another heap which DLL's can
access. If you use static libs then the heap of that is not accessible to
code in DLL's. Wierd but thats how things seem to be. Also read another
thread in this newsgroup some time ago titled "using STL in a DLL".
HTH
Shiv


Mon, 08 Oct 2001 03:00:00 GMT  
 Std::set and passing between DLLs
Does enyone know off a newsserver where I can
pick up that thread.
Or does enyone have a copy to spear ?

I am very interested in everything that is writen on this issue.

Quote:

>Also read another
>thread in this newsgroup some time ago titled "using STL in a DLL".



Mon, 08 Oct 2001 03:00:00 GMT  
 Std::set and passing between DLLs

Quote:



>> I have a class 'AA' I have exported from DLL 'A' that containes a class
>> data member of type "std::set<SomeCommonType>" . A function of this
>> class returns a pointer to this data member. I can iterate successfully
>> through this set within this DLL at any time. Another DLL 'B', which
>> imports class 'AA', instantiates an object of class 'AA' and calls the
>> function that gets a pointer to this std::set data member. However, from
>> within DLL 'B' iterating successfully through this std::set using the
>> pointer always fails. Here is an illustration:

>What run library are you linking to? If you use STL in a DLL make sure that
>you use the C run time lib as a DLL and not as a static lib (which is gene-
>rally the default). STL screws up badly if you use it in DLL's and yet link
>with static CRT. You will find this option in Project/Settings/'C/C++'/-
>Category-"Code Geneartion"/Use run time library. Just change it to
multithr-
>eaded DLL to be very safe.

It is already set for the multithreaded DLL in all cases, but that is not
the problem. The problem is the class static member variables in the tree
class used by std::set. I also saw the discussion of using the STL in DLLs
but that did not discuss my problem. Thanks anyway.


Mon, 08 Oct 2001 03:00:00 GMT  
 Std::set and passing between DLLs

Quote:




>>> I have a class 'AA' I have exported from DLL 'A' that containes a class
>>> data member of type "std::set<SomeCommonType>" . A function of this
>>> class returns a pointer to this data member. I can iterate successfully
>>> through this set within this DLL at any time. Another DLL 'B', which
>>> imports class 'AA', instantiates an object of class 'AA' and calls the
>>> function that gets a pointer to this std::set data member. However, from
>>> within DLL 'B' iterating successfully through this std::set using the
>>> pointer always fails. Here is an illustration:

>>What run library are you linking to? If you use STL in a DLL make sure
that
>>you use the C run time lib as a DLL and not as a static lib (which is
gene-
>>rally the default). STL screws up badly if you use it in DLL's and yet
link
>>with static CRT. You will find this option in Project/Settings/'C/C++'/-
>>Category-"Code Geneartion"/Use run time library. Just change it to
>multithr-
>>eaded DLL to be very safe.

>It is already set for the multithreaded DLL in all cases, but that is not
>the problem. The problem is the class static member variables in the tree
>class used by std::set. I also saw the discussion of using the STL in DLLs
>but that did not discuss my problem. Thanks anyway.

I had the same problem in exporting instances of STL classes in a DLL.
It was in std::map<int,int>. The origin of the problem is the same, and
it is in the base template class _Tree defined in header "xtree".
Refer to MSDN [Q172396] and MSDN [Q168958].
Two solutions are given :
- the first one is only a workaround :
  you should not export STL classes, but just use them in your DLL,
  and export functions or classes which uses only simple types.
  You could declare STL members of your class as private,
  to be safer. This way is a bit limiting for STL, but if you also
  use COM interface, it is obvious and necessary.
- the second one is to export your STL-derived class,
  and ALL the classes used by your class (at least the ones
  which have static members). But I did not succeeded in exporting
  any instance of _Tree template class (I do not know why).

Another solution could be to remove static members from Xtree.
These are _Nil and _Nilrefs, and are used to share only one
nil node among all trees. If there were one different nil node
per tree, only a very few memory would be required more,
but the problem would be solved.



Sun, 28 Oct 2001 03:00:00 GMT  
 Std::set and passing between DLLs

Quote:



>   You could declare STL members of your class as private,
>   to be safer. This way is a bit limiting for STL, but if you also
>   use COM interface, it is obvious and necessary.
> - the second one is to export your STL-derived class,
>   and ALL the classes used by your class (at least the ones
>   which have static members). But I did not succeeded in exporting
>   any instance of _Tree template class (I do not know why).

> Another solution could be to remove static members from Xtree.
> These are _Nil and _Nilrefs, and are used to share only one
> nil node among all trees. If there were one different nil node
> per tree, only a very few memory would be required more,
> but the problem would be solved.

I believe SP2 fixes this problem. If not, get the updated map, xtree etc. files
from www.dinkumware.com/vc_fixes.html.
They already dealt with this issue

Nick



Sun, 28 Oct 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Passing std::set and std::map objects between 2 dlls

2. std::vector passed to DLL class never arrives???

3. HELP: Passing std::list to a DLL

4. passing std::ofstream by reference to DLL

5. passing std::vector to a DLL by reference

6. passing std::map based objects from an app to a dll

7. export classes using std namespace (ex std::vector) in a DLL

8. STL set passed from DLL

9. access violation passing set to dll

10. RESULT: comp.std.c++ moderation passes 378:37

11. Linking a static library that uses std::set

12. Diffrent ways of useing std::set or any template container

 

 
Powered by phpBB® Forum Software