Memory Leak Due To Destructor Not Being Called

0

I am trying to track down a memory leak in code i didn't write and have traced it to a function foo(). From debug prints i have further confirmed that it appears said function calls the constructor for foo(), but never the destructor.

I cannot share the exact code, and it is a bit cumbersome, but i have mocked up a sketch of the relevant parts below.

foo.cpp

Foo::Foo(){};
Foo::~Foo(){};
...

foo.hpp

#include bar.hpp

class Foo : public Bar {
  public:
    Foo();
    ~Foo();
}
...

bar.cpp

Bar::Bar(){
  pFizz = new fizz();
  printf("Allocate 0x%x", (uint32_t)&pFizz);
}

Bar::~Bar(){
  printf("Allocate 0x%x", (uint32_t)&pFizz);
  delete pFizz;
}
...

bar.hpp

class Bar{
  public:
    Bar()
    virtual ~Bar()
  private:
    Fizz pFizz;
}
...

I feel some more context may be necessary. Going to abstract it a bit but the jist of it is that the code is in a redundant system, so there are two different threads on different systems. One executing, and the other keeping track of where the first is and the data it produces. When the executing thread is killed, the secondary thread takes over and spins up a new thread to keep track of the data on a different system. Every time something becomes the primary it needs to allocate memory for itself, which is no problem providing the secondary deallocated beforehand.

The test that showed the memory leak simply kills the primary thread, causing it to transfer control to the secondary. If i run the test with the prints described above i see something like as follows.

Allocate: 0xDEADBEEF

Allocate: 0xDEADBEF7

Allocate: 0xDEADBEFF

and so on, with the destructor's print never appearing. After 1000 or so runs the program errors out at the allocation of foo, saying it is out of space in what is effectively the shared memory.

I for one, am at a loss as to what can cause a destructor to not trigger, this is the only object in the codebase that is leaking. So i thought i would ask if you all had any theories while i try to puzzle it out.

c++
multithreading
oop
multiprocessing
asked on Stack Overflow Apr 1, 2020 by Falderol

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0