I am wrapping a little bit of poppler just aiming to extract some images from PDF's.
I think I am having some issues with memory management in this example.
The issue is actually here. I think that the issue is that when I yield the pointer it goes out of scope. It is funny that when I echo (see this line), it works.
How should I properlly fix this situations. Using maybe GC-ref? (I have never used it)
I am not that familiar with valgrind (I come from python). I did:
$ valgrind --leak-check=yes ./ex04_extract_images_with_sugar
==364593== Memcheck, a memory error detector
==364593== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==364593== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
==364593== Command: ./ex04_extract_images_with_sugar
==364593==
Number of pages: 25
==364593== Invalid read of size 8
==364593== at 0x1146AC: nimIncRefCyclic (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x114EE7: eqcopy___OOZsrcZlibZpage_138 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x11F9D5: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120108: main (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== Address 0x6ce7580 is 16 bytes before a block of size 40 alloc'd
==364593== at 0x4841888: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==364593== by 0x49B8329: g_malloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x49DAF17: g_slice_alloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x48C12B2: poppler_image_mapping_new (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x48C1348: poppler_page_get_image_mapping (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x11F2C6: getImageMapping__OOZsrcZlibZpage_27 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x11F997: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120108: main (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593==
==364593== Invalid write of size 8
==364593== at 0x1146E2: nimIncRefCyclic (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x114EE7: eqcopy___OOZsrcZlibZpage_138 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x11F9D5: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120108: main (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== Address 0x6ce7580 is 16 bytes before a block of size 40 alloc'd
==364593== at 0x4841888: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==364593== by 0x49B8329: g_malloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x49DAF17: g_slice_alloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x48C12B2: poppler_image_mapping_new (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x48C1348: poppler_page_get_image_mapping (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x11F2C6: getImageMapping__OOZsrcZlibZpage_27 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x11F997: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120108: main (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593==
==364593== Conditional jump or move depends on uninitialised value(s)
==364593== at 0x48E07D4: ??? (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x500BA4B: Gfx::doImage(Object*, Stream*, bool) (in /usr/lib/libpoppler.so.125.0.0)
==364593== by 0x500CA14: Gfx::opXObject(Object*, int) (in /usr/lib/libpoppler.so.125.0.0)
==364593== by 0x4FF777E: Gfx::go(bool) (in /usr/lib/libpoppler.so.125.0.0)
==364593== by 0x4FF8226: Gfx::display(Object*, bool) (in /usr/lib/libpoppler.so.125.0.0)
==364593== by 0x5063799: Page::display(Gfx*) (in /usr/lib/libpoppler.so.125.0.0)
==364593== by 0x48BDEC2: ??? (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x48C0631: poppler_page_get_image (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x11FBF2: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593==
==364593== Conditional jump or move depends on uninitialised value(s)
==364593== at 0x48C063B: poppler_page_get_image (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x11FBF2: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120108: main (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593==
==364593== Use of uninitialised value of size 8
==364593== at 0x48C0644: poppler_page_get_image (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x11FBF2: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120108: main (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593==
ptr Surface()
==364593== Invalid read of size 8
==364593== at 0x1147FA: nimDecRefIsLastCyclicStatic (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x114F0F: eqcopy___OOZsrcZlibZpage_138 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x11FE8F: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120108: main (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== Address 0x6ce7580 is 16 bytes before a block of size 40 alloc'd
==364593== at 0x4841888: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==364593== by 0x49B8329: g_malloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x49DAF17: g_slice_alloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x48C12B2: poppler_image_mapping_new (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x48C1348: poppler_page_get_image_mapping (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x11F2C6: getImageMapping__OOZsrcZlibZpage_27 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x11F997: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120108: main (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593==
==364593== Invalid read of size 8
==364593== at 0x114834: nimDecRefIsLastCyclicStatic (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x114F0F: eqcopy___OOZsrcZlibZpage_138 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x11FE8F: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120108: main (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== Address 0x6ce7580 is 16 bytes before a block of size 40 alloc'd
==364593== at 0x4841888: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==364593== by 0x49B8329: g_malloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x49DAF17: g_slice_alloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x48C12B2: poppler_image_mapping_new (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x48C1348: poppler_page_get_image_mapping (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x11F2C6: getImageMapping__OOZsrcZlibZpage_27 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x11F997: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120108: main (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593==
==364593== Invalid write of size 8
==364593== at 0x11486A: nimDecRefIsLastCyclicStatic (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x114F0F: eqcopy___OOZsrcZlibZpage_138 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x11FE8F: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120108: main (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== Address 0x6ce7580 is 16 bytes before a block of size 40 alloc'd
==364593== at 0x4841888: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==364593== by 0x49B8329: g_malloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x49DAF17: g_slice_alloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x48C12B2: poppler_image_mapping_new (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x48C1348: poppler_page_get_image_mapping (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x11F2C6: getImageMapping__OOZsrcZlibZpage_27 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x11F997: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120108: main (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593==
==364593== Invalid read of size 8
==364593== at 0x10E798: rememberCycle__system_3164 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x114895: nimDecRefIsLastCyclicStatic (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x114F0F: eqcopy___OOZsrcZlibZpage_138 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x11FE8F: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120108: main (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== Address 0x6ce7588 is 8 bytes before a block of size 40 alloc'd
==364593== at 0x4841888: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==364593== by 0x49B8329: g_malloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x49DAF17: g_slice_alloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x48C12B2: poppler_image_mapping_new (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x48C1348: poppler_page_get_image_mapping (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x11F2C6: getImageMapping__OOZsrcZlibZpage_27 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x11F997: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120108: main (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593==
==364593== Invalid read of size 8
==364593== at 0x10E7FC: rememberCycle__system_3164 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x114895: nimDecRefIsLastCyclicStatic (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x114F0F: eqcopy___OOZsrcZlibZpage_138 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x11FE8F: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120108: main (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== Address 0x6ce7580 is 16 bytes before a block of size 40 alloc'd
==364593== at 0x4841888: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==364593== by 0x49B8329: g_malloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x49DAF17: g_slice_alloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x48C12B2: poppler_image_mapping_new (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x48C1348: poppler_page_get_image_mapping (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x11F2C6: getImageMapping__OOZsrcZlibZpage_27 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x11F997: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120108: main (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593==
==364593== Invalid write of size 8
==364593== at 0x10E80A: rememberCycle__system_3164 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x114895: nimDecRefIsLastCyclicStatic (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x114F0F: eqcopy___OOZsrcZlibZpage_138 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x11FE8F: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120108: main (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== Address 0x6ce7580 is 16 bytes before a block of size 40 alloc'd
==364593== at 0x4841888: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==364593== by 0x49B8329: g_malloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x49DAF17: g_slice_alloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x48C12B2: poppler_image_mapping_new (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x48C1348: poppler_page_get_image_mapping (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x11F2C6: getImageMapping__OOZsrcZlibZpage_27 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x11F997: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120108: main (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593==
==364593== Invalid write of size 8
==364593== at 0x10E5E6: registerCycle__system_3124 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x10E832: rememberCycle__system_3164 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x114895: nimDecRefIsLastCyclicStatic (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x114F0F: eqcopy___OOZsrcZlibZpage_138 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x11FE8F: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120108: main (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== Address 0x6ce7588 is 8 bytes before a block of size 40 alloc'd
==364593== at 0x4841888: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==364593== by 0x49B8329: g_malloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x49DAF17: g_slice_alloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x48C12B2: poppler_image_mapping_new (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x48C1348: poppler_page_get_image_mapping (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x11F2C6: getImageMapping__OOZsrcZlibZpage_27 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x11F997: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120108: main (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593==
==364593==
==364593== HEAP SUMMARY:
==364593== in use at exit: 132,624,255 bytes in 574 blocks
==364593== total heap usage: 9,142 allocs, 8,568 frees, 268,907,023 bytes allocated
==364593==
==364593== 64 (24 direct, 40 indirect) bytes in 1 blocks are definitely lost in loss record 274 of 501
==364593== at 0x4841888: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==364593== by 0x49B8329: g_malloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x49DAF17: g_slice_alloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x49AF30A: g_list_prepend (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x48C13BC: poppler_page_get_image_mapping (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x11F2C6: getImageMapping__OOZsrcZlibZpage_27 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x11F997: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200BD: NimMainInner (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x1200CE: NimMain (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120108: main (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593==
==364593== 656 (392 direct, 264 indirect) bytes in 1 blocks are definitely lost in loss record 482 of 501
==364593== at 0x4841888: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==364593== by 0x4AD4BD5: _cairo_image_surface_create_for_pixman_image (cairo-image-surface.c:189)
==364593== by 0x4AD5286: UnknownInlinedFun (cairo-image-surface.c:365)
==364593== by 0x4AD5286: UnknownInlinedFun (cairo-image-surface.c:345)
==364593== by 0x4AD5286: cairo_image_surface_create (cairo-image-surface.c:411)
==364593== by 0x48E080C: ??? (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x500BA4B: Gfx::doImage(Object*, Stream*, bool) (in /usr/lib/libpoppler.so.125.0.0)
==364593== by 0x500CA14: Gfx::opXObject(Object*, int) (in /usr/lib/libpoppler.so.125.0.0)
==364593== by 0x4FF777E: Gfx::go(bool) (in /usr/lib/libpoppler.so.125.0.0)
==364593== by 0x4FF8226: Gfx::display(Object*, bool) (in /usr/lib/libpoppler.so.125.0.0)
==364593== by 0x5063799: Page::display(Gfx*) (in /usr/lib/libpoppler.so.125.0.0)
==364593== by 0x48BDEC2: ??? (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x48C0631: poppler_page_get_image (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x11FBF2: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593==
==364593== 47,772 (56 direct, 47,716 indirect) bytes in 1 blocks are definitely lost in loss record 500 of 501
==364593== at 0x4841888: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==364593== by 0x49B8329: g_malloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x49DAF17: g_slice_alloc (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x49DB56D: g_slice_alloc0 (in /usr/lib/libglib-2.0.so.0.7400.1)
==364593== by 0x49416FE: g_type_create_instance (in /usr/lib/libgobject-2.0.so.0.7400.1)
==364593== by 0x4928560: ??? (in /usr/lib/libgobject-2.0.so.0.7400.1)
==364593== by 0x4929DBF: g_object_new_with_properties (in /usr/lib/libgobject-2.0.so.0.7400.1)
==364593== by 0x492ABB9: g_object_new (in /usr/lib/libgobject-2.0.so.0.7400.1)
==364593== by 0x48B92AC: poppler_document_get_page (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x11F1AA: getPage__ex485295extract95images95with95sugar_9 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x11F8F7: main__ex485295extract95images95with95sugar_5 (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593== by 0x120179: NimMainModule (in /home/jose/src/nimlang/poppler_glib.nim/examples/ex04_extract_images_with_sugar)
==364593==
==364593== 132,483,948 bytes in 1 blocks are possibly lost in loss record 501 of 501
==364593== at 0x4846A73: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==364593== by 0x5A5ABCA: UnknownInlinedFun (pixman-bits-image.c:1273)
==364593== by 0x5A5ABCA: _pixman_bits_image_init (pixman-bits-image.c:1296)
==364593== by 0x5A5ACF5: create_bits_image_internal (pixman-bits-image.c:1349)
==364593== by 0x4AD5274: UnknownInlinedFun (cairo-image-surface.c:359)
==364593== by 0x4AD5274: UnknownInlinedFun (cairo-image-surface.c:345)
==364593== by 0x4AD5274: cairo_image_surface_create (cairo-image-surface.c:411)
==364593== by 0x48E080C: ??? (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x500BA4B: Gfx::doImage(Object*, Stream*, bool) (in /usr/lib/libpoppler.so.125.0.0)
==364593== by 0x500CA14: Gfx::opXObject(Object*, int) (in /usr/lib/libpoppler.so.125.0.0)
==364593== by 0x4FF777E: Gfx::go(bool) (in /usr/lib/libpoppler.so.125.0.0)
==364593== by 0x4FF8226: Gfx::display(Object*, bool) (in /usr/lib/libpoppler.so.125.0.0)
==364593== by 0x5063799: Page::display(Gfx*) (in /usr/lib/libpoppler.so.125.0.0)
==364593== by 0x48BDEC2: ??? (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593== by 0x48C0631: poppler_page_get_image (in /usr/lib/libpoppler-glib.so.8.24.0)
==364593==
==364593== LEAK SUMMARY:
==364593== definitely lost: 472 bytes in 3 blocks
==364593== indirectly lost: 48,020 bytes in 58 blocks
==364593== possibly lost: 132,483,948 bytes in 1 blocks
==364593== still reachable: 85,711 bytes in 463 blocks
==364593== suppressed: 0 bytes in 0 blocks
==364593== Reachable blocks (those to which a pointer was found) are not shown.
==364593== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==364593==
==364593== Use --track-origins=yes to see where uninitialised values come from
==364593== For lists of detected and suppressed errors, rerun with: -s
==364593== ERROR SUMMARY: 16 errors from 16 contexts (suppressed: 0 from 0)
which shows that I have many things wrong. But I don't know what to do with this to be honest.
What exactly was the problem that you originally had? From the valgrind output you showed, it seems like ex04_extract_images_with_sugar.nim runs, except it leaks memory at the end.
Generally, when you're working with C (or some C++) code, you need to take care of memory resources yourself. That means, sometimes a function returns a pointer to some object, and then it's your responsibility to make sure that the memory at that pointer gets freed once your done with it. I assume that for your example here, a function like poppler_page_get_image_mapping gives you such a pointer, and because you are not freeing it at the end, you get all the memory leaks (e.g. 64 (24 direct, 40 indirect) bytes in 1 blocks are definitely lost in loss record 274 of 501).
However, while memory leaks are not really a good thing, a program with memory leaks still should at first sight appear to run normally.
I am trying to extract all the images in a page from a PDF. I was trying to make a nicer API by doing an iterator over all the images (they happen to be pointers to Cairo's surfaces). When I yield the pointer from the iterator it fails. It is not failing in my example above because I am echoing within the iterator. I understand that this is increasing the lifetime of the pointer.
So as a short time objective would be: what should I do in order to yield the pointer from the iterator without depending on echoing the pointer from within the iterator.
I understand that this is increasing the lifetime of the pointer.
No, that's just a side effect of you not managing the memory properly. From the valgrind output it looks like either that you cast the result of poppler_image_mapping_new to a ref which is wrong or that you declared its return type to be ref which is equally wrong.
I noticed that you recently forked gintro. Well, it had no poppler support yet, but I guess you tried to add it and maybe wondered why the poppler module failed to compile. The reason seems to be, that some proc parameters are named cairo, same as the module, like in
proc renderForPrinting*(self: Page; cairo: cairo.Context) =
poppler_page_render_for_printing(cast[ptr Page00](self.impl), cast[ptr cairo.Context00](cairo.impl))
Well, poppler is not easy, I never used it, and I guess no other Nim users may need it. So I hope that you will not need gintro support. But when you should fail with your own attempts, we may consider adding poppler to gintro. I have just fixed gintro for latest Arch Linux, as Jason B. asked for it recently.
Yes, it is not impossible. I did a short test with https://www.ti.com/lit/ds/symlink/lm741.pdf
import gintro/[poppler, cairo]
import std/[uri, strformat]
proc main =
# Load the PDF document
#var fname = parseUri("file:///home/jose/Descargas/Escrituras Espartales.pdf")
#var password = ""
var doc = newDocumentFromFile("file:///tmp/hhh/lm741.pdf", nil)
echo "Number of pages: ", doc.nPages() # Get the number of pages in the document
var page: Page = doc.getPage(2)
var imseq = getImageMapping(page)
echo imseq.len
var surface = getImage(page, 0)
echo surface != nil
let status = cairo.writeToPng(surface, "test.png")
echo status
#var n = 1
#for img in page.images:
# echo repr img
# img.toPng(fmt"page_{n:.2d}.png")
# n += 1
main()
And indeed I get a PNG picture, the rectangular shaped, top right from page 3. But the bindings would need some manual tuning. And I think I should really not do your homework for you. And extracting images from PDF documents have always the taste of stealing for me, similar as unauthorized github forks :-)
@Araq That makes sense. I had to define the following with ref because next/prev are GList:
type
# List management
GList* {.importc.} = ref object
data* {.importc.} : Gpointer
next* {.importc.} : GList
prev* {.importc.} : GList
I am looking into how to make it an object.
@Stefan_Salewski First, thank you very much for taking the time for getting a working example of poppler within gintro. It is much appreciated.
Regarging your last comment, you got it very wrong to be frank:
[...] But as the number of serious gintro users is tiny, we may fully remove gintro from GitHub by end of 2022. [...]
It needs to be:
type
# List management
GList* {.importc.} = ptr object
data* {.importc.} : Gpointer
next* {.importc.} : GList
prev* {.importc.} : GList
About the forking: I also fork packages that I fear will disappear otherwise.
StefanSalewski/gintro is licensed under the
MIT License A short and simple permissive license with conditions only requiring preservation of copyright and license notices. Licensed works, modifications, and larger works may be distributed under different terms and without source code.
Ianal but I believe you could've technically used any license you wanted, since you're just dynamically linking against LGPL libraries. And you chose this one.
Accusing people of stealing is serious, dude, that's not cool.
For forking repositories: I would never do that myself without asking the original author for permission, but I guess that GitHub allows it, and it is no real problem for me.
As @shirleyquick pointed out you used a license that explicitly allows this. This is afterall what free software allows, actual ownership of code.
It's not nearly as frustrating as having to read your ramblings.
Now now you know what they say "If one does not have something nice to say they should not say anything". Could always be worse, we could start posting code snippets of the compiler. :D
As @shirleyquick pointed out you used a license that explicitly allows this. This is afterall what free software allows, actual ownership of code.
Yes, I have to agree. GitHub seems to allow forking for all public projects, independently which license the project actually is using. I think the idea of forking was initially to allow others people to contribute, not just to copy.
https://github.com/isaacs/github/issues/1280
Today the kids copy all forms of digital contents, without respecting the work of the original authors in any way. Of course the benefit is, that information is freely available for all people, which is very positive.
I am wrapping a little bit of poppler just aiming to extract some images from PDF's.
Unfortunately Google was not able to find your valid email or postal address.
I have added poppler to gintro.
You may test with
nimble uninstall gintro
nimble install gintro@#head
At least this tiny test program seems to work:
import gintro/[poppler, cairo]
proc main =
var doc = newDocumentFromFile("file:///home/salewski/hhh9/lm741.pdf", nil)
echo "Number of pages: ", doc.nPages()
var page: Page = doc.getPage(2)
var imseq = getImageMapping(page)
echo imseq.len
for i in imseq:
echo i.imageId
echo i.area
var surface = getImage(page, 0)
let status = cairo.writeToPng(surface, "test.png")
echo status
main()
nim c t.nim
Hint: mm: orc; threads: on; opt: none (DEBUG BUILD, `-d:release` generates faster code)
105225 lines; 1.142s; 168.074MiB peakmem; proj: /home/salewski/hhh9/t.nim; out: /home/salewski/hhh9/t [SuccessX]
salewski@hx90 ~/hhh9 $ ./t
Number of pages: 17
2
1
(x1: 101.5, y1: 136.8, x2: 246.5, y2: 224.8)
0
(x1: 348.8, y1: 136.96, x2: 526.8, y2: 227.96)
success
salewski@hx90 ~/hhh9 $ nim c -d:gintroDebug t.nim
salewski@hx90 ~/hhh9 $ ./t
Number of pages: 17
2
1
(x1: 101.5, y1: 136.8, x2: 246.5, y2: 224.8)
0
(x1: 348.8, y1: 136.96, x2: 526.8, y2: 227.96)
success
destroy Surface:ObjectType 140070177714368
destroy ImageMapping:ObjectType 140070177706080
destroy ImageMapping:ObjectType 140070177706144
destroy Page:ObjectType 140070177714320
destroy Document:ObjectType 140070177714272
For poppler.Rectangle and poppler.ImageMapping we still use proxy objects -- I have provided getter procs to access the fields, see end of poppler.nim file. I have not the time and motivation to examine poppler in much detail, there may exist more data types with fields, for which you may have to create getter procs. For that, we would have to generate the fields first, which needs a modification of the gen.nim generator script. Search for poppler.Rectangle to find were fields are generated. You should know enough Nim to manage that yourself, you can find in the issue tracker instructions how you can run gen.nim inside of a local directory. Libraries like libnice, gstreamer and poppler are difficult for bindings, as the gobject-introspection is a late add-on for these. Native GObject libs generally avoid use of objects with fields or provide getter and setter functions by gobject-introspection, which manual intervention unnecessary.
Well, enough "ramblings" from me for today. At least I have learned a new word, German translation seems to be "Geschwafel", which is not a very friendly term.
When you should need more support, use the gintro issue tracker or email, as I will not continue reading this thread.