michalp
June 16th, 2012, 04:55 PM
Hi
I just started with C++ and I did something like this:
main.cpp
int main ( int argc, char** argv )
{
MyClass * First = new MyClass();
First()-> DoSomething();
First->~MyClass();
delete First;
return 0;
}
and MyClass.h
class MyClass
{
public:
MyClass();
virtual ~MyClass();
protected:
private:
AnotherClass * Second;
};
MyClass.cpp
MyClass::MyClass()
{
Second = new AnotherClass();
}
MyClass::~MyClass()
{
Second->~AnotherClass();
delete this->Second;
}
Let the AnotherClass is total empty. What is wrong with this? I got segmentation fault at the end of the program. Could you please help me? As far as I know when I use new then I should have use delete. When I comment the line with delete this->Second; then the program will exit cleary but of course valgrind will report a memory leak.
I just started with C++ and I did something like this:
main.cpp
int main ( int argc, char** argv )
{
MyClass * First = new MyClass();
First()-> DoSomething();
First->~MyClass();
delete First;
return 0;
}
and MyClass.h
class MyClass
{
public:
MyClass();
virtual ~MyClass();
protected:
private:
AnotherClass * Second;
};
MyClass.cpp
MyClass::MyClass()
{
Second = new AnotherClass();
}
MyClass::~MyClass()
{
Second->~AnotherClass();
delete this->Second;
}
Let the AnotherClass is total empty. What is wrong with this? I got segmentation fault at the end of the program. Could you please help me? As far as I know when I use new then I should have use delete. When I comment the line with delete this->Second; then the program will exit cleary but of course valgrind will report a memory leak.