Storing a vector in a DLL data segment

1.7k views Asked by At

Information

The following warning:
LINK : warning LNK4039: section '.SHARED' specified with /SECTION option does not exist
always occur whenever I try to store a vector in a data segment of a dynamic link library in C++.

For an example:

#include <vector>

struct Obj {
   unsigned int A;
   unsigned int B;
   bool C;
   std::vector< unsigned char > D;
};

#pragma data_seg( ".SHARED" )
std::vector< Obj > Objects;
#pragma data_seg()

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

However, if I try to store a simple variable, there will be no warnings upon compilation. Like this:

#pragma data_seg (".SHARED" )
int SimpleVariable = 0;
#pragma data_seg()

I am not absolutely positive but, I believe the warning was caused by not initializing the vector? If so, how could we initialize a vector that will be stored within the data segment?

I have another question, is this a terrible idea to store a vector in the data segment within a DLL?

My Goal

I am trying to share the content of a vector within a DLL, which will be loaded into several different processes.

Like this:

process_1.exe
    - example.dll -|    * access the same vector (SomeVector) as
                   |      example.dll within process_2
                   |
                std::vector SomeVector; // vector in example.dll
                   |
process_2.exe      |    * access the same vector (SomeVector) as
                   |      example.dll within process_1
    - example.dll -|    
1

There are 1 answers

0
Dan On

http://msdn.microsoft.com/en-us/library/h90dkhs0(v=vs.90).aspx

And how is an std::vector<> initialized?

yes it is a terrible idea to use a shared data segment.

Since the code executing is within the process space of that program you are going to default to allocating memory for your vector from that processes address space. In which any other process will throw an exception trying to read or write to.

You can write your own _alloc to pull from system shared memory, but this wont resolve your initialization issues.

The recommended method for ipc is via memory mapped files, and mutex's.

To get this to work the way you desire is going to require a good deal of code. Get comfortable looking through the Std:Vector code until you can derive a class from that template, or choose a faster route, and write your own from scratch.