3

I've really tried to put off asking this question because it seems like it should be a simple problem, but here goes. I've been chasing a segfault in a new piece of code. I've included the smallest chunk that demonstrates the issue below. The rest of the code has been working for some time.

class A {
private:
     std::map <uint64_t, uint32_t> memberMap;
public:
     A();
     ~A();
     void access() {
        const uint64_t key = 1;
        uint32_t value = 1;
        if(memberMap.count(key) == 0) {
             memberMap.insert(std::pair<uint64_t, uint32_t>(key, value));
        } else if(memberMap[key] < value) {
             memberMap[key] = value;
        }
     }
};

class B : public BsParent {
private:
     A handler;
public:
     B();
     ~B();
     uint64_t access(Data dat) {
        //...
        handler.access();
        //...
     }
};

What I've tried so far...

  • You'll notice there are zero explicit pointers.
  • I've checked that A() is called.
  • The stack trace from gdb (yep, already played with that extensively with no obvious null pointers) pins the blame on stl_tree.h:1157 which defines the pair function. However if I comment out the insert line and the else if block the program still segfaults at another obscure line number in stl_tree.
  • I tested the code in access() by making memberMap a local variable in access and calling the code in a loop.

Any thoughts or suggestions on further debugging steps?

AlaskaJoslin
  • 760
  • 8
  • 14
  • Try valgrind and/or the memory sanitizer options of GCC or clang, feels like you're playing with dead objects or trashed your heap or stack somewhere. Impossible to tell without a complete, minimal piece of code that actually reproduces the issue. – Mat Dec 04 '15 at 08:23
  • It seems that something else corrupts the memory belonging to the map. Try putting a data modification breakpoint. – Serge Rogatch Dec 04 '15 at 08:23
  • 1
    Please post [mcve]. If I take your example as-is, I can't reproduce the problem. – Algirdas Preidžius Dec 04 '15 at 08:25
  • 1
    Either it's corrupted memory or you are accessing the map concurrently from different threads. – Horstling Dec 04 '15 at 08:26
  • 3
    Write a test program only consisting of `Class A` and a `main()` where you make an instance of A and then calls `access`. Does it crash? My guess is no. I think you are looking for the error in the wrong part of the code. – Support Ukraine Dec 04 '15 at 08:36
  • Thanks everyone for the feedback. This wasn't the problem. These two classes are part of a simulator and the toy problem that I use for testing broke around when I started adding this feature. I did note that the posted code was incomplete, but I had already eliminated the rest of the code hierarchy. Again thanks. – AlaskaJoslin Jan 03 '16 at 02:30

1 Answers1

0

probably you have trimmed down your original code to keep the simplicity but it doesn't convey the problem rightly and is not reproducible.

Although It doesn't look to have any problem but if you are having map with non-native and local class type key/value pair you would probably require a copy constructor for the two.

Jamil Farooq
  • 131
  • 2
  • 9