I am trying to put a multimap inside of a separate class for easy lookup of objects downstream in another main code that I am trying to access.

If I put the multimap inside of my main routine and insert items from there it works fine. The second I move it into another class, it seems to be working, but I keep getting bus errors.

Any ideas?

Dani AI

Generated

reported a SIGBUS after moving a std::multimap into a separate class while the same insertion code worked in main. and rightly flagged that details are missing, but a short checklist usually finds the culprit. Note that std::multimap insertion does not invalidate iterators or references; see the std::multimap reference for container guarantees (cppreference — multimap).

Frequent causes when a container is moved into a class: dangling pointers (storing pointers to stack objects or to objects that get deleted), incorrect ownership or shallow copies in the class copy/move/destructor, global/static initialization order (if the class or map has static storage), concurrent unsynchronized access, or a comparator/value type that relies on invalid state. A SIGBUS can indicate invalid address or alignment problems; kernel/signal docs explain the signal semantics (signal(7)). Static initialization subtleties are described here ().

Practical steps: build a debug binary (-g, -O0) and get a backtrace at the crash, run Valgrind (valgrind.org) and AddressSanitizer (AddressSanitizer docs) to catch use-after-free and invalid writes, and simplify the test (use a POD mapped_type or make the container local again) to isolate whether the fault is in lifetime/ownership, concurrency, or value/comparator code. These approaches reliably narrow down the root cause.

Recommended Answers

All 2 Replies

Allow me to paraphrase your help request:
I have a program which do something but sometimes it does not do it. Any ideas?..

lol .. contrary to popular belief, we are not clairvoyant, so you need to elaborate on what you are doing , what your problem is and post some code you've worked on !!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.