Error freeing allocated memory

Started by
3 comments, last by rip-off 10 years, 10 months ago
Hi everyone,
my question is a c++ based topic, but I'm using Cocoa and Xcode.
I'm allocating sone buffers to send files over the network.
When i read the network-input into the buffer i process it in another object called Controller.
after that i want to release(free) the buffer in order not to get any memory leaks.
But here's my problem the program crashes and says:

Game(3984,0x103481000) malloc: *** error for object 0x102009600: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
but why? Here's the code:
So this is the code of my Network-Controller which gets the input as follows:

int n = 0;
    char *buffer = malloc(2049*sizeof(char));
    while ( (n = (int)read( sockfd, buffer, 2048)) ) {
        buffer[n] = '0';
        [cont networkinput:buffer length:n];
        free(buffer); Error!!!
    }
    connected = NO;
[cont networkinput:buffer length:n];
sends the buffer to the Controller and the class processes it.
but why not allocated? I used malloc.
Can anyone help me? If I take out the freeing my memory must be increased to 1000GB
Thanks a lot
Advertisement

I see two problems. One is that you're using '0' as a terminator rather than '\0'. Two is that you allocate once and free every time you go through the loop. In any case, since you're only allocating a fixed sized buffer then you don't need malloc() in the first place. You can just put the array on the stack.

You are freeing it once per iteration of the loop.

Free it after the loop finishes all its iterations (i.e. outside the while).

Even better, just have a local variable buffer instead.

char buffer[2049] = { '\0' };

EDIT: Ninja'd. I didn't notice the '0' bit either, that's completely wrong ;) Use '\0'.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

It is rude to remove the content of your original post. Someone could learn something from the thread, but not if you deface it.

Here's approximately what the original post was:

Hi everyone,

my question is a c++ based topic, but I'm using Cocoa and Xcode.

I'm allocating sone buffers to send files over the network.

When i read the network-input into the buffer i process it in another object called Controller.

after that i want to release(free) the buffer in order not to get any memory leaks.

But here's my problem the program crashes and says:

Game(3984,0x103481000) malloc: *** error for object 0x102009600: pointer being freed was not allocated

*** set a breakpoint in malloc_error_break to debug

but why? Here's the code:

So this is the code of my Network-Controller which gets the input as follows:

int n = 0;
char *buffer = malloc(2049*sizeof(char));
while ( (n = (int)read( sockfd, buffer, 2048)) ) {
buffer[n] = '0';
[cont networkinput:buffer length:n];
free(buffer); Error!!!
}
connected = NO;

[cont networkinput:buffer length:n]; sends the buffer to the Controller and the class processes it.


but why not allocated? I used malloc.
Can anyone help me? If I take out the freeing my memory must be increased to 1000GB smile.png
Thanks a lot

Do not redact your posts.

This topic is closed to new replies.

Advertisement