tags:

views:

186

answers:

6

The following crashes when it gets to free: (there is reasoning for using void*).

bool s = true;

void* arg = &s;
argument = malloc(sizeof(arg));
memcpy(argument,arg,sizeof(arg));
free(argument);

argument here is a void*

why does free crash the application?

Thanks

+6  A: 

Are you sure it's free that's crashing not the memcpy()

Martin Beckett
My thoughts exactly.
Tom
Yes, it crashes at the free part
Milo
Doing invalid things does not always cause a crash right away. "Undefined behavior" means you're rolling the dice. (Though the next time you `delete` or `free()` something is a common time for a past mistake to come back and bite you.)
aschepler
@aschepler, good point - the crash may be totally unrelated to the code shown here, perhaps any `free` at this point would cause a crash.
Mark Ransom
Yes, it's the free that crashes. The `memcpy` is wrong but not going to crash reading past the end of a stack variable (there's plenty more readable memory on the stack above `s`).
Ben Voigt
+1 I think the memcpy is trashing the stack.
kenny
@kenny, No, it isn't. Here's my relevant comment to a now-deleted question <quote>`memcpy` isn't overwriting the pointer, it's overwriting the target of the pointer. Which incidentally is not sized correctly, but it's not overflowing `*argument` because number of bytes copied == number of bytes allocated.</quote>
Ben Voigt
WTH? An answer that gets early votes and then is overcome by the evidence (it was a reasonable question, but the OP said that the problem happens only when free is called, the memcpy doesn't crash) is marked correct. Sometimes the denizens of SO make me despair about the future of programming.
Ben Voigt
Martin Beckett
A: 

Are you certain it is free() that is crashing? It looks more likely that it is the memcpy() call. You are blindly copying X bytes, where X is the size of a pointer, from arg to argument. But you do not know what the size of the data is that arg points at.

Jonathan
Is there a way to know the size of it without knowing what the object is?
Milo
@Milo: no. (cruft to meet minimum comment length)
Ben Voigt
No. You need to know what the object is. If it is a structure, `sizeof(struct x)` will work. If it is a string, use `strlen(arg)`.
Jonathan
+2  A: 

arg points to a bool and you're memcopying 4 bytes from it! That's why it crashes. The bool is only one byte.

Michael Goldshteyn
You're half right.
Mark Ransom
Why am I half right, there's nothing wrong with the free itself.
Michael Goldshteyn
"The bool is [likely] only one byte [on your platform]."
GMan
While reading extra bytes beyond the variable is bad, that in itself is not likely to cause a crash unless the variable happens to be located near a non-readable area of memory (which isn't that likely).
TheUndeadFish
@TheUndeadFish: +1, especially true for local variables on stack that grow downward.
Ben Voigt
Although you're correct that the memcpy is copying too many bytes, that's unlikely to be the cause of the crash.
Mark Ransom
+6  A: 

Your problem is most likely elsewhere.

Since you have some misconceptions about sizeof (pointer) I suspect you have a buffer overflow/underflow elsewhere in your program which is resulting in corruption of heap headers, then free crashes when it tries to iterate across the corrupted heap.

Others are correct when they point out that you're reading sizeof (void*) from a variable which is only sizeof (bool), but since it's on the stack the memory just beyond s is almost certainly valid (it might contain other local variables, function arguments, or the return address, none of which would cause a program crash if they were unintentionally read).

memcpy can read unaligned data just fine, so even if you were on an architecture that crashes on unaligned accesses this wouldn't be the issue.

Check for overflow of other dynamically allocated buffers.

EDIT: I also want to respond to Milo's comment on a now-deleted question:

I need to get the object, which could be of any type, allocated – Milo

C++ doesn't work that way. malloc can only allocate memory for POD types, which have to meet some very strong restrictions. There is NO standard-compliant way, at all, to clone an instance of completely arbitrary type.

Some ways that work reliably on some types are:

  • if the type has a base type with a virtual Clone function, call Clone polymorphically
  • if the type is POD and you know the size, use new char[] and memcpy
Ben Voigt
Agreed. If the program has written past the end of other heap allocated arrays/buffers/objects/whatever then it may very well have corrupted the heap in ways that don't show up until a later call to `free`.
TheUndeadFish
+2  A: 
#include <string>

int main(void)
{
    void* argument;

    bool s = true;

    void* arg = &s;
    argument = malloc(sizeof(arg));
    memcpy(argument,arg,sizeof(arg));
    free(argument);

    return 0;
}

This code does not crash in VS 2010. In fact, there is nothing illegal about this code, as far as memory management goes. But yes, it does not function as intended because it copies garbage. Assuming a 32-bit environment, you are allocating 4 bytes and setting "argument" to point to these 4 bytes. Then you are copying 4 bytes from the memory that "arg" points to into the 4 bytes that "argument" points to. Yes, 3 of those 4 bytes that arg points to is garbage, but they are there. The compiler is going to allocate 4 bytes on the stack for bool s, not 1. Stacks need to be allocated in chunks of 4 bytes. So the memory is there, although there is 3 bytes that have garbage in them.

I suspect you have a corrupted heap from elsewhere and the call to free just so happens to crash here because of this.

Mario
Welcome to stackoverflow Mario. Agreed on the cause of the crash, +1 for going beyond my answer and actually running the code snippet. And because I want more people who understand pointers like you.
Ben Voigt
Hello, Ben. Thanks for the welcome! =]
Mario
A: 

Judging by your comments, you think sizeof(arg) is returning the actual size of whatever arg is pointing at. This is wrong. It's returning the size of a void* pointer.

Your memcpy is copying data from beyond the valid end of the source, but that's extremely unlikely to cause a crash.

If free is crashing when you give it a valid pointer, it indicates a trashed heap. Since there's no code in the example you show that would invalidate the pointer or trash the heap, I must conclude the problem lies elsewhere, before you get to this code.

Mark Ransom
Wow, it's your third answer to this question.
Johannes Schaub - litb
Third time's the charm? I think it's correct this time, even though someone else has already made the same points.
Mark Ransom