I ran into this problem in my application after checking it for memory leaks and found that some of my classes are not destroyed at all.
The code below is divided into 3 files, it is assumed that it implements the pimpl template. The expected scenario is for both the Cimpl constructor and the destructor to print their messages. However, this is not what I get with g ++. In my application, only the constructor is called.
classes.h:
#include <memory> class Cimpl; class Cpimpl { std::auto_ptr<Cimpl> impl; public: Cpimpl(); };
classes.cpp:
#include "classes.h" #include <stdio.h> class Cimpl { public: Cimpl() { printf("Cimpl::Cimpl()\n"); } ~Cimpl() { printf("Cimpl::~Cimpl()\n"); } }; Cpimpl::Cpimpl() { this->impl.reset(new Cimpl); }
main.cpp:
#include "classes.h" int main() { Cpimpl c; return 0; }
Here is what I could discover next:
g++ -Wall -c main.cpp g++ -Wall -c classes.cpp g++ -Wall main.o classes.o -o app_bug g++ -Wall classes.o main.o -o app_ok
It seems that the destructor is called in one of two possible cases, and this depends on the binding order. With app_ok, I managed to get the correct script, while app_bug behaved exactly like my application.
Is there any kind of wisdom that I am missing in this situation? Thanks for any suggestion in advance!
c ++ gcc destructor
Tosha Sep 07 2018-12-17T00: 00Z
source share