Static data with DLL  
Author Message
prayami





PostPosted: Visual C++ General, Static data with DLL Top

Hi..

We got a huge library made using C++. We want to call that library from other software like LabVIEW.

And the best option I found is DLL. But I come to know that DLL in 32 bit have some issues with static

data. i.e. Static data will not be common for all the instances of the applications in 32 bit. Is there any

way to over come this problem without chaning much things into the code

Two more questions I have is :

-> Does DLL is capable working with C++ not C

-> When I call any method from other software (i.e. LabVIEW) and close the application, will it call the

destructor at the end . Similarly when I we load the DLL, will it call the constructors in the programs

Thanks in advance...



Visual C++5  
 
 
Viorel.





PostPosted: Visual C++ General, Static data with DLL Top

[...] Static data will not be common for all the instances of the applications in 32 bit. Is there any

way to over come this problem without chaning much things into the code [...]

I think you can try a method based on shared sections:

#pragma data_seg("MYSHARED")

int mySharedInteger = 0; // (Note that variables must be initialized)

char mySharedString[100] = "";

#pragma data_seg()

#pragma comment(linker, "/section:MYSHARED,RWS")

These variables will be common for all of instances of your DLL. But you cannot share pointers in this manner.

For more information see http://msdn2.microsoft.com/en-us/library/h90dkhs0.aspx.

I hope this helps.


 
 
prayami





PostPosted: Visual C++ General, Static data with DLL Top

Thanks Viorel,

In the shared section, do declare a class instance as well Means is this section is only for variable declaration or

I can declare a class instance as well

You said, we can not share pointers in this maner Is there any other way, we can share pointers

Please let me know....Thanks....


 
 
Mike Danes





PostPosted: Visual C++ General, Static data with DLL Top

I'm afraid that the news are not very good. In summary sharing data between processes is not easy and if the code wasn't designed for this it's going to be even harder to do it.

"In the shared section, do declare a class instance as well "

The article pointed by Viorel already has this information:

  • Classes with virtual functions always contain function pointers. Classes with virtual functions should never be stored in shared data segments nor in memory mapped files. This is particularly important to MFC classes or classes that inherit from MFC.

  • Static data members are implemented as the equivalent of global variables. This means that each process would have its own copy of that class's static data members. Classes with static data members should not be shared.

  • The initialization requirement of a shared data segment causes a particular problem for C++ classes. If you have something like CTest Counter(0); in a shared data segment, the Counter object gets initialized in each process as they load the DLL, potentially zeroing out the object's data each time. This is very different than intrinsic data types that are initialized by the linker when it creates the DLL.

In addition to the above and one of your questions ("When I call any method from other software (i.e. LabVIEW) and close the application, will it call the destructor at the end") I'll add that the destructor of a shared object will be called whenever a process that uses that DLL exits. Needless to say that destroying the same object multiple times can have very bad effects.

Even worse, if your dll does not use threads you probably don't have any form of thread synchronization around this static variables. Sharing this variables between processes will make them accessible from multiple threads and without synchronization you'll have troubles.

And altough this may not be something to worry in your case I'll add that shared sections are a security hole because they can be accessed from any process of any user (a process runing as a non admin user can access the shared section in a process running as administrator). A much better alternative to shared sections is to use file mapping objects (see CreateFileMapping, MapViewOfView functions).

As for pointers:

It's practically impossible to share pointers between two processes. For a pointer to be valid in both processes the object pointed by it must be in a shared section and that shared section must be at the same address in both processes. The problem is that there is nothing that guarantess you that the shared section will be at the same address in both processes. Event if it were so you are limited to share pointers to objects in this shared section, you cannot share a pointer returned by the new operator or malloc function for example.

And a final question: why exactly do you need to share this variables between processes


 
 
prayami





PostPosted: Visual C++ General, Static data with DLL Top

Thanks Mike Danes

The library is made with lots of features in that. The static part is for PCI Memory Menager driver class. It was kept static because, there are different applicatins

in which PCI Memory Menager driver class is used to do the same things. If we don't keep it static then everytime it will allocate the different memory. Which may

not be proper programming. Because some user might open more than one applications.

If static pointers are not possible with DLL then I have to convert that.

One thing to ask is in the class instance we want to share have virtual destructor, will it create problem

with file mapping Note: I haven't use any MFC class or inheritance in that....

As I am new to Visual C++ & specially DLL, it will be greate if you give some more ideas about "CreateFileMapping, MapViewOfView functions" or some examples or link

will be helpful.

Thanks....