Segmentation fault when free [SOLVED]

Unga unga888 at yahoo.com
Sat Sep 20 14:04:10 UTC 2008


--- On Sat, 9/20/08, Giorgos Keramidas <keramida at ceid.upatras.gr> wrote:

> > ktrace.out shows:
> > malloc_init()
> > 0x8103400 = malloc(1024)
> > malloc_init()
> > malloc_init()
> > 0x810b0b0 = malloc(400)
> > :
> > so many malloc
> > :
> > so many free
> > :
> > malloc/free combinations
> > :
> > free(0xbfbfc9c9)
> >
> > 1. This clearly shows my program is trying to free a
> memory that has
> > not been allocated. How it could have happened?
> 
> Aha.  This looks remarkably like an address in the runtime
> stack.  It
> usually happens when you have a function that returns the
> address of a
> 'local' variable, instead of a newly allocated heap
> area, i.e.:
> 
>     char *
>     function(void)
>     {
>         char buffer[100];
> 
>         return buf;
>     }
> 

This was indeed the case, worst yet, I was trying to free a part of the buffer (as per your example above) by mistake which was not allocated by malloc.

Thank you all who helped me. 

Best regards
Unga





      


More information about the freebsd-questions mailing list