2

Admittedly I am a novice and self-taught programmer, and am finally venturing into the depths and power of C and C++. Some things that come with this self-learning process are not textbook or overtly googleable knowledge, such as tricks to use in difficult circumstances, and debugging strategies.

I am using boost::interprocess to set up shared memory using the managed_shared_memory and named objects. Unsurprisingly, my program crashes here and there for a number of reasons during my development. So far, I am rather unfamiliar with debugging tools, especially in linux. As a consequence, many times my shared memory does not get removed properly, as the crashing can result in destructors never being called, etc.

So, after such a program crash, when I attempt to run my application again, when my code tires to allocate a new segment of shared memory I see messages like:

terminate called after throwing an instance of 'boost::interprocess::interprocess_exception'
  what():  boost::interprocess_exception::library_error

which I understand is the result of the lingering shared memory of the same name that never got closed, and is now rogue. I have tried using the

open_or_create

flag in my application, in hopes that subsequent instances of my program would reconnect that shared memory, and then I could find/clear the previous objects in the segment, and start over as if it were a fresh run. However, this doesn't happen. My application throws the above error, or hangs, and I can't proceed with subsequent attempts at running my program.

What is an effective way of clearing out shared memory after a crash like this, so that I can run again after editing/rebuilding my application?

Right now, the only thing I can do is to do my best to avoid these crashes. But when crashes do occur, all I know how to do at the moment to run again successfully is to first reboot. Time consuming, and awkward, and certainly short of something optimal a more experienced programmer would do.

Any suggestions would be appreciated! Thanks, B

EDIT: Is there any specific advice how to proceed with ipcs and ipcrm?

On a fresh boot, I can run ipcs, then my app, then ipcs a second time (while my app is still open, and shared memory still accessible), and I see no difference in any shared memory segments.

I am allocating memory with a statement that looks like this

managed_shared_memory segment(open_or_create, "sharedMemtest", 1048588)
10
  • On *nix systems, I would try the ipcs and ipcrm commands. Jun 10, 2016 at 20:21
  • C/C++ is not a language. You are writing C++. Jun 10, 2016 at 20:38
  • 1
    @user3386109, thanks, I'll look into those
    – brneuro
    Jun 10, 2016 at 20:43
  • 1
    @AlanStokes, thanks :-/ yes I am aware C/C++ is not a single language, i never claimed it was. I had written these two with a slash as is commonly used to group the likes of them together. For your reading pleasure, I have edited my question and replaced the slash with 'and'
    – brneuro
    Jun 10, 2016 at 20:45
  • @AlanStokes you're reaching--he neither claimed 'C/C++' was a language nor that he was programming in it; he simply was giving background for his question and using it as shorthand for 'C and C++'. if you pay more attention next time you might note that he correctly tagged the question with 'c++', anyway
    – obataku
    Jun 10, 2016 at 20:46

1 Answer 1

-1

Add

 shared_memory_object::remove("sharedMemtest");

before your

managed_shared_memory segment(open_or_create, "sharedMemtest", 1048588);
3
  • Could you give more details?
    – Idan
    Jul 22, 2016 at 3:37
  • may use construct function when open a shm; 1. use remove 2. segement find_or_construct instead of construct function
    – Eric
    Jul 22, 2016 at 9:25
  • It just a feedback of how you answered the question. Try and edit your question to add all the details that the SO might need. Try and review before: stackoverflow.com/help/how-to-answer
    – Idan
    Jul 22, 2016 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.