On Thu, Jul 26, 2012 at 9:14 AM, Geoffrey Irving <irv...@naml.us> wrote:
> I'm using valgrind 3.8.0.SVN from MacPorts on Mac OS X 10.7.4.
> valgrind is showing a memory leak of 18 bytes when I try to allocate
> an empty array using posix_memalign.  The leak warning is attached.
>
> The posix_memalign call is
>
>     void* data;
>     const size_t alignment = max(sizeof(T),sizeof(void*));
>     if (posix_memalign(&data,alignment,sizeof(T)*size))
>         throw bad_alloc();
>
> where sizeof(T)==64 and size==0.  I've confirmed that I call free on
> the same pointer.  If I replace the code with
>
>     void* data = malloc(sizeof(T)*size);
>
> the leak warning goes away.  Any idea what could be going wrong?

Okay, this looks like a bug in Mac's posix_memalign, not a valgrind
issue.  Here's a reduced test program:

    #include <stdlib.h>
    #include <stdio.h>

    int main() {
      void* data = 0;
      int r = posix_memalign(&data,64,0);
      printf("r = %d, data = %p\n",r,data);
      free(data);
      return 0;
    }

Running this produces

    tile:scratch% ./memalign
    r = 0, data = 0x106900840
    memalign(3949) malloc: *** error for object 0x106900840: pointer
being freed was not allocated
    *** set a breakpoint in malloc_error_break to debug
    Abort trap: 6

Allocating 64 bytes instead of 0 works fine.  I'll tell Apple about it
and add the !size check to my code.

Thanks,
Geoffrey

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Valgrind-users mailing list
Valgrind-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/valgrind-users

Reply via email to