Hi, I am a begginer dealing (trying to understand actually) dynamic memory. I took the following code from a book and try to see how it works, and I get memory leaks (using visual leak detector) when executing the following code:
(there's a "char* pmessage" private member in the class definition, deleted in the class destructor)
1 2 3 4 5 6 7
// Constructor definition
CMessage(constchar* text = "Default message")
{
cout < < "Constructor called." < < endl;
pmessage = newchar[strlen(text) + 1]; // Allocate space for text
strcpy_s(pmessage, strlen(text)+1, text); // Copy text to new memory
}
But, if I change the code as follows, memory leaks disappear:
1 2 3 4 5 6 7 8 9 10 11 12
// Constructor definition
CMessage(constchar* text = NULL)
{
cout << "Constructor called." << endl;
if(!text)
pmessage = NULL;
else
{
pmessage = newchar[strlen(text) + 1]; // Allocate space for ext
strcpy_s(pmessage, strlen(text)+1, text); // Copy text to new memory
}
}
My question is: why is it happening??
As far as I understand, there's no memory allocation (using the new op) in the function (constructor) call. So I don't get it.
Please, help.
Thank you!
I don't know why you get a memory leak. It depends on the rest of the code.
In your second code, if text is always null then there will of course be no memory leak caused by this code because there is no dynamic memory allocation.
In the complete code below, there are two objects (motto1 and motto2 - lines 96 and 97) initialized with a text string, using the "const char* constructor" shown above. If I set it to "Default Message", I get memory leaks, but, if I set it to NULL, there are no memory leaks (Output messages after code). Thus, where is the memory allocation? since no "new" operator is used inside this constructor?
I was wondering whether it has to do with something going on in the stack frame of a constructor, so I copied the code of the constructor to a function, and added an extra bit of code to clean the private member:
1 2 3 4 5 6 7 8 9 10 11 12 13
// Test function
void Assign(constchar* text = "some text...") // *** NO ERROR !!! *** //
{
cout << "const char* test function called." << endl;
if(!text)
pmessage = NULL;
else
{
delete[] pmessage;
pmessage = newchar[strlen(text) + 1]; // Allocate space for text
strcpy_s(pmessage, strlen(text)+1, text); // Copy text to new memory
}
}
and added the following line to main()
motto3.Assign();
and got no memory leaks!!!
1 2 3 4 5 6
/*
Visual Leak Detector Version 2.4RC2 installed.
'CMessage.exe': Loaded 'C:\Windows\System32\apphelp.dll', Cannot find or open the PDB file
No memory leaks detected.
Visual Leak Detector is now exiting.
The program '[5384] CMessage.exe: Native' has exited with code 0 (0x0). */
could it has to do with something going on in a constructor??? more doubts than certanties... perhaps has to do with the compiler... I don't know
The problem is with operator+. When you create the variable message on line 23 the constructor allocates memory that is pointed to by message.pmessage but on the next line (24) you reassign message.pmessage without first freeing the memory.
Good! nice and safe. I`ll make it part of my code.
I thought you cannot delete a null pointer, and it would throw an exception, for trying to delete a memory that was not allocated... too much imagination! I better make a simple test before making these kind of assupmtions.
I better make a simple test before making these kind of assupmtions.
If you are unsure it's better to look it up somewhere. C++ is not a language suitable to learn by trial and error. Many things have undefined behavior, such as dereferencing a null (or invalid) pointer, division by zero, writing to an array out of bounds, etc. What will happen in such cases are usually not predictable. It might crash the program, or the program might act buggy or it might just "work".