memory leak or something wrong

yao liufeng ylfroy at 163.com
Wed Jun 20 04:25:02 CEST 2012


At 2012-06-19 20:27:55,"Marc Lehmann" <schmorp at schmorp.de> wrote:
>The debian manpage is explicit though:
>
>   if size is equal to zero, and ptr is not NULL, then the call is
>   equivalent to free(ptr)
>
After doing some research, I think the issue is not about realloc(ptr, 0), it's about realloc(NULL, 0). According to C99 standard,        malloc()
        If the size of the space requested is zero, the behavior is implementation-defined.

        realloc()
        If ptr is null, the realloc function behaves like the malloc function for the specified size.According to glibc,        If n is zero, malloc returns a minumum-sized chunk. (The minimum
        size is 16 bytes on most 32bit systems, and 24 or 32 bytes on 64bit
        systems.)

And, just adding some trace to libev, we can find between ev_loop_new() and ev_loop_destroy(), several ev_realloc_emual(NULL, 0), by default realloc(NULL, 0) was triggered. This can explain where the memory leaks are from. And it's better for libev to deal with this situation.
        ev_realloc_emul: ptr (nil), size 764, return 0x94ef008
        ev_realloc_emul: ptr (nil), size 768, return 0x94ef308
        ev_realloc_emul: ptr 0x94ef308, size 0, return (nil)
        ev_realloc_emul: ptr (nil), size 0, return 0x94ef308
        ev_realloc_emul: ptr (nil), size 0, return 0x94ef318
        ev_realloc_emul: ptr (nil), size 0, return 0x94ef328
        ev_realloc_emul: ptr (nil), size 0, return 0x94ef338
        ev_realloc_emul: ptr (nil), size 0, return 0x94ef348
        ev_realloc_emul: ptr (nil), size 0, return 0x94ef358
        ev_realloc_emul: ptr (nil), size 0, return 0x94ef368
        ev_realloc_emul: ptr (nil), size 0, return 0x94ef378
        ev_realloc_emul: ptr (nil), size 0, return 0x94ef388
        ev_realloc_emul: ptr (nil), size 0, return 0x94ef398
        ev_realloc_emul: ptr (nil), size 0, return 0x94ef3a8
        ev_realloc_emul: ptr (nil), size 0, return 0x94ef3b8
        ev_realloc_emul: ptr (nil), size 0, return 0x94ef3c8
        ev_realloc_emul: ptr (nil), size 0, return 0x94ef3d8
        ev_realloc_emul: ptr (nil), size 0, return 0x94ef3e8
        ev_realloc_emul: ptr (nil), size 0, return 0x94ef3f8
        ev_realloc_emul: ptr (nil), size 0, return 0x94ef408
        ev_realloc_emul: ptr (nil), size 0, return 0x94ef418
        ev_realloc_emul: ptr (nil), size 0, return 0x94ef428
        ev_realloc_emul: ptr (nil), size 0, return 0x94ef438
        ev_realloc_emul: ptr (nil), size 0, return 0x94ef448
        ev_realloc_emul: ptr 0x94ef008, size 0, return (nil)


----------
ylfroy

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.schmorp.de/pipermail/libev/attachments/20120620/ed120b31/attachment.html>


More information about the libev mailing list