The bug is in these two lines:
char* iopszString = new char[1];
iopszString = "";
You are allocating new memory with new
and you store its location in your pointer iopszString
. You then assign the location of the string literal ""
to that pointer, so the value of the pointer itself changes. It now points somewhere else, to a memory location that you haven't allocated with new
and that you don't own. You therefore lost the pointer of the memory you allocated (a memory leak) and when you call delete[]
on the pointer to the location of ""
, it crashes (as you can not free anything with delete[]
that you haven't allocated with new
.
You probably meant to write:
char* iopszString = new char[1];
iopszString[0] = '\0';
Which will only set the value of the first char
that you allocated to '\0'
and therefore turns it into a valid, empty, zero-terminated string.