Sign in to follow this  
Followers 0
Astado89

Error freeing allocated memory

4 posts in this topic

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
Mod edit, restoring redacted post
-2

Share this post


Link to post
Share on other sites

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.

3

Share this post


Link to post
Share on other sites

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
2

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0