Question C++ weird destructor behavior.

Carlis

Senior member
May 19, 2006
237
0
76
Hi
I have encountered a strange behavior of my c++ code when calling a destructor. Basically, I do the following:

I have a pointer to an object v of type diagVertex.
Then I delete it:
delete v; v=NULL;
This should call the destructor of diagVertex:

diagVertex::~diagVertex()
{
____diagTable->popVertex(this);
}

The method popVertex removes the object from a data base called diagTable. Now, what is weird is that it is only removed sometimes. On some occasions, it remains in the database non the less. And, using breakpoints, I notice that the function popVertex is not always called upon deleting v, but only sometimes. However, if I change my code and call the pop function before deleting, then it works like a charm, i.e.:

v->diagTable->popVertex(v);
delete v; v=NULL;

and

diagVertex::~diagVertex()
{

}

--------
This is puzzling to me to say the least. Is there something I have gotten wrong about the destructor? Does anyone have an idea about what I am doing wrong?

Best
//
Johan
 

BFG10K

Lifer
Aug 14, 2000
22,709
2,996
126
How is v initialized? In a single place, or at multiple points in the program? I'm guessing you're leaking memory by sometimes re-assigning v before deleting it. The destructor will never be called in those cases.

But you shouldn't be using manual new/delete in C++ anyway unless you have a very good reason. Use std::unique_ptr or better yet, use a stack object instance.
 

Carlis

Senior member
May 19, 2006
237
0
76
Thanks for the input. My thought was also that it would be a memory leak.

After some reading, and thinking, I realized what the problem is:
diagVertex is an inherited class of type diagElement. In some instances, the pointer to v is of type diagElement. When calling the destructor, the call is made to the destructor of diagElement it seems, https://www.quantstart.com/articles/C-Virtual-Destructors-How-to-Avoid-Memory-Leaks/ .
So this seems to explain the source of the error... You learn something everyday I guess. I'm a physicist and completely self-taught when it comes to programming, but with c++ this is dangerous sometimes

On a side note, you mention new/delete is bad form in general. I do this all the time . Why do you think so?
 

BFG10K

Lifer
Aug 14, 2000
22,709
2,996
126
On a side note, you mention new/delete is bad form in general. I do this all the time . Why do you think so?
It's old style C++ which is extremely error-prone. A single mismatched new/delete anywhere in the program is a serious problem that can be almost impossible to find, and is extremely easy to happen without realizing.

Stack objects and STL containers are far safer because the memory management is encapsulated away from you. If you have to use pointers, either make them non-owning so they're never responsible for the memory, or use std::unique_ptr if you need ownership.

As an aside, I also avoid inheritance as well. It quickly becomes a nightmare to track what's actually going on with all that internal coupling. Object composition is far simpler because each class remains fully shielded from the others. Or better yet, use free functions if they're obviously the simpler choice. So many people rush to create multi-inheritance templated monstrosities for no good reason.
 

Hail The Brain Slug

Diamond Member
Oct 10, 2005
3,502
2,451
136
Thanks for the input. My thought was also that it would be a memory leak.

After some reading, and thinking, I realized what the problem is:
diagVertex is an inherited class of type diagElement. In some instances, the pointer to v is of type diagElement. When calling the destructor, the call is made to the destructor of diagElement it seems, https://www.quantstart.com/articles/C-Virtual-Destructors-How-to-Avoid-Memory-Leaks/ .
So this seems to explain the source of the error... You learn something everyday I guess. I'm a physicist and completely self-taught when it comes to programming, but with c++ this is dangerous sometimes

I had a feeling it had something to do with inheritance but you didn't give enough information and I didn't want to speculate.

Code snippets are worth a million words.
 

mv2devnull

Golden Member
Apr 13, 2010
1,511
149
106
On a side note, you mention new/delete is bad form in general. I do this all the time . Why do you think so?
Memory is a resource and releasing it properly on all possible execution paths (some of which may be exceptionally unforeseeable) is tedious. RAII helper handles, such as std::unique_ptr, save your time.

Resource Acquisition Is Initialization or RAII:

 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |