Possible memory leak in SDL2-Image?

No answers? Any ideas how to fix it?

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 :> No answers? Any ideas how to fix it?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

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.

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 :> 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
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

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.

Check the library dependencies of your SDL2_image with

$ readelf -d | 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 :> 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
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Oops, that should have been

$ readelf -d | grep NEEDED

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

$ readelf -d | 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 :

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
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org