Emil D
Emil D

Reputation: 1904

Heap corruption issues

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?

Upvotes: 2

Views: 262

Answers (5)

bta
bta

Reputation: 45057

What about:

TypeName myFunction() {
    TypeName result;
    void* storage = &result;

    /*Magic code that stores a value in the space pointed to by storage*/

    return result;
}

Here, all your variables will be stored on the stack so you shouldn't encounter heap-related problems (depending on what exactly your "magic" code does).

Is there a reason why you have your storage array separate from result? If the results will simply be copied into result, it would make more sense (IMHO) to only use one object (and either keep a void* pointer to it or type-cast &result as needed).

If there is a reason to use a separate storage and result, you will probably get better milage using TypeName storage = new TypeName and delete instead of malloc(4) and free.

Upvotes: 1

Adam Liss
Adam Liss

Reputation: 48290

I think your confusion lies in this line:

 void * storage = malloc( 4 );

It looks like you're trying to allocate space for a 4-byte pointer, but that's not what you need to do. Let's break the line into two steps:

void * storage;          // This allocates 4 bytes for a variable of type "pointer to void"
storage = malloc( 4 );   // This allocates 4 _more_ bytes and sets "storage" to their address.

I'm assuming that the "magic" code copies data from a variable of type Typename to the memory that was allocated for storage with this effect:

memcpy(storage, data_from_a_Typename_variable, sizeof(Typename));

So if sizeof(Typename) is larger than the 4 bytes that were allocated to storage you'll see the heap-corruption error.

As the other answers indicate, what you need to do is allocate enough space for a Typename variable, like this:

void * storage = malloc(sizeof(Typename));

But, as Liz Albin suggested, you've already allocated space for a Typename in result so it's simpler to pass &result or (void *) &result to the magic function.

Upvotes: 1

t0mm13b
t0mm13b

Reputation: 34592

Why did you malloc with 4 bytes and yet casted to the type name of TypeName? That definitely looks odd!

The other answers are hinting you what it is...!

Hope this helps, Best regards, Tom.

Upvotes: 0

Liz Albin
Liz Albin

Reputation: 1489

You don't need to allocate storage, You could probably pass your result variable into a function that does your magic stuff. Something like this.

void magic(void *buffer)
{
  // magic stuff 
}

TypeName foo()
{
   TypeName result;
   magic(&result);
   return result;
}

Or of course you could have your TypeName structure set up as bit-fields or whatever your magic code manipulate...

Upvotes: 3

Andrey
Andrey

Reputation: 60065

don't call it like this:

TypeName result;
void * storage = malloc( 4 );

you should call it

TypeName result;
void * storage = malloc( sizeof(TypeName) );

anyway code looks strange :)

Upvotes: 2

Related Questions