Inside my template function I have the following code:
TypeName myFunction()
{
TypeName result;
void * storage = malloc( sizeof( TypeName ) );
/*Magic code that stores a value in the space pointed to by storage*/
result = *(TypeName *)storage;
free( storage );
return result;
}
This causes an "HEAP CORRUPTION DETECTED" error.If I don't call the free() function, the error doesn't occur, but I am afraid that I am creating a memory leak.what would be the proper way to return the value of "storage" and then deallocate the memory?