malloc segfaults

Juergen Bohn bohn@osc-es.de
Wed Sep 3 22:03:00 GMT 2003


Thanks for the hints.

gdb and strace show the buggy behavior on my machine.

Starting program: /cygdrive/e/verify/malloc/malltest.exe

Program received signal SIGSEGV, Segmentation fault.
0x61042e26 in strdup () from /usr/bin/cygwin1.dll
(gdb) bt
#0  0x61042e26 in strdup () from /usr/bin/cygwin1.dll
#1  0x6104326b in mmap64 () from /usr/bin/cygwin1.dll
#2  0x61043849 in mmap () from /usr/bin/cygwin1.dll
#3  0x610401bc in mktime () from /usr/bin/cygwin1.dll
#4  0x61040413 in mktime () from /usr/bin/cygwin1.dll
#5  0x610413e6 in malloc () from /usr/bin/cygwin1.dll
#6  0x004010e7 in main (argc=1, argv=0xa042630) at malltest.c:10

and

   448 1192931 [main] malltest 1472 mmap64: 630000 = mmap() succeeded
   249 1193180 [main] malltest 1472 mmap64: addr 0, len 1048576, prot 3,
flags 22, fd -1, off 0
   202 1193382 [main] malltest 1472 handle_exceptions: In
cygwin_except_handler exc 0xC0000005 at 0x61042E26 sp 0x22FCB4
   123 1193505 [main] malltest 1472 handle_exceptions: In
cygwin_except_handler sig = 11 at 0x61042E26
   118 1193623 [main] malltest 1472 handle_exceptions: In
cygwin_except_handler calling 0x0
1193743 [main] malltest 1472 handle_exceptions: Exception:
STATUS_ACCESS_VIOLATION
   120 1193743 [main] malltest 1472 handle_exceptions: Exception:
STATUS_ACCESS_VIOLATION

The wrapper from Bill Riemer's reply works, at least when I wrap my own 
malloc() calls.

More tips are still appreciated.

Best regards,
Juergen

Ronald Landheer-Cieslak wrote:
> Your code may have an unexpected side-effect: printf allocates memory as well.
> Try using this:
> 
> #include <stdlib.h>
> 
> int main(void)
> {
>    void * x;
> 
>    x = malloc(1000000);
>    while (x != NULL)
>    {
>       x = malloc(1000000);
>       if (x == NULL)
>          write(2, "malloc returned NULL\n", 21);
>    }
>                   
>    return(0);
> }
> 
> I've tested it under gdb and with strace, both say "malloc returned NULL". OTOH,
> when simply run from the command-line, I do get a segmentation fault.
> 
> HTH
> 
> rlc
> 
> On Tue, Sep 02, 2003 at 03:35:04PM +0200, Juergen Bohn wrote:
> 
>>Tested with cygwin1.dll 1.5.3-1 and 1.3.22-1 on Win2000-SP4, malloc() does 
>>not (always)
>>return NULL if there is no more memory available.  Try, for example, simple 
>>loops like:
>>
>>  x = malloc(10000);
>>  for (i=0; x != NULL; i++)
>>  {
>>      x = malloc(10000);
>>      if (x == NULL)    printf("x is NULL\n");
>>  }
>>
>>My application terminates with a segmentation violation, but all attempts 
>>to handle this by signal() or atexit() fail.  Unfortunately, also sysconf() 
>>does not work to get the number of available pages (_SC_AVPHYS_PAGES, I get 
>>always the same but wrong value).
>>
>>While testing, I detected that errno is set to 12 ("Not enough memory") 
>>after enough iterations through the for-loop above, while variable x still 
>>is not zero.  But still my application crashes even when I break the loop 
>>at errno!=0.
>>
>>Is there any secure way to find out, how much memory is available (or hope 
>>that the malloc() problems will be solved)?
>>
>>Many thanks,
>>Juergen
>>
>>
>>
>>--
>>Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
>>Problem reports:       http://cygwin.com/problems.html
>>Documentation:         http://cygwin.com/docs.html
>>FAQ:                   http://cygwin.com/faq/
> 
> 




--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/



More information about the Cygwin mailing list