The Destructor can't cleanup the memory?
-
Hello, I have an unmanaged document class in my code, that looks like this
class MyDocument
{
public:
MyDocument()
{
// Initilize the dynamic structures throught call to 'new' operator
}
~MyDocument()
{
// Cleanup the code
this->CleanUp(); // When the function is called, it can't access the memory
}
bool CleanUp()
{
// Clean all the memory using 'delete' operator
}
};Then I have a pointer to it in my Form (that is a .NET managed class, a Form object).
MyDocument *document;
Inside the Constructor of the form, I initialize the document with
this->document = new MyDocument();
The
Dispose()
function of the Form has the code:if(disposing && document)
delete document;During the lifetime of the program, several time i need to free the resouces of the document class and allocated new ones, like this:
document->CleanUp(); // Code runs alright
// allocated new resources to the documentHowever when I close the application (i.e. the destructor of the form runs), I get an error that
CleanUp()
can't access the memory (because it is not accessible, may be it has been cleaned already). Could any one please explain why I can callCleanUp()
anywhere in my program and I get no error (the memory gets cleaned as expected), and I get a'Memory Not Accessible'
error when I call it from the destrcutor. (I tried to comment out the code in the destructor, and the application gets cured i.e. It produces no errors/exceptions etc. - A programmer's national anthem; "AAAAAHHHHH!!!!" -
Hello, I have an unmanaged document class in my code, that looks like this
class MyDocument
{
public:
MyDocument()
{
// Initilize the dynamic structures throught call to 'new' operator
}
~MyDocument()
{
// Cleanup the code
this->CleanUp(); // When the function is called, it can't access the memory
}
bool CleanUp()
{
// Clean all the memory using 'delete' operator
}
};Then I have a pointer to it in my Form (that is a .NET managed class, a Form object).
MyDocument *document;
Inside the Constructor of the form, I initialize the document with
this->document = new MyDocument();
The
Dispose()
function of the Form has the code:if(disposing && document)
delete document;During the lifetime of the program, several time i need to free the resouces of the document class and allocated new ones, like this:
document->CleanUp(); // Code runs alright
// allocated new resources to the documentHowever when I close the application (i.e. the destructor of the form runs), I get an error that
CleanUp()
can't access the memory (because it is not accessible, may be it has been cleaned already). Could any one please explain why I can callCleanUp()
anywhere in my program and I get no error (the memory gets cleaned as expected), and I get a'Memory Not Accessible'
error when I call it from the destrcutor. (I tried to comment out the code in the destructor, and the application gets cured i.e. It produces no errors/exceptions etc. - A programmer's national anthem; "AAAAAHHHHH!!!!"signbit wrote:
bool CleanUp() { // Clean all the memory using 'delete' operator }
Make sure you set all pointers to
NULL
after deleting them. For eg:delete pointer;
pointer = NULL;
Nibu thomas Software Developer
-
signbit wrote:
bool CleanUp() { // Clean all the memory using 'delete' operator }
Make sure you set all pointers to
NULL
after deleting them. For eg:delete pointer;
pointer = NULL;
Nibu thomas Software Developer
-
Yeah, I always set the pointer to NULL after it's been deleted to avoid confusion, my code looks like:
if(NULL != pointer)
{
delete pointer;
pointer = NULL;
}- A programmer's national anthem; "AAAAAHHHHH!!!!"
signbit wrote:
Yeah, I always set the pointer to NULL after it's been deleted to avoid confusion, my code looks like:
Try to debug and find out. You should find something interesting. Set a breakpoint inside
CleanUp
, and step through.
Nibu thomas Software Developer
-
signbit wrote:
Yeah, I always set the pointer to NULL after it's been deleted to avoid confusion, my code looks like:
Try to debug and find out. You should find something interesting. Set a breakpoint inside
CleanUp
, and step through.
Nibu thomas Software Developer
-
Yeah, I always set the pointer to NULL after it's been deleted to avoid confusion, my code looks like:
if(NULL != pointer)
{
delete pointer;
pointer = NULL;
}- A programmer's national anthem; "AAAAAHHHHH!!!!"
Why do you need such a code? As far as I remember "delete NULL;" is a valid instruction, that is delete knows about NULL and know how to handle it. Basically, there is something similar to your "if(NULL != pointer)" within implementation of delete, so there is no need for extra one.
-
Yeah, I always set the pointer to NULL after it's been deleted to avoid confusion, my code looks like:
if(NULL != pointer)
{
delete pointer;
pointer = NULL;
}- A programmer's national anthem; "AAAAAHHHHH!!!!"
No need to check for NULL before deleting a pointer - it's a benign operation, as
free(...)
in C. -- The Blog: Bits and Pieces -
Yeah, I always set the pointer to NULL after it's been deleted to avoid confusion, my code looks like:
if(NULL != pointer)
{
delete pointer;
pointer = NULL;
}- A programmer's national anthem; "AAAAAHHHHH!!!!"
In C++
delete NULL
is a NOP - It does nothing. You don't need theif
statement in the code above, the following is equivalent:delete pointer; pointer = NULL;
If you put the
if
you're checking twice and the code is longer then need be (more chance of making a mistake). Steve -
In C++
delete NULL
is a NOP - It does nothing. You don't need theif
statement in the code above, the following is equivalent:delete pointer; pointer = NULL;
If you put the
if
you're checking twice and the code is longer then need be (more chance of making a mistake). Steve -
Okay, Okay, but why the destructor can't cleanup? - A programmer's national anthem; "AAAAAHHHHH!!!!"
I can't see anything wrong - Can you provide more detail? Steve