Opened 13 years ago

Closed 13 years ago

#4066 closed Bugs (invalid)

Boost::Interprocess::vector only pushes back so many correct values before it always pushes 0.

Reported by: powerking89670@… Owned by: Ion Gaztañaga
Milestone: Boost 1.43.0 Component: interprocess
Version: Boost 1.42.0 Severity: Problem
Keywords: Cc:

Description

I was perusing the documentation on interprocess, and I decided to scale up the example a bit to make sure it would suit my purposes.

#include <conio.h> //For getch
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/thread.hpp>
#include <stdio.h>

using namespace boost::interprocess;

//Define an STL compatible allocator of ints that allocates from the managed_shared_memory.
//This allocator will allow placing containers in the segment
typedef allocator<int, managed_shared_memory::segment_manager>  ShmemAllocator;

//Alias a vector that uses the previous STL-like allocator so that allocates
//its values from the segment
typedef boost::interprocess::vector<int, ShmemAllocator> MyVector;

int main(int argc, char* argv[])
{	
	struct shm_remove 
     {
         shm_remove() { shared_memory_object::remove("MySharedMemory"); }
         ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
      } remover;

      //Create a new segment with given name and size
      managed_shared_memory segment(create_only, "MySharedMemory", 80000);

      //Initialize shared memory STL-compatible allocator
      const ShmemAllocator alloc_inst (segment.get_segment_manager());

      //Construct a vector named "MyVector" in shared memory with argument alloc_inst
      MyVector *myvector = segment.construct<MyVector>("MyVector")(alloc_inst);

		myvector->reserve(5000);

      for(int i = 0; i < 5000; i++)  //Insert data in the vector
         myvector->push_back(5);

	     for(unsigned int i=0; i < myvector->size(); i++)
		{
			printf("%u: %u\n", i+1, myvector[i]);
		}



   _getch();
   return 0;
}

However, when this code is build/run it outputs the correct value for a while and then suddenly the values switch to 0 or vary widely.

Am I doing something incorrectly, or is this a problem with the lib?

Change History (2)

comment:1 by joseph.h.garvin@…, 13 years ago

In the printf you should be passing (*myvector)[i], not myvector[i]. With that change compiles and runs fine for me printing 5 the whole time on Solaris.

comment:2 by powerking89670@…, 13 years ago

Resolution: invalid
Status: newclosed

That solved the issue. I had thought I tried everything. I guess not!

Thank you very much!

Note: See TracTickets for help on using tickets.