I have a program that has to use mmf to map a ppm image to memory and then, each child, will have to invert row by row the mmf. It says :"the MMF version will first have to create a copy of the image (and rename it to the destiny filename) and then execute the inversion on that copy." The semaphores are in the correct order too and according to the assignment too.
From this, I've coded and it gives me the correct output but this happens :S. I don't understand why but it's clearly not right:
(...)
Inverting row...
Done ||
Inverting row...
Done ||
Cleaning up...
Closing file pointers.
*** glibc detected *** ./MMF_inverter: double free or corruption (!prev): 0x093a0170 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(+0x6b591)[0x28e591]
/lib/tls/i686/cmov/libc.so.6(+0x6cde8)[0x28fde8]
/lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0x292ecd]
/lib/tls/i686/cmov/libc.so.6(fclose+0x14a)[0x27eaaa]
./MMF_inverter[0x80497d5]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x239bd6]
./MMF_inverter[0x8048d01]
======= Memory map: ========
00110000-00125000 r-xp 00000000 08:05 1569987 /lib/tls/i686/cmov/libpthread-2.11.1.so
00125000-00126000 r--p 00014000 08:05 1569987 /lib/tls/i686/cmov/libpthread-2.11.1.so
00126000-00127000 rw-p 00015000 08:05 1569987 /lib/tls/i686/cmov/libpthread-2.11.1.so
00127000-00129000 rw-p 00000000 00:00 0
00129000-00146000 r-xp 00000000 08:05 1439060 /lib/libgcc_s.so.1
00146000-00147000 r--p 0001c000 08:05 1439060 /lib/libgcc_s.so.1
00147000-00148000 rw-p 0001d000 08:05 1439060 /lib/libgcc_s.so.1
00223000-00376000 r-xp 00000000 08:05 1569962 /lib/tls/i686/cmov/libc-2.11.1.so
00376000-00377000 ---p 00153000 08:05 1569962 /lib/tls/i686/cmov/libc-2.11.1.so
00377000-00379000 r--p 00153000 08:05 1569962 /lib/tls/i686/cmov/libc-2.11.1.so
00379000-0037a000 rw-p 00155000 08:05 1569962 /lib/tls/i686/cmov/libc-2.11.1.so
0037a000-0037d000 rw-p 00000000 00:00 0
00459000-0045a000 r-xp 00000000 00:00 0 [vdso]
00471000-0048c000 r-xp 00000000 08:05 1440096 /lib/ld-2.11.1.so
0048c000-0048d000 r--p 0001a000 08:05 1440096 /lib/ld-2.11.1.so
0048d000-0048e000 rw-p 0001b000 08:05 1440096 /lib/ld-2.11.1.so
00905000-0090c000 r-xp 00000000 08:05 1569989 /lib/tls/i686/cmov/librt-2.11.1.so
0090c000-0090d000 r--p 00006000 08:05 1569989 /lib/tls/i686/cmov/librt-2.11.1.so
0090d000-0090e000 rw-p 00007000 08:05 1569989 /lib/tls/i686/cmov/librt-2.11.1.so
08048000-0804b000 r-xp 00000000 08:05 1458241 /home/neverMind/Desktop/SO-TP2/MMF/MMF_inverter
0804b000-0804c000 r--p 00002000 08:05 1458241 /home/neverMind/Desktop/SO-TP2/MMF/MMF_inverter
0804c000-0804d000 rw-p 00003000 08:05 1458241 /home/neverMind/Desktop/SO-TP2/MMF/MMF_inverter
093a0000-093c1000 rw-p 00000000 00:00 0 [heap]
b7700000-b7721000 rw-p 00000000 00:00 0
b7721000-b7800000 ---p 00000000 00:00 0
b781c000-b7855000 rw-s 00000000 08:05 1458172 /home/neverMind/Desktop/SO-TP2/MMF/out.ppm
b7855000-b7857000 rw-p 00000000 00:00 0
b7862000-b7863000 rw-s 00000000 00:04 52069041 /SYSV00000000 (deleted)Aborted
Here is the code (run as ./invert input_filename.ppm output_filename.ppm) it has to be ppm:
main
functions c file
header file
makefile
You can test it with this for example: ppm image with p6 header
I'm only mapping the file in the master process, before spawn children and make each child invert a row on that mapped file. Is this correct?
Among other things, you are closing your file handles twice. Run your program under valgrind, as Drakosha suggested.
Related
I'm getting the familiar free(): invalid pointer error. In trying to debug, I ended up commenting out each free() in my code, one by one, until there were none left and I'm still getting this runtime error. Has anyone else run into a similar issue?
By the way - it's difficult for me to debug this using gdb, because the entire server doesn't actually crash when the error message is printed, just the particular forked process that was handling the single client.
Thank you.
==============================
*** glibc detected *** ./server: free(): invalid pointer: 0x08641a38 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x6b961)[0xefe961]
/lib/i386-linux-gnu/libc.so.6(+0x6d28b)[0xf0028b]
/lib/i386-linux-gnu/libc.so.6(cfree+0x6d)[0xf0341d]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x21)[0x4c74d1]
./server[0x804b499]
./server[0x804b2ad]
./server[0x804aecd]
./server[0x804ad36]
./server[0x804a3a3]
/lib/i386-linux-gnu/libc.so.6(+0x2fa6f)[0xec2a6f]
/lib/i386-linux-gnu/libc.so.6(+0x2facf)[0xec2acf]
./server[0x804966b]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0xea9e37]
./server[0x8049331]
======= Memory map: ========
00338000-00352000 r-xp 00000000 08:01 394236 /lib/i386-linux-gnu/libgcc_s.so.1
00352000-00353000 r--p 00019000 08:01 394236 /lib/i386-linux-gnu/libgcc_s.so.1
00353000-00354000 rw-p 0001a000 08:01 394236 /lib/i386-linux-gnu/libgcc_s.so.1
003c1000-003c2000 r-xp 00000000 00:00 0 [vdso]
0041d000-004fc000 r-xp 00000000 08:01 792946 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.14
004fc000-00500000 r--p 000de000 08:01 792946 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.14
00500000-00501000 rw-p 000e2000 08:01 792946 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.14
00501000-00508000 rw-p 00000000 00:00 0
00664000-00688000 r-xp 00000000 08:01 394245 /lib/i386-linux-gnu/libm-2.13.so
00688000-00689000 r--p 00023000 08:01 394245 /lib/i386-linux-gnu/libm-2.13.so
00689000-0068a000 rw-p 00024000 08:01 394245 /lib/i386-linux-gnu/libm-2.13.so
00793000-007af000 r-xp 00000000 08:01 394195 /lib/i386-linux-gnu/ld-2.13.so
007af000-007b0000 r--p 0001b000 08:01 394195 /lib/i386-linux-gnu/ld-2.13.so
007b0000-007b1000 rw-p 0001c000 08:01 394195 /lib/i386-linux-gnu/ld-2.13.so
00960000-0096a000 r-xp 00000000 08:01 394254 /lib/i386-linux-gnu/libnss_files-2.13.so
0096a000-0096b000 r--p 00009000 08:01 394254 /lib/i386-linux-gnu/libnss_files-2.13.so
0096b000-0096c000 rw-p 0000a000 08:01 394254 /lib/i386-linux-gnu/libnss_files-2.13.so
00e93000-00fed000 r-xp 00000000 08:01 394208 /lib/i386-linux-gnu/libc-2.13.so
00fed000-00fee000 ---p 0015a000 08:01 394208 /lib/i386-linux-gnu/libc-2.13.so
00fee000-00ff0000 r--p 0015a000 08:01 394208 /lib/i386-linux-gnu/libc-2.13.so
00ff0000-00ff1000 rw-p 0015c000 08:01 394208 /lib/i386-linux-gnu/libc-2.13.so
00ff1000-00ff4000 rw-p 00000000 00:00 0
08048000-08056000 r-xp 00000000 08:01 1084793 /home/mwrosen/cpe464/prog2/server
08056000-08057000 r--p 0000d000 08:01 1084793 /home/mwrosen/cpe464/prog2/server
08057000-08058000 rw-p 0000e000 08:01 1084793 /home/mwrosen/cpe464/prog2/server
08641000-08662000 rw-p 00000000 00:00 0 [heap]
b7600000-b7621000 rw-p 00000000 00:00 0
b7621000-b7700000 ---p 00000000 00:00 0
b7718000-b771b000 rw-p 00000000 00:00 0
b7729000-b772c000 rw-p 00000000 00:00 0
bfacf000-bfaf0000 rw-p 00000000 00:00 0 [stack]
If your code trashes a pointer used by some other code, other code may wind up calling free on an invalid pointer. Likely you are accessing some memory you don't own.
There are many ways this can happen, here are two common ones:
1) If you declare an array like int f[7];, the last array element is f[6]. Modifying f[7] can corrupt someone else's memory.
2) If you save a pointer to an object allocated on the stack, that object goes out of scope, and then you modify something through that pointer, you can corrupt someone else's memory.
Try to use valgrind to debug your issue.
If your error persist even with all free removed, it could be because some code is overflowing its memory zone (e.g. a buffer overflow).
I have a program, where I am injecting a fault and I am expecting this to cause a segmentation fault. The problem I am facing is that for a fault such as:
char *str = malloc(sizeof(char)*10);
free(str+1);
I get the following printed in the shell:
*** Error in `./tests': free(): invalid pointer: 0x0000000002442574 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x80996)[0x2abd5ff5b996]
./tests[0x401558]
./tests[0x401735]
./tests[0x402211]
./tests[0x402c1b]
./tests[0x4013fd]
./tests[0x4014a2]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0x2abd5fefcde5]
./tests[0x4011c9]
======= Memory map: ========
00400000-00407000 r-xp 00000000 08:05 13109176 /home/jay/Desktop/Mutate/Mutate/CMeanQueue-master/tests
00606000-00607000 r--p 00006000 08:05 13109176 /home/jay/Desktop/Mutate/Mutate/CMeanQueue-master/tests
00607000-00608000 rw-p 00007000 08:05 13109176 /home/jay/Desktop/Mutate/Mutate/CMeanQueue-master/tests
00608000-0060a000 rw-p 00000000 00:00 0
02440000-02461000 rw-p 00000000 00:00 0 [heap]
2abd5fcb7000-2abd5fcd8000 r-xp 00000000 08:05 11274438 /lib64/ld-2.17.so
2abd5fcd8000-2abd5fcdb000 rw-p 00000000 00:00 0
2abd5fcef000-2abd5fcf2000 rw-p 00000000 00:00 0
2abd5fed8000-2abd5fed9000 r--p 00021000 08:05 11274438 /lib64/ld-2.17.so
2abd5fed9000-2abd5fedb000 rw-p 00022000 08:05 11274438 /lib64/ld-2.17.so
2abd5fedb000-2abd60098000 r-xp 00000000 08:05 6164261 /lib/x86_64-linux-gnu/libc-2.17.so
2abd60098000-2abd60298000 ---p 001bd000 08:05 6164261 /lib/x86_64-linux-gnu/libc-2.17.so
2abd60298000-2abd6029c000 r--p 001bd000 08:05 6164261 /lib/x86_64-linux-gnu/libc-2.17.so
2abd6029c000-2abd6029e000 rw-p 001c1000 08:05 6164261 /lib/x86_64-linux-gnu/libc-2.17.so
2abd6029e000-2abd602a3000 rw-p 00000000 00:00 0
2abd602a3000-2abd602b8000 r-xp 00000000 08:05 6160389 /lib/x86_64-linux-gnu/libgcc_s.so.1
2abd602b8000-2abd604b7000 ---p 00015000 08:05 6160389 /lib/x86_64-linux-gnu/libgcc_s.so.1
2abd604b7000-2abd604b8000 r--p 00014000 08:05 6160389 /lib/x86_64-linux-gnu/libgcc_s.so.1
2abd604b8000-2abd604b9000 rw-p 00015000 08:05 6160389 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fffee8c6000-7fffee8e7000 rw-p 00000000 00:00 0 [stack]
7fffee993000-7fffee995000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Now, what I am looking to do is hide this from the display, since I am making a fault injection tool, I am aware of this happening and do not want such an output to ruin the GUI for the user. I have tried to redirect the stderror to file 2) change unlimit to 0 and nothing works.
NOTE:: I am injecting this problem into a code myself, hence please do not suggest to fix the error. My aim is to hide this from printing on the shell.
THanks
Solved for the above example using:
#export MALLOC_CHECK_=0
http://kb.parallels.com/en/4611
Try changing your kernel log level to limit what it prints.
something like:
bash$ echo "0 0 0 0" > /proc/sys/kernel/printk
Great write up here: Change default console loglevel during boot up
This question already has answers here:
When / How does Linux load shared libraries into address space?
(5 answers)
Closed 9 years ago.
On linux platform,
Could anyone tell me where is the dynamic library in the memory?
I learned that the dynamic library are mmap to the process according to the GOT
of this process,
is that true?
Thank you!
You can see where things got mapped in a Linux process by looking in /proc/pid/maps -- all you need to know is the process id. For example:
$ cat /proc/self/maps
00400000-0040b000 r-xp 00000000 08:01 71827604 /bin/cat
0060a000-0060b000 r--p 0000a000 08:01 71827604 /bin/cat
0060b000-0060c000 rw-p 0000b000 08:01 71827604 /bin/cat
00690000-006b1000 rw-p 00000000 00:00 0 [heap]
7f07fbaf7000-7f07fbdc0000 r--p 00000000 08:01 18094104 /usr/lib/locale/locale-archive
7f07fbdc0000-7f07fbf75000 r-xp 00000000 08:01 14552996 /lib/x86_64-linux-gnu/libc-2.15.so
7f07fbf75000-7f07fc175000 ---p 001b5000 08:01 14552996 /lib/x86_64-linux-gnu/libc-2.15.so
7f07fc175000-7f07fc179000 r--p 001b5000 08:01 14552996 /lib/x86_64-linux-gnu/libc-2.15.so
7f07fc179000-7f07fc17b000 rw-p 001b9000 08:01 14552996 /lib/x86_64-linux-gnu/libc-2.15.so
7f07fc17b000-7f07fc180000 rw-p 00000000 00:00 0
7f07fc180000-7f07fc1a2000 r-xp 00000000 08:01 14553008 /lib/x86_64-linux-gnu/ld-2.15.so
7f07fc37e000-7f07fc381000 rw-p 00000000 00:00 0
7f07fc3a0000-7f07fc3a2000 rw-p 00000000 00:00 0
7f07fc3a2000-7f07fc3a3000 r--p 00022000 08:01 14553008 /lib/x86_64-linux-gnu/ld-2.15.so
7f07fc3a3000-7f07fc3a5000 rw-p 00023000 08:01 14553008 /lib/x86_64-linux-gnu/ld-2.15.so
7fff90e28000-7fff90e49000 rw-p 00000000 00:00 0 [stack]
7fff90f1f000-7fff90f20000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
shows everything that got mapped in to run the cat program.
my SDL application is crashing sporadically with a very weird error.
glibc is complaining about a "double free or corruption" error and crashing the program. I don't have any multi-threading and the code in question is only freeing a surface that has been allocated in the same static function.
Doing an object dump, I find that the crash is occurring in the following code segment.
Code segment:
static void draw_text_to_screen(const char *text, TTF_Font *text_font,
SDL_Color text_color, unsigned int x_offset, unsigned int y_offset)
{
SDL_Rect offset = (SDL_Rect) {x_offset, y_offset, 0, 0};
SDL_Surface *surface_text =
TTF_RenderText_Solid(text_font, text, text_color);
SDL_BlitSurface(surface_text, NULL, screen, &offset);
printf("Last thing that is printed\n");
SDL_FreeSurface(surface_text);
printf("Not reaching here...\n", surface_text);
}
*** glibc detected *** ./app: double free or corruption (out): 0x0000000002bf82d0 *
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7f78a054bb96]
/usr/lib/x86_64-linux-gnu/libSDL-1.2.so.0(SDL_FreeSurface+0xdd)[0x7f78a0cdf9dd]
./app[0x4041da]
./app[0x404985]
./app[0x403322]
./app[0x4036a0]
./app[0x401dd9]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f78a04ee76d]
./app[0x401f25]
======= Memory map: ========
00400000-0040c000 r-xp 00000000 08:05 3805386 /home/me/repos/audio/app
0060b000-0060c000 r--p 0000b000 08:05 3805386 /home/me/repos/audio/app
0060c000-0060d000 rw-p 0000c000 08:05 3805386 /home/me/repos/audio/app
02199000-03307000 rw-p 00000000 00:00 0 [heap]
7f7899461000-7f7899476000 r-xp 00000000 08:05 3804734 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f7899476000-7f7899675000 ---p 00015000 08:05 3804734 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f7899675000-7f7899676000 r--p 00014000 08:05 3804734 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f7899676000-7f7899677000 rw-p 00015000 08:05 3804734 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f7899677000-7f7899ace000 rw-s 00000000 00:04 229377 /SYSV00000000 (deleted)
7f7899ace000-7f7899ad3000 r-xp 00000000 08:05 4857142 /usr/lib/x86_64-linux-gnu/libXfixes.so.3.1.0
7f7899ad3000-7f7899cd2000 ---p 00005000 08:05 4857142 /usr/lib/x86_64-linux-gnu/libXfixes.so.3.1.0
7f7899cd2000-7f7899cd3000 r--p 00004000 08:05 4857142 /usr/lib/x86_64-linux-gnu/libXfixes.so.3.1.0
7f7899cd3000-7f7899cd4000 rw-p 00005000 08:05 4857142 /usr/lib/x86_64-linux-gnu/libXfixes.so.3.1.0
7f7899cd4000-7f7899cdd000 r-xp 00000000 08:05 4857160 /usr/lib/x86_64-linux-gnu/libXrender.so.1.3.0
7f7899cdd000-7f7899edc000 ---p 00009000 08:05 4857160 /usr/lib/x86_64-linux-gnu/libXrender.so.1.3.0
7f7899edc000-7f7899edd000 r--p 00008000 08:05 4857160 /usr/lib/x86_64-linux-gnu/libXrender.so.1.3.0
7f7899edd000-7f7899ede000 rw-p 00009000 08:05 4857160 /usr/lib/x86_64-linux-gnu/libXrender.so.1.3.0
7f7899ede000-7f7899ee7000 r-xp 00000000 08:05 4857134 /usr/lib/x86_64-linux-gnu/libXcursor.so.1.0.2
7f7899ee7000-7f789a0e6000 ---p 00009000 08:05 4857134 /usr/lib/x86_64-linux-gnu/libXcursor.so.1.0.2
7f789a0e6000-7f789a0e7000 r--p 00008000 08:05 4857134 /usr/lib/x86_64-linux-gnu/libXcursor.so.1.0.2
7f789a0e7000-7f789a0e8000 rw-p 00009000 08:05 4857134 /usr/lib/x86_64-linux-gnu/libXcursor.so.1.0.2
7f789a0e8000-7f789a7cb000 r--p 00000000 08:05 4856463 /usr/lib/locale/locale-archive
7f789a7cb000-7f789a7cc000 ---p 00000000 00:00 0
7f789a7cc000-7f789afcc000 rw-p 00000000 00:00 0
7f789afcc000-7f789afe4000 r-xp 00000000 08:05 3801993 /lib/x86_64-linux-gnu/libresolv-2.15.so
7f789afe4000-7f789b1e4000 ---p 00018000 08:05 3801993 /lib/x86_64-linux-gnu/libresolv-2.15.so
7f789b1e4000-7f789b1e5000 r--p 00018000 08:05 3801993 /lib/x86_64-linux-gnu/libresolv-2.15.so
7f789b1e5000-7f789b1e6000 rw-p 00019000 08:05 3801993 /lib/x86_64-linux-gnu/libresolv-2.15.so
7f789b1e6000-7f789b1e8000 rw-p 00000000 00:00 0
7f789b1e8000-7f789b1ee000 r-xp 00000000 08:05 4857537 /usr/lib/x86_64-linux-gnu/libogg.so.0.7.1
7f789b1ee000-7f789b3ed000 ---p 00006000 08:05 4857537 /usr/lib/x86_64-linux-gnu/libogg.so.0.7.1
7f789b3ed000-7f789b3ee000 r--p 00005000 08:05 4857537 /usr/lib/x86_64-linux-gnu/libogg.so.0.7.1
7f789b3ee000-7f789b3ef000 rw-p 00006000 08:05 4857537 /usr/lib/x86_64-linux-gnu/libogg.so.0.7.1
7f789b3ef000-7f789b41a000 r-xp 00000000 08:05 4857695 /usr/lib/x86_64-linux-gnu/libvorbis.so.0.4.5
7f789b41a000-7f789b619000 ---p 0002b000 08:05 4857695 /usr/lib/x86_64-linux-gnu/libvorbis.so.0.4.5
7f789b619000-7f789b61a000 r--p 0002a000 08:05 4857695 /usr/lib/x86_64-linux-gnu/libvorbis.so.0.4.5
7f789b61a000-7f789b61b000 rw-p 0002b000 08:05 4857695 /usr/lib/x86_64-linux-gnu/libvorbis.so.0.4.5
7f789b61b000-7f789b8ce000 r-xp 00000000 08:05 4857697 /usr/lib/x86_64-linux-gnu/libvorbisenc.so.2.0.8
7f789b8ce000-7f789bacd000 ---p 002b3000 08:05 4857697 /usr/lib/x86_64-linux-gnu/libvorbisenc.so.2.0.8
7f789bacd000-7f789bae9000 r--p 002b2000 08:05 4857697 /usr/lib/x86_64-linux-gnu/libvorbisenc.so.2.0.8
7f789bae9000-7f789baea000 rw-p 002ce000 08:05 4857697 /usr/lib/x86_64-linux-gnu/libvorbisenc.so.2.0.8
7f789baea000-7f789bb32000 r-xp 00000000 08:05 4857070 /usr/lib/x86_64-linux-gnu/libFLAC.so.8.2.0
7f789bb32000-7f789bd32000 ---p 00048000 08:05 4857070 /usr/lib/x86_64-linux-gnu/libFLAC.so.8.2.0
7f789bd32000-7f789bd33000 r--p 00048000 08:05 4857070 /usr/lib/x86_64-linux-gnu/libFLAC.so.8.2.0
7f789bd33000-7f789bd34000 rw-p 00049000 08:05 4857070 /usr/lib/x86_64-linux-gnu/libFLAC.so.8.2.0
7f789bd34000-7f789bd4b000 r-xp 00000000 08:05 3804922 /lib/x86_64-linux-gnu/libnsl-2.15.so
7f789bd4b000-7f789bf4a000 ---p 00017000 08:05 3804922 /lib/x86_64-linux-gnu/libnsl-2.15.so
7f789bf4a000-7f789bf4b000 r--p 00016000 08:05 3804922 /lib/x86_64-linux-gnu/libnsl-2.15.so
7f789bf4b000-7f789bf4c000 rw-p 00017000 08:05 3804922 /lib/x86_64-linux-gnu/libnsl-2.15.so
7f789bf4c000-7f789bf4e000 rw-p 00000000 00:00 0
7f789bf4e000-7f789bf53000 r-xp 00000000 08:05 4857138 /usr/lib/x86_64-linux-gnu/libXdmcp.so.6.0.0
7f789bf53000-7f789c152000 ---p 00005000 08:05 4857138 /usr/lib/x86_64-linux-gnu/libXdmcp.so.6.0.0
7f789c152000-7f789c153000 r--p 00004000 08:05 4857138 /usr/lib/x86_64-linux-gnu/libXdmcp.so.6.0.0
7f789c153000-7f789c154000 rw-p 00005000 08:05 4857138 /usr/lib/x86_64-linux-gnu/libXdmcp.so.6.0.0
7f789c154000-7f789c156000 r-xp 00000000 08:05 4857127 /usr/lib/x86_64-linux-gnu/libXau.so.6.0.0
7f789c156000-7f789c355000 ---p 00002000 08:05 4857127 /usr/lib/x86_64-linux-gnu/libXau.so.6.0.0
7f789c355000-7f789c356000 r--p 00001000 08:05 4857127 /usr/lib/x86_64-linux-gnu/libXau.so.6.0.0Aborted (core dumped)
Apparently SDL_FreeSurface() will catch a NULL pointer, so this is not because you failed to check the return value of TTF_RenderText_Solid(), which, according to this API reference, will return NULL on error.
However, what that would mean for SDL_BlitSurface() I don't know, so you should be checking that anyway. I notice in some documentation for BlitSurface that:
If either of the surfaces were in video memory, and the blit returns -2, the video memory was lost, so it should be reloaded with artwork and re-blitted...
Which may have some significance.
WRT to this:
printf("Last thing that is printed\n");
SDL_FreeSurface(surface_text);
printf("Not reaching here...\n", surface_text);
Although a newline may flush the stdout buffer, it is a much better idea to use stderr --fprintf(stderr, "...") -- for this kind of debugging since stderr is unbuffered and the program may actually die after your second printf() is executed but before the stdout buffer is flushed. Deceptive!
I'm getting the familiar free(): invalid pointer error. In trying to debug, I ended up commenting out each free() in my code, one by one, until there were none left and I'm still getting this runtime error. Has anyone else run into a similar issue?
By the way - it's difficult for me to debug this using gdb, because the entire server doesn't actually crash when the error message is printed, just the particular forked process that was handling the single client.
Thank you.
==============================
*** glibc detected *** ./server: free(): invalid pointer: 0x08641a38 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x6b961)[0xefe961]
/lib/i386-linux-gnu/libc.so.6(+0x6d28b)[0xf0028b]
/lib/i386-linux-gnu/libc.so.6(cfree+0x6d)[0xf0341d]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x21)[0x4c74d1]
./server[0x804b499]
./server[0x804b2ad]
./server[0x804aecd]
./server[0x804ad36]
./server[0x804a3a3]
/lib/i386-linux-gnu/libc.so.6(+0x2fa6f)[0xec2a6f]
/lib/i386-linux-gnu/libc.so.6(+0x2facf)[0xec2acf]
./server[0x804966b]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0xea9e37]
./server[0x8049331]
======= Memory map: ========
00338000-00352000 r-xp 00000000 08:01 394236 /lib/i386-linux-gnu/libgcc_s.so.1
00352000-00353000 r--p 00019000 08:01 394236 /lib/i386-linux-gnu/libgcc_s.so.1
00353000-00354000 rw-p 0001a000 08:01 394236 /lib/i386-linux-gnu/libgcc_s.so.1
003c1000-003c2000 r-xp 00000000 00:00 0 [vdso]
0041d000-004fc000 r-xp 00000000 08:01 792946 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.14
004fc000-00500000 r--p 000de000 08:01 792946 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.14
00500000-00501000 rw-p 000e2000 08:01 792946 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.14
00501000-00508000 rw-p 00000000 00:00 0
00664000-00688000 r-xp 00000000 08:01 394245 /lib/i386-linux-gnu/libm-2.13.so
00688000-00689000 r--p 00023000 08:01 394245 /lib/i386-linux-gnu/libm-2.13.so
00689000-0068a000 rw-p 00024000 08:01 394245 /lib/i386-linux-gnu/libm-2.13.so
00793000-007af000 r-xp 00000000 08:01 394195 /lib/i386-linux-gnu/ld-2.13.so
007af000-007b0000 r--p 0001b000 08:01 394195 /lib/i386-linux-gnu/ld-2.13.so
007b0000-007b1000 rw-p 0001c000 08:01 394195 /lib/i386-linux-gnu/ld-2.13.so
00960000-0096a000 r-xp 00000000 08:01 394254 /lib/i386-linux-gnu/libnss_files-2.13.so
0096a000-0096b000 r--p 00009000 08:01 394254 /lib/i386-linux-gnu/libnss_files-2.13.so
0096b000-0096c000 rw-p 0000a000 08:01 394254 /lib/i386-linux-gnu/libnss_files-2.13.so
00e93000-00fed000 r-xp 00000000 08:01 394208 /lib/i386-linux-gnu/libc-2.13.so
00fed000-00fee000 ---p 0015a000 08:01 394208 /lib/i386-linux-gnu/libc-2.13.so
00fee000-00ff0000 r--p 0015a000 08:01 394208 /lib/i386-linux-gnu/libc-2.13.so
00ff0000-00ff1000 rw-p 0015c000 08:01 394208 /lib/i386-linux-gnu/libc-2.13.so
00ff1000-00ff4000 rw-p 00000000 00:00 0
08048000-08056000 r-xp 00000000 08:01 1084793 /home/mwrosen/cpe464/prog2/server
08056000-08057000 r--p 0000d000 08:01 1084793 /home/mwrosen/cpe464/prog2/server
08057000-08058000 rw-p 0000e000 08:01 1084793 /home/mwrosen/cpe464/prog2/server
08641000-08662000 rw-p 00000000 00:00 0 [heap]
b7600000-b7621000 rw-p 00000000 00:00 0
b7621000-b7700000 ---p 00000000 00:00 0
b7718000-b771b000 rw-p 00000000 00:00 0
b7729000-b772c000 rw-p 00000000 00:00 0
bfacf000-bfaf0000 rw-p 00000000 00:00 0 [stack]
If your code trashes a pointer used by some other code, other code may wind up calling free on an invalid pointer. Likely you are accessing some memory you don't own.
There are many ways this can happen, here are two common ones:
1) If you declare an array like int f[7];, the last array element is f[6]. Modifying f[7] can corrupt someone else's memory.
2) If you save a pointer to an object allocated on the stack, that object goes out of scope, and then you modify something through that pointer, you can corrupt someone else's memory.
Try to use valgrind to debug your issue.
If your error persist even with all free removed, it could be because some code is overflowing its memory zone (e.g. a buffer overflow).