Reputation: 1410
A simple question here, I have several classes in my code, but only one of them exhibits this issue and I cannot for the life of me work out why. When I create an instance of the class, the destructor is called straight after, yet the instance of the class does not actually appear to be deleted.
Maybe I could live with that if there were not delete[] operations in the destructor that DO affect the instance of the class.
I read somewhere about the 'rule of three' or something, so attempted to see what I was missing. I already have a default constructor as well as a user-defined one. I then added what I think is called a copy-constructor, something like this:
MyClass::MyClass(const MyClass &duplicate)
{
variable1 = duplicate.variable1;
variable2 = duplicate.variable2;
// etc
}
What am I missing here that could cause this issue?
EDIT: The requested code. I've renamed everything so that it's all clear (this code still compiles with the issue). First, the header file, MyClass.h
:
#ifndef MYCLASS_H
#define MYCLASS_H
#ifndef UNICODE
#define UNICODE
#endif
#include <string>
class MyClass
{
public:
MyClass();
MyClass::MyClass(const MyClass &);
MyClass(int, std::wstring inputWord, int);
~MyClass();
int intOne;
int intTwo;
};
#endif
Next MyClass.cpp
:
#include "MyClass.h"
#include <Windows.h>
MyClass::MyClass(const MyClass &duplicate)
{
intOne = duplicate.intOne;
intTwo = duplicate.intTwo;
}
MyClass::MyClass()
{
}
MyClass::~MyClass()
{
MessageBox(NULL, TEXT("TEST"), TEXT("TEST"),0);
}
MyClass::MyClass(int intOneInput, std::wstring stringInput, int intTwoInput)
{
intOne = intOneInput;
intTwo = intTwoInput;
}
And finally how I'm creating my object:
MyClass test(0, TEXT("TEST"), 0);
[Copied from op's comment]
Actually, scratch my last comment, the deconstructor is NOT called with that particular line (until it goes out of scope), the line that does is words.push_back(MyClass(0, TEXT("TEST"), 0));
declared as std::vector<MyClass> words
Upvotes: 4
Views: 6914
Reputation: 569
I know this is an old thread but if it can help someone in the future, I've seen the same issue testing the code in this post when copy constructor is not defined: https://www.viva64.com/en/w/v690/:
1 raise 0x7ffff4e320e0
2 abort 0x7ffff4e336c1
3 __libc_message 0x7ffff4e75427
4 malloc_printerr 0x7ffff4e7bc43
5 MyArray::Clear mainwindow.cpp 50 0x4204a5
6 MyArray::~MyArray mainwindow.cpp 53 0x4204e2
7 MainWindow::MainWindow mainwindow.cpp 73 0x41416b
8 main main.cpp 10 0x40c372
And I see in your code the copy constructor is defined the header with:
MyClass::MyClass(const MyClass &);
Instead of simply:
MyClass(const MyClass &);
The I'm not sure your copy constructor is properly declared and used.
Anyway with current GCC version this particular case should not happen since it should not pass because of "extra qualification ‘MyArray::’ on member ‘MyArray [-fpermissive]" error, but it can help people for which copy constructor is missing.
Upvotes: 1
Reputation: 2181
The destructor should be called when the object is destroyed. If you create the object with new
, The destructor will be called when you call delete
on the object. Otherwise it should be called when it goes out of its scope. You can set breakpoint inside its destructor and see the callstack to check what is calling the destructor. Hope it helps.
[Update]
Try to add the below printf
for all ctor and dtor to make it sure you are not confused with temporarily created objects.
printf("ctor %p\n", this); // in constructors
printf("dtor %p\n", this); // in destructor
[Update]
words.push_back(MyClass(0, TEXT("TEST"), 0));
This creates a temporary object as stl containers(like vector
) always "copy" things to store. (Unless "move" happens. I don't want to start explaining "move" and rvalue here.)
Upvotes: 3
Reputation: 264709
This:
MyClass test(0, TEXT("TEST"), 0);
declares an automatic object.
The object is destroyed when it goes out of scope.
{
MyClass test(0, TEXT("TEST"), 0); // constructed here
} // destructed here.
Upvotes: 1
Reputation: 10947
A possible issue (but we need to see more) is that your copy constructor perform a shadow copy and you construct your object copy-constructing from a temporary. When the temporary get destroyed, the memory held by the temporary is released and the new object now has pointer on deleted memory.
One solution is: Perform a deep copy when copy constructing.
http://www.learncpp.com/cpp-tutorial/912-shallow-vs-deep-copying/
Another is: Use smart pointers instead of raw pointers.
What is a smart pointer and when should I use one?
Upvotes: 1