• FEATURED

View more

View more

View more

\$50

### Image of the Day Submit

IOTD | Top Screenshots

## Error freeing allocated memory

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

Posted 22 June 2013 - 12:58 PM

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

Edited by rip-off, 23 June 2013 - 05:24 AM.
Mod edit, restoring redacted post

### #2SiCrane  Moderators

Posted 22 June 2013 - 01:07 PM

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.

Posted 22 June 2013 - 01:08 PM

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'.

Edited by Paradigm Shifter, 22 June 2013 - 01:09 PM.

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

### #4Álvaro  Members

Posted 22 June 2013 - 07:08 PM

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

Thanks a lot

### #5rip-off  Moderators

Posted 23 June 2013 - 05:25 AM