3

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 -|    
XOR Bit
  • 31
  • 3
  • Probably because it cannot store a complex type in that data_segement. – Tony The Lion Aug 12 '13 at 08:11
  • Hmm, if you are correct, are there any other way to share a complex type between the same DLL, which are loaded into several processes? – XOR Bit Aug 12 '13 at 08:13
  • 2
    I believe the linker will only place *statically* initialized data into the data segment (instead of .bss). You could create a block of shared memory, then use placement new to place the vector there. You'd also need a custom allocator that allocated the associated data in the shared memory. – Jerry Coffin Aug 12 '13 at 08:19
  • TBH, I don't completely know how to write that. Do you know any good examples, articles, or even tutorials that could guide me? I believe, I have to use `MapViewOfFile` to create a block of shared memory. – XOR Bit Aug 12 '13 at 08:27
  • I don't think it is a good idea to place std::vector in shared section. Note that this class may allocate memory dynamically. While the vector housekeeping data will be in the shared section, the dynamically allocated buffer will not resulting to other processes accessing some random memory in there own address spaces. – yurish Aug 12 '13 at 08:46

1 Answers1

0

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.

Dan
  • 2,460
  • 2
  • 18
  • 12