I'm trying to run my c++ code of which introduction of this new code has brought this memory corruption, can any one help me out what would be causing this. After the delete call I'm facing this issue. I also tried placing the newArgs[SZ] = '\0'; after each strncpy call.
The error says:
HEAP CORRUPTION DETECTED after Normal Block(#274) at 0X00C09600 etc
int main(HINSTANCE hInstance, HINSTANCE hPrevInstance, char* argc, int nShowCmd){
MyClass *obj;
char args[] = " hello world";
int SZ = strlen(args);
int argsLength = 0;
if(argc != NULL)
argsLength=strlen(argc);
SZ+=argsLength;
char *newArgs = new char[SZ];
strncpy(newArgs, "",SZ);
if(argc != NULL)
strncpy(newArgs, argc,argsLength);
StrCat(newArgs,args);
obj = new MyClass(newArgs);
delete[] newArgs;
return 0;
}
I can't see your
MyClassimplementation but I would be willing to bet that It keeps a copy of the pointer passed into it's constructor and that it is also trying to work with that after yourdeletecall.I'd strongly recommend that you stop
newingchar*s in C++ and usestrings.