The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Possible memory leak in SDL2-Image?
Alex


Joined: 19 Sep 2014
Posts: 35
I made very simple test program, like this:
Code:
#include <stdio.h>
#include "SDL.h"
#include "SDL_image.h"
int main(void)
{
    SDL_Surface *surface = IMG_Load("testimage.png");
    if (surface == NULL){
        printf("Error: %s\n", SDL_GetError());
        return 1;
    }
    else
        printf("Image loaded, no errors\n");
    SDL_FreeSurface(surface);
    return 0;
}

Tested it with valgrind. Valgrind log:
Code:
==5666== Conditional jump or move depends on uninitialised value(s)
==5666==    at 0x8CA34E0: inflateReset2 (in /lib/x86_64-linux-gnu/libz.so.1.2.3.4)
==5666==    by 0x8CA35D8: inflateInit2_ (in /lib/x86_64-linux-gnu/libz.so.1.2.3.4)
==5666==    by 0x78F70D3: png_create_read_struct_2 (in /lib/x86_64-linux-gnu/libpng12.so.0.46.0)
==5666==    by 0x78F7296: png_create_read_struct (in /lib/x86_64-linux-gnu/libpng12.so.0.46.0)
==5666==    by 0x5139297: IMG_LoadPNG_RW (in /usr/lib/x86_64-linux-gnu/libSDL2_image-2.0.so.0.0.0)
==5666==    by 0x5132F8B: IMG_LoadTyped_RW (in /usr/lib/x86_64-linux-gnu/libSDL2_image-2.0.so.0.0.0)
==5666==    by 0x400755: main (main.c:7)
==5666==
Image loaded, no errors
==5666==
==5666== HEAP SUMMARY:
==5666==     in use at exit: 4,096 bytes in 1 blocks
==5666==   total heap usage: 16 allocs, 15 frees, 1,112,267 bytes allocated
==5666==
==5666== LEAK SUMMARY:
==5666==    definitely lost: 0 bytes in 0 blocks
==5666==    indirectly lost: 0 bytes in 0 blocks
==5666==      possibly lost: 0 bytes in 0 blocks
==5666==    still reachable: 4,096 bytes in 1 blocks
==5666==         suppressed: 0 bytes in 0 blocks

As you can see 4096 bytes in 1 block still reachable. Is it OK? I think my test program is too simple to make this leak. I think this leak is from SDL2 Image library.
PS: Tested on Ubuntu Linux 12.04 64-bit, SDL 2.0.3, SDL Image 2.0.0.
No answers?
Alex


Joined: 19 Sep 2014
Posts: 35
No answers? Any ideas how to fix it?
Possible memory leak in SDL2-Image?
Jonas Kulla
Guest

If you run valgrind memcheck with the additional parameters "--leak-check=full --show-reachable=yes",

it will show you exactly where these blocks have been allocated. And as the name

"still reachable" implies, they aren't lost or leaked by the lib, just likely stored in

global variables. For me, all of them originated in dlopen, so it's very likely just the

image conversion libraries (libpng, libjpeg) that SDL2_image dlopens at init time and
keeps around until either shutdown or IMG_Quit() (which you forgot to call btw.).

With IMG_Quit() at the end, the amount of unfreed memory reported by valgrind
shrinks to 32 bytes on my end.



2014-10-28 13:10 GMT+01:00 Alex:
Quote:
No answers? Any ideas how to fix it?


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Alex


Joined: 19 Sep 2014
Posts: 35
Thanks for answer.

And thank you for remind me about IMG_Quit() function.

But IMG_Quit() is not helpful in my case. This is the valgrind log after add IMG_Quit() (and IMG_Init too) and with "--leak-check=full --show-reachable=yes" :
Code:
==3645== Conditional jump or move depends on uninitialised value(s)
==3645==    at 0x8CA34E0: inflateReset2 (in /lib/x86_64-linux-gnu/libz.so.1.2.3.4)
==3645==    by 0x8CA35D8: inflateInit2_ (in /lib/x86_64-linux-gnu/libz.so.1.2.3.4)
==3645==    by 0x78F70D3: png_create_read_struct_2 (in /lib/x86_64-linux-gnu/libpng12.so.0.46.0)
==3645==    by 0x78F7296: png_create_read_struct (in /lib/x86_64-linux-gnu/libpng12.so.0.46.0)
==3645==    by 0x5139297: IMG_LoadPNG_RW (in /usr/lib/x86_64-linux-gnu/libSDL2_image-2.0.so.0.0.0)
==3645==    by 0x5132F8B: IMG_LoadTyped_RW (in /usr/lib/x86_64-linux-gnu/libSDL2_image-2.0.so.0.0.0)
==3645==    by 0x4007EF: main (main.c:9)
==3645==
Image loaded, no errors
==3645==
==3645== HEAP SUMMARY:
==3645==     in use at exit: 4,096 bytes in 1 blocks
==3645==   total heap usage: 16 allocs, 15 frees, 1,112,267 bytes allocated
==3645==
==3645== 4,096 bytes in 1 blocks are still reachable in loss record 1 of 1
==3645==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3645==    by 0x7B120B7: libjpeg_general_init (in /usr/lib/x86_64-linux-gnu/libjpeg.so.8.0.2)
==3645==    by 0x400F305: call_init.part.0 (dl-init.c:85)
==3645==    by 0x400F3DE: _dl_init (dl-init.c:52)
==3645==    by 0x40016E9: ??? (in /lib/x86_64-linux-gnu/ld-2.15.so)
==3645==
==3645== LEAK SUMMARY:
==3645==    definitely lost: 0 bytes in 0 blocks
==3645==    indirectly lost: 0 bytes in 0 blocks
==3645==      possibly lost: 0 bytes in 0 blocks
==3645==    still reachable: 4,096 bytes in 1 blocks
==3645==         suppressed: 0 bytes in 0 blocks
==3645==
==3645== For counts of detected and suppressed errors, rerun with: -v
==3645== Use --track-origins=yes to see where uninitialised values come from
==3645== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)

As you can see 4096 bytes in 1 block still reachable with IMG_Quit() function too. I try to make my program free from any leaks like this.
Possible memory leak in SDL2-Image?
Jonas Kulla
Guest

As you can see in the valgrind log, the allocation originates from libjpeg, so
it is neither an issue with your app nor SDL2_image. If this output bothers you,
you can either fix libjpeg, or add a valgrind surpression.


2014-10-28 14:13 GMT+01:00 Alex:
Quote:
Thanks for answer.

And thank you for remind me about IMG_Quit() function.

But IMG_Quit() is not helpful in my case. This is the valgrind log after add IMG_Quit() (and IMG_Init too) and with "--leak-check=full --show-reachable=yes" :



Code:

==3645== Conditional jump or move depends on uninitialised value(s)
==3645==    at 0x8CA34E0: inflateReset2 (in /lib/x86_64-linux-gnu/libz.so.1.2.3.4)
==3645==    by 0x8CA35D8: inflateInit2_ (in /lib/x86_64-linux-gnu/libz.so.1.2.3.4)
==3645==    by 0x78F70D3: png_create_read_struct_2 (in /lib/x86_64-linux-gnu/libpng12.so.0.46.0)
==3645==    by 0x78F7296: png_create_read_struct (in /lib/x86_64-linux-gnu/libpng12.so.0.46.0)
==3645==    by 0x5139297: IMG_LoadPNG_RW (in /usr/lib/x86_64-linux-gnu/libSDL2_image-2.0.so.0.0.0)
==3645==    by 0x5132F8B: IMG_LoadTyped_RW (in /usr/lib/x86_64-linux-gnu/libSDL2_image-2.0.so.0.0.0)
==3645==    by 0x4007EF: main (main.c:9)
==3645==
Image loaded, no errors
==3645==
==3645== HEAP SUMMARY:
==3645==     in use at exit: 4,096 bytes in 1 blocks
==3645==   total heap usage: 16 allocs, 15 frees, 1,112,267 bytes allocated
==3645==
==3645== 4,096 bytes in 1 blocks are still reachable in loss record 1 of 1
==3645==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3645==    by 0x7B120B7: libjpeg_general_init (in /usr/lib/x86_64-linux-gnu/libjpeg.so.8.0.2)
==3645==    by 0x400F305: call_init.part.0 (dl-init.c:85)
==3645==    by 0x400F3DE: _dl_init (dl-init.c:52)
==3645==    by 0x40016E9: ??? (in /lib/x86_64-linux-gnu/ld-2.15.so)
==3645==
==3645== LEAK SUMMARY:
==3645==    definitely lost: 0 bytes in 0 blocks
==3645==    indirectly lost: 0 bytes in 0 blocks
==3645==      possibly lost: 0 bytes in 0 blocks
==3645==    still reachable: 4,096 bytes in 1 blocks
==3645==         suppressed: 0 bytes in 0 blocks
==3645==
==3645== For counts of detected and suppressed errors, rerun with: -v
==3645== Use --track-origins=yes to see where uninitialised values come from
==3645== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)



As you can see 4096 bytes in 1 block still reachable with IMG_Quit() function too. I try to make my program free from any leaks like this.


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Alex


Joined: 19 Sep 2014
Posts: 35
Thanks for information.
I just don't understand why this program used libjpeg if I never use jpeg images. And more: I made init with IMG_Init(IMG_INIT_PNG), to avoid using of libjpeg. But this is not helpful to solve this problem.
Possible memory leak in SDL2-Image?
Jonas Kulla
Guest

Check the library dependencies of your SDL2_image with


$ readelf -d <your SDL2_image.so> | grep needed


you will very likely find a libjpeg in there. If you check the callstack in your

valgrind log, you see that the libjpeg init function responsible for the malloc
is called by ld.so, ie. the dynamic linker that links all shared libraries at executable
startup time, so this is very likely a "library constructor" that is called even
before your main function. If you do not want the libjpeg dependency in your
application, build SDL2_image with jpeg support disabled.



2014-10-28 16:52 GMT+01:00 Alex:
Quote:
Thanks for information.
I just don't understand why this program used libjpeg if I never use jpeg images. And more: I made init with IMG_Init(IMG_INIT_PNG), to avoid using of libjpeg. But this is not helpful to solve this problem.


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Possible memory leak in SDL2-Image?
Jonas Kulla
Guest

Oops, that should have been

$ readelf -d <your SDL2_image.so> | grep NEEDED


2014-10-28 17:03 GMT+01:00 Jonas Kulla:
Quote:
Check the library dependencies of your SDL2_image with


$ readelf -d <your SDL2_image.so> | grep needed


you will very likely find a libjpeg in there. If you check the callstack in your

valgrind log, you see that the libjpeg init function responsible for the malloc
is called by ld.so, ie. the dynamic linker that links all shared libraries at executable
startup time, so this is very likely a "library constructor" that is called even
before your main function. If you do not want the libjpeg dependency in your
application, build SDL2_image with jpeg support disabled.



2014-10-28 16:52 GMT+01:00 Alex:
Quote:
Thanks for information.
I just don't understand why this program used libjpeg if I never use jpeg images. And more: I made init with IMG_Init(IMG_INIT_PNG), to avoid using of libjpeg. But this is not helpful to solve this problem.


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org