Recommended Posts

Hi,
The following code seems to work fine:
CString *test;
while (true) {
test = new CString("Test");
delete test;
}
But when I put "CString *test" within the loop:
while (true) {
CString *test = new CString("Test");
delete test;
}
I get a memory leak. Anyone have any idea why?
First of all I realise I shouldn't be declaring CString within the loop, it is only like this for testing purposes as I'm actually trying to figure out why i have a memory leak somewhere else in the program and I figured CString was the most likely culprit.
My Constructor & Destructor for CString are:
CString::CString(char *initialStr) {
str = new char[strlen(initialStr+1)];
strcpy(str, initialStr);
}
CString::~CString() {
delete str;
}
str is declared as:
char *str;
Thank you!