
Ion GaztaƱaga wrote:
I just don't know why this function is called when you call close(). I would need more information. Could you just check if that destructor does anything apart from closing the segment (if any base or member has object destruction logic), and print the parameter "name" just before the assertion so that I can have more clues?
Hi Ion, yes, the destructor performs two destroy calls in front of the close call (see below). But it should not give a damn about, destroyed is destroyed. The call of close should only delete objects that still exists. I've printed out the name of the named shared object which triggers the error (it's "shmDefinition"). If I comment out the two lines which destroy my named shared objects in the destructor, it works. Hmm, I don't know why it not work when I destroy the objects by hand. But definitely it should not be done twice. Platform/Compiler: Linux version 2.6.17.1 (root@karo) (gcc version 4.0.4 20060507 (prerelease) (Debian 4.0.3-3)) Here is a short version of the class with con-/destructor: 13 class Myclass { 14 public: 15 Myclass(); 16 ~Myclass(); ... 27 private: ... 31 boost::shmem::named_shared_object segment; 32 typedef std::pair<int,int> shmDefinition; 33 typedef std::pair<char*,int> errDefinition; 34 shmDefinition* shmDef; 35 errDefinition* errDef; ... 37 const char* shmDefStr; 38 const char* errDefStr; 39 }; 25 Myclass::Myclass() : shmDefStr("shmDefinition"), errDefStr("errDefinition") 26 { 27 /* create or open the shared memory segment */ 28 if (!segment.open_or_create(SHMNAME,SHMMAX,(void*)0x30000000)) 29 { 30 std::cerr << "Can't create or open shm segment!" << std::endl; 31 exit(1); 32 } 33 34 /* try to find a shmDefinition */ 35 std::pair<shmDefinition*, std::size_t> shmRef; 36 shmRef = segment.find<shmDefinition>(shmDefStr); 37 38 /* there should be exactly one or nothing */ 39 assert(shmRef.second == 0 || shmRef.second == 1); 40 41 /* if there is an old definition, clear it */ 42 if (shmRef.second == 1 && shmRef.first) 43 { 44 shmDef = shmRef.first; 45 if (shmDef->first && shmDef->second) 46 { 47 void *shmData = segment.get_address_from_offset(shmDef->first); 48 segment.deallocate(shmData); 49 } 50 shmDef->first = shmDef->second = 0; 51 } 52 /* build a new one */ 53 else 54 { 55 shmDef = segment.construct<shmDefinition>(shmDefStr)(0,0); 56 } 57 58 if (!shmDef) 59 { 60 std::cerr << "Can't create or open shmDef!" << std::endl; 61 exit(1); 62 } ... 90 } 92 Myclass::~Myclass() 93 { 94 /* destroy named definitions */ 95 segment.destroy<shmDefinition>(shmDefStr); 96 segment.destroy<errDefinition>(errDefStr); 97 /* close segment, it should be deleted */ 98 segment.close(); 99 } Thanks a lot for your reply, Sascha