Help with a port of a C++ demo


Farox

Certified Guru
Joined
Jan 8, 2009
Messages
2,413
Age
56
Location
Italy
Website
rbnet.it
Hi
i have found another demo sources to port, but i have problem running it.
After compilation/linking goes fine when i run have:
*** glibc detected *** ./yourination: munmap_chunk(): invalid pointer: 0x000a5238 ***
Aborted

i tried running with gdb but it seems stuck with an initial image...and nothing more (i have waited for about 30 min) the only option is kill the program pressing the Pandora button...and when the prog is closed no backtrace is availble.

Searching on the web i found some people suggesting the use of Valgrind, but seems that it needs some debug enabled library that (i think) is not present on Codeblocks PND.

Next i tried building and running on my Linux x86 machine, after installing Valgrind i have:

valgrind --tool=memcheck ./yurination
==2295== Memcheck, a memory error detector
==2295== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==2295== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==2295== Command: ./yurination
==2295==
==2295== Invalid write of size 8
==2295== at 0x4C2F793: memcpy@@GLIBC_2.14 (vg_replace_strmem.c:915)
==2295== by 0x40B1F4: memcpy (string3.h:51)
==2295== by 0x40B1F4: loadJPG(char*, int*, int*) (jpeg.cpp:42)
==2295== by 0x40AA11: UploadJPEG (ImageWrapper.cpp:41)
==2295== by 0x40AA11: ImageWrapper::SetTexture(char*) (ImageWrapper.cpp:88)
==2295== by 0x4070FE: Init() (Frame.cpp:935)
==2295== by 0x40B08B: piirra() (main.cpp:74)
==2295== by 0x5358243: fghcbDisplayWindow (in /usr/lib/x86_64-linux-gnu/libglut.so.3.9.0)
==2295== by 0x535BAA8: fgEnumWindows (in /usr/lib/x86_64-linux-gnu/libglut.so.3.9.0)
==2295== by 0x53587FC: glutMainLoopEvent (in /usr/lib/x86_64-linux-gnu/libglut.so.3.9.0)
==2295== by 0x5358FFC: glutMainLoop (in /usr/lib/x86_64-linux-gnu/libglut.so.3.9.0)
==2295== by 0x4020B2: main (main.cpp:67)
==2295== Address 0xe89c040 is 0 bytes after a block of size 786,432 alloc'd
==2295== at 0x4C2B800: operator new[](unsigned long) (vg_replace_malloc.c:389)
==2295== by 0x40B1A0: loadJPG(char*, int*, int*) (jpeg.cpp:35)
==2295== by 0x40AA11: UploadJPEG (ImageWrapper.cpp:41)
==2295== by 0x40AA11: ImageWrapper::SetTexture(char*) (ImageWrapper.cpp:88)
==2295== by 0x4070FE: Init() (Frame.cpp:935)
==2295== by 0x40B08B: piirra() (main.cpp:74)
==2295== by 0x5358243: fghcbDisplayWindow (in /usr/lib/x86_64-linux-gnu/libglut.so.3.9.0)
==2295== by 0x535BAA8: fgEnumWindows (in /usr/lib/x86_64-linux-gnu/libglut.so.3.9.0)
==2295== by 0x53587FC: glutMainLoopEvent (in /usr/lib/x86_64-linux-gnu/libglut.so.3.9.0)
==2295== by 0x5358FFC: glutMainLoop (in /usr/lib/x86_64-linux-gnu/libglut.so.3.9.0)
==2295== by 0x4020B2: main (main.cpp:67)
==2295==
==2295== Invalid read of size 8
==2295== at 0x559C993: ??? (in /usr/lib/x86_64-linux-gnu/libjpeg.so.62.0.0)
==2295== by 0x5589BF4: jpeg_abort (in /usr/lib/x86_64-linux-gnu/libjpeg.so.62.0.0)
==2295== by 0x558E681: jpeg_finish_decompress (in /usr/lib/x86_64-linux-gnu/libjpeg.so.62.0.0)
==2295== by 0x40B211: loadJPG(char*, int*, int*) (jpeg.cpp:45)
==2295== by 0x40AA11: UploadJPEG (ImageWrapper.cpp:41)
==2295== by 0x40AA11: ImageWrapper::SetTexture(char*) (ImageWrapper.cpp:88)
==2295== by 0x4070FE: Init() (Frame.cpp:935)
==2295== by 0x40B08B: piirra() (main.cpp:74)
==2295== by 0x5358243: fghcbDisplayWindow (in /usr/lib/x86_64-linux-gnu/libglut.so.3.9.0)
==2295== by 0x535BAA8: fgEnumWindows (in /usr/lib/x86_64-linux-gnu/libglut.so.3.9.0)
==2295== by 0x53587FC: glutMainLoopEvent (in /usr/lib/x86_64-linux-gnu/libglut.so.3.9.0)
==2295== by 0x5358FFC: glutMainLoop (in /usr/lib/x86_64-linux-gnu/libglut.so.3.9.0)
==2295== by 0x4020B2: main (main.cpp:67)
==2295== Address 0x202020000000810 is not stack'd, malloc'd or (recently) free'd
==2295==
==2295==
==2295== Process terminating with default action of signal 11 (SIGSEGV)
==2295== General Protection Fault
==2295== at 0x559C993: ??? (in /usr/lib/x86_64-linux-gnu/libjpeg.so.62.0.0)
==2295== by 0x5589BF4: jpeg_abort (in /usr/lib/x86_64-linux-gnu/libjpeg.so.62.0.0)
==2295== by 0x558E681: jpeg_finish_decompress (in /usr/lib/x86_64-linux-gnu/libjpeg.so.62.0.0)
==2295== by 0x40B211: loadJPG(char*, int*, int*) (jpeg.cpp:45)
==2295== by 0x40AA11: UploadJPEG (ImageWrapper.cpp:41)
==2295== by 0x40AA11: ImageWrapper::SetTexture(char*) (ImageWrapper.cpp:88)
==2295== by 0x4070FE: Init() (Frame.cpp:935)
==2295== by 0x40B08B: piirra() (main.cpp:74)
==2295== by 0x5358243: fghcbDisplayWindow (in /usr/lib/x86_64-linux-gnu/libglut.so.3.9.0)
==2295== by 0x535BAA8: fgEnumWindows (in /usr/lib/x86_64-linux-gnu/libglut.so.3.9.0)
==2295== by 0x53587FC: glutMainLoopEvent (in /usr/lib/x86_64-linux-gnu/libglut.so.3.9.0)
==2295== by 0x5358FFC: glutMainLoop (in /usr/lib/x86_64-linux-gnu/libglut.so.3.9.0)
==2295== by 0x4020B2: main (main.cpp:67)
==2295==
==2295== HEAP SUMMARY:
==2295== in use at exit: 2,404,439 bytes in 4,046 blocks
==2295== total heap usage: 7,966 allocs, 3,920 frees, 3,942,934 bytes allocated
==2295==
==2295== LEAK SUMMARY:
==2295== definitely lost: 0 bytes in 0 blocks
==2295== indirectly lost: 0 bytes in 0 blocks
==2295== possibly lost: 87,647 bytes in 1,135 blocks
==2295== still reachable: 2,316,792 bytes in 2,911 blocks
==2295== suppressed: 0 bytes in 0 blocks
==2295== Rerun with --leak-check=full to see details of leaked memory
==2295==
==2295== For counts of detected and suppressed errors, rerun with: -v
==2295== ERROR SUMMARY: 9 errors from 2 contexts (suppressed: 1 from 1)
Killed

i think the culprit is loadJPG(char*, int*, int*) (jpeg.cpp:42)
and here is this function:

// A little wrapper for the jpeglib
// -Marq

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

extern "C" {
#include <jpeglib.h>
}

unsigned char *loadJPG(char *path,int *width,int *height)
{
FILE *f;
JSAMPARRAY buffer;
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
int row_stride;

if((f=fopen(path,"rb"))==NULL)
return(NULL);

cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo,f);
jpeg_read_header(&cinfo, TRUE);

*width=cinfo.image_width;
*height=cinfo.image_height;

row_stride=cinfo.image_width*3;
buffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);

unsigned char *pixels=new unsigned char[row_stride*cinfo.image_height];

jpeg_start_decompress(&cinfo);

while (cinfo.output_scanline < cinfo.image_height)
{
jpeg_read_scanlines(&cinfo, buffer, 1);
memcpy(&pixels[cinfo.output_scanline*row_stride],buffer[0],row_stride);
}

jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);

fclose(f);

return(pixels);
}

I don't know what is wrong...anyone that could help is wellcome.
 
i tried running with gdb but it seems stuck with an initial image...and nothing more (i have waited for about 30 min) the only option is kill the program pressing the Pandora button...and when the prog is closed no backtrace is availble.
gdb stops running when the program being debugged segfaults, so it seems as if the program is frozen but gdb is actually waiting for a command.

I imagine that in Pandora you can't switch back to the terminal gdb is running in and type "backtrace" to get the backtrace. So instead create a gdb script like:
Code:
run
backtrace
continue

And then run gdb like:
Code:
gdb --batch --command=script.gdb ./prog
 
Change this line (in file jpeg.cpp):
Code:
memcpy(&pixels[cinfo.output_scanline*row_stride],buffer[0],row_stride);
to:
Code:
memcpy(&pixels[(cinfo.output_scanline-1)*row_stride],buffer[0],row_stride);
 
Great @M-HT this line solved the error when i run.
Thanks also to @Yoyobuae for the hint on gdb (i din't know that i could create
a script with gdb)
 
Back
Top