Jump to content

  • Log In with Google      Sign In   
  • Create Account


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.

  • You cannot reply to this topic
4 replies to this topic

#1 Astado89   Members   -  Reputation: 135

Like
-2Likes
Like

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


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9492

Like
3Likes
Like

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.



#3 Paradigm Shifter   Crossbones+   -  Reputation: 5213

Like
2Likes
Like

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   Crossbones+   -  Reputation: 12385

Like
0Likes
Like

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 smile.png
 
Thanks a lot


#5 rip-off   Moderators   -  Reputation: 7964

Like
0Likes
Like

Posted 23 June 2013 - 05:25 AM

Do not redact your posts.






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.



PARTNERS