SDL memory usage - c

I'm writing a very basic SDL application, but when I run it through valgrind, it reports several lost blocks apparently related to the SDL library (before you ask it, I do call SDL_Quit, or more precisely I call atexit(SDL_Quit)). Here is an example:
==2525== 192 (16 direct, 176 indirect) bytes in 1 blocks are definitely lost in loss record 107 of 131
==2525== at 0x4C25502: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2525== by 0x644244A: ??? (in /usr/lib/libX11.so.6.3.0)
==2525== by 0x6442989: ??? (in /usr/lib/libX11.so.6.3.0)
==2525== by 0x64440A2: ??? (in /usr/lib/libX11.so.6.3.0)
==2525== by 0x6444915: _XlcCreateLC (in /usr/lib/libX11.so.6.3.0)
==2525== by 0x6462B5F: _XlcDefaultLoader (in /usr/lib/libX11.so.6.3.0)
==2525== by 0x644C325: _XOpenLC (in /usr/lib/libX11.so.6.3.0)
==2525== by 0x644C467: _XlcCurrentLC (in /usr/lib/libX11.so.6.3.0)
==2525== by 0x644C4BD: XSetLocaleModifiers (in /usr/lib/libX11.so.6.3.0)
==2525== by 0x4E69EED: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==2525== by 0x4E6A57F: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==2525== by 0x4E59E00: SDL_VideoInit (in /usr/lib/libSDL-1.2.so.0.11.3)
I searched here on StackOverflow and found a similar question. The answer was that apparently, there are some small buffers allocated within the library which the writers never care to free. My question is, what is the motivation of such an approach? Why allocate something and then deliberately not free() it?

If the buffers are needed till the end of the program there is no need to free them. When a program exits all memory it was using is freed by the OS anyway. If the program bothered to free the blocks before exiting it would only serve to slow down the exiting of the program.

Related

Segmentation Fault error while running C program [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm new using Valgrind for the first time to check memory errrors. I'm running C program and seeing the errors that are not related to the C program but all the errors are from memory (open64.c:48, _IO_file_open (fileops.c:189), .....). I don't know where these files are located. Could you please help me how to resolve this?
==40910== Memcheck, a memory error detector
==40910== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==40910== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==40910== Command: ./dd
==40910==
==40910== Syscall param openat(filename) points to unaddressable byte(s)
==40910== at 0x4ABCEAB: open (open64.c:48)
==40910== by 0x4A3F195: _IO_file_open (fileops.c:189)
==40910== by 0x4A3F459: _IO_file_fopen##GLIBC_2.2.5 (fileops.c:281)
==40910== by 0x4A31B0D: __fopen_internal (iofopen.c:75)
==40910== by 0x4A31B0D: fopen##GLIBC_2.2.5 (iofopen.c:86)
==40910== by 0x109336: main (in /home/Desktop/dd)
==40910== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==40910==
==40910== Invalid read of size 4
==40910== at 0x4A317D7: fgets (iofgets.c:47)
==40910== by 0x109427: main (in /home/Desktop/dd)
==40910== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==40910==
==40910==
==40910== Process terminating with default action of signal 11 (SIGSEGV)
==40910== Access not within mapped region at address 0x0
==40910== at 0x4A317D7: fgets (iofgets.c:47)
==40910== by 0x109427: main (in /home/Desktop/dd)
==40910== If you believe this happened as a result of a stack
==40910== overflow in your program's main thread (unlikely but
==40910== possible), you can try to increase the size of the
==40910== main thread stack using the --main-stacksize= flag.
==40910== The main thread stack size used in this run was 16777216.
==40910==
==40910== HEAP SUMMARY:
==40910== in use at exit: 984 bytes in 3 blocks
==40910== total heap usage: 4 allocs, 1 frees, 1,456 bytes allocated
==40910==
==40910== LEAK SUMMARY:
==40910== definitely lost: 0 bytes in 0 blocks
==40910== indirectly lost: 0 bytes in 0 blocks
==40910== possibly lost: 0 bytes in 0 blocks
==40910== still reachable: 984 bytes in 3 blocks
==40910== suppressed: 0 bytes in 0 blocks
==40910== Rerun with --leak-check=full to see details of leaked memory
==40910==
==40910== For lists of detected and suppressed errors, rerun with: -s
==40910== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)
Without the code this is certainly the easiest question to answer!
"unaddressable" = point to byte that do not belong to you.
valgrind warns you because probably the memory that you freed in memory is not yours (or, at least, it is no longer reserved for the use you had asked for), and then you could be using it for another thing and interpret a value that is not.
Why doesn't it break when you run without valgrind? Good, for starters - that's what you say. For onething your code is not doing appropriate error checking. So it may be breaking inside, so you wont notice it. All I could say is bad coding style may compile and runs without showing you any errors but in the background it maybe suffocating itself or the thing which it is running on.
Address 0x0 is not stack'd, malloc'd or (recently) free'd`
tells you you're dereferencing a NULL pointer (Address 0x0 ...) meaning fopen failed and returned 0/NULL.
Try fixing it? like..
-Check if returned fopen() valid FILE* to avoid undefined behavior when trying to read from input_file.
-Make sure that if fgets() succeeds (does not return NULL) to avoid undefined behavior.
PS: Read "The 8 Commandments for C Programmers"
2. Thou shalt not follow the NULL pointer, for chaos and madness await thee at its end.
6. If a function be advertised to return an error code in the event of difficulties, thou shalt check for that code, yea, even though the checks triple the size of thy code and produce aches in thy typing fingers, for if thou thinkest “it cannot happen to me”, the gods shall surely punish thee for thy arrogance.
Address 0x0 is not stack'd, malloc'd or (recently) free'd
That means you are using a NULL pointer ( NULL = (void*)0 AND 0 = 0x0 in hexadecimal). Try check if a pointer is NULL before using it.
Edit: if you are using "fopen", this function returns NULL if it cannot open the file.

Memory leak when C program ran from bash script

I have a C program that takes various command line arguments, i.e
./Coupled arg1 argv2
And when I run this with valgrind as
valgrind ./Coupled arg1 arg2
I get no memory leaks. But when I use a bash script, called run, of the form
arg1=thing1
arg2=thing2
./Coupled $thing1 $thing2
and then run
valgrind ./run
I get a lot of still reachable memory leakage. I have read that that still reachable memory leakage isn't a huge problem, but I would quite like to know why this is happening? When running valgrind with --leak-check=full --show-leak-kinds=all flags, an example bit of output (the full valgrind output is many pages long)
==4518== 1 bytes in 1 blocks are still reachable in loss record 1 of 269
==4518== at 0x4C29BE3: malloc (vg_replace_malloc.c:299)
==4518== by 0x46A3DA: xmalloc (in /usr/bin/bash)
==4518== by 0x437219: make_variable_value (in /usr/bin/bash)
==4518== by 0x438230: ??? (in /usr/bin/bash)
==4518== by 0x43A35E: initialize_shell_variables (in /usr/bin/bash)
==4518== by 0x41DD92: ??? (in /usr/bin/bash)
==4518== by 0x41C482: main (in /usr/bin/bash)
valgrind ./run will debug the shell and not your program.
Take a look at the output, see how it mentions (e.g.)
==4518== by 0x41C482: main (in /usr/bin/bash)
[Emphasis mine]
If you want to debug your program, you need to run valgrind in the script:
arg1=thing1
arg2=thing2
valgrind ./Coupled $thing1 $thing2

C - memory leak in case of big input [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
For an assignment, I have to order a list of students. Each one is represented by a number (string of size 15), his father's lastname (string 20), his mother's lastname (string 20) and his firstname (string 20 also).
I did a program that build from a file the list of students and order it (I use a merge sort to do so).
When I run the program on small number of students (<10 000) everything is fine (no memory leak or anything according to valgrind).
However, as soon as I try to use it on bigger ones (more than 100 000), I get a segmentation fault 11. I investigated with Valgrind and it says the error comes from the strcy or strcasecmp functions, and renders :
==2433== Invalid write of size 8
==2433== at 0x4019BD: merge (sort.c:59)
==2433== by 0x40173B: sortBeginEnd (sort.c:38)
==2433== by 0x4014B0: sortWithoutInterval (sort.c:9)
==2433== by 0x401EE0: firstSort (sort.c:166)
==2433== by 0x4009EB: main (main.c:44)
==2433== Address 0xffe79ac88 is on thread 1's stack
==2433==
==2433==
==2433== Process terminating with default action of signal 11 (SIGSEGV)
==2433== Access not within mapped region at address 0xFFE79AC88
==2433== at 0x4019BD: merge (sort.c:59)
==2433== If you believe this happened as a result of a stack
==2433== overflow in your program's main thread (unlikely but
==2433== possible), you can try to increase the size of the
==2433== main thread stack using the --main-stacksize= flag.
==2433== The main thread stack size used in this run was 8388608.
==2433==
==2433== Process terminating with default action of signal 11 (SIGSEGV)
==2433== Access not within mapped region at address 0xFFE79AC81
==2433== at 0x4A256B0: _vgnU_freeres (in /usr/lib/valgrind/vgpreload_core-amd64-linux.so)
==2433== If you believe this happened as a result of a stack
==2433== overflow in your program's main thread (unlikely but
==2433== possible), you can try to increase the size of the
==2433== main thread stack using the --main-stacksize= flag.
==2433== The main thread stack size used in this run was 8388608.
==2433==
==2433== HEAP SUMMARY:
==2433== in use at exit: 12,800,101 bytes in 500,007 blocks
==2433== total heap usage: 500,008 allocs, 1 frees, 12,800,669 bytes allocated
==2433==
==2433== LEAK SUMMARY:
==2433== definitely lost: 0 bytes in 0 blocks
==2433== indirectly lost: 0 bytes in 0 blocks
==2433== possibly lost: 0 bytes in 0 blocks
==2433== still reachable: 12,800,101 bytes in 500,007 blocks
==2433== suppressed: 0 bytes in 0 blocks
==2433== Rerun with --leak-check=full to see details of leaked memory
==2433==
==2433== For counts of detected and suppressed errors, rerun with: -v
==2433== ERROR SUMMARY: 7452721 errors from 31 contexts (suppressed: 0 from 0)
Could the error be that I use too much memory (each student represents 79 characters = 316 bytes and I have 100 000 of them so it is 31 600 000 bytes if I am right) ?
PS : i am not really familiar with the concept of stack and heap
EDIT :
"Everything is fine" valgrind report :
==2454==
==2454== HEAP SUMMARY:
==2454== in use at exit: 0 bytes in 0 blocks
==2454== total heap usage: 50,008 allocs, 50,008 frees, 1,280,669 bytes allocated
==2454==
==2454== All heap blocks were freed -- no leaks are possible
==2454==
==2454== For counts of detected and suppressed errors, rerun with: -v
==2454== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
EDIT2 :
The code is available here if you want to check it.
EDIT LAST :
I finally found the solution thanks to #Lundin's answer. The problem was that i was not using a malloc to allocate the temporary arrays for the merge part of the mergeSort.
I will investigate a bit more the question of heap/stack to fully understand the problem.
You aren't even mentioning which system this is for. Because of Valgrind I assume Linux. You don't mention where you allocate the variables. Apparently not on the heap since Valgrid only reports 12.8kb there.
If I remember correctly (and I know very little of Linux) processes have a stack size of roughly 8Mb.
316 * 10000 = 3.16 Mb.
316 * 100000 = 31.60 Mb.
Qualified guess: if you are allocating your variables in any other way than with malloc, then stack overflow is the source of the described problems.
Whenever using large amounts of memory in your program, you must allocate them dynamically on the heap.
the stack is the place, where your function holds its local/temporary data (parameters and local variables). it is organized as stack of papers, so when you call a function, the parameters are put onto the stack, and when the function finishes, everything except the result is discarded from the stack. normally the stack has a limited size.
the heap is the memory, where your allocated data is kept (f.e. malloc()). you can have different heaps (for your application, for each process and system wide)

debugging a multithreaded program with valgrind and gdb

Premise, I'm using Eclipse.
In my quest for the debug of a multithreaded application i first ran valgrind memcheck that gave me a bunch of errors, couldn't identify which lines of code these errors originated from.
I then created a profile to use valgrind on Debug build, it gave me an error
"Invalid read of size 1" that pointed to a line in the source code which allowed me to fix it. Now valgrind runs on the Debug build are giving me no errors, but if I try to run valgrind on the Release build i get errors, which cannot pinpoint.
==5083== 16 bytes in 1 blocks are definitely lost in loss record 2 of 4
==5083== at 0x4C29F90: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5083== by 0x400F67: main (in /home/crysis/workspace/ReliableUPDserver/Release/ReliableUPDserver)
==5083==
==5083== 16 bytes in 1 blocks are definitely lost in loss record 3 of 4
==5083== at 0x4C29F90: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5083== by 0x400FA1: main (in /home/crysis/workspace/ReliableUPDserver/Release/ReliableUPDserver)
==5083==
==5083== 512 bytes in 1 blocks are possibly lost in loss record 4 of 4
==5083== at 0x4C2C080: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5083== by 0x400F3E: main (in /home/crysis/workspace/ReliableUPDserver/Release/ReliableUPDserver)
==5083==
How come these errors appear only with the Release build? What can i do to get more information?
Also, my multithreaded program hangs somewhere, is this the right way to try find out where the problem is?

valgrind can not detect any leak

We run our program on a Linux virtual machine which compiles from linux-2.6.3-long-term. I want to run valgrind on that machine, and I compile from source code in the suggested way.
./configure --prefix=/tmp/valgrind/
make
make install
Then, I copy the /tmp/valgrind to my own Linux machine, I put it on the directory /data, set the environment variables:
export PATH=$PATH:/data/valgrind/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/lib:/lib64:/data/valgrind/lib
export VALGRIND_LIB=/data/valgrind/lib/valgrind
The valgrind can work, and can detect some other errors except memory leak. Here is the result of running valgrind ls -l
==2207==
==2207== Conditional jump or move depends on uninitialised value(s)
==2207== at 0x80F3CDE: ??? (in /bin/busybox)
==2207== by 0x80548ED: ??? (in /bin/busybox)
==2207==
==2207== Use of uninitialised value of size 4
==2207== at 0x80D8F77: ??? (in /bin/busybox)
==2207== by 0x8048866: ??? (in /bin/busybox)
==2207==
==2207== Conditional jump or move depends on uninitialised value(s)
==2207== at 0x80F0F55: ??? (in /bin/busybox)
==2207== by 0x80D8F49: ??? (in /bin/busybox)
==2207== by 0x8048866: ??? (in /bin/busybox)
==2207==
==2207== Conditional jump or move depends on uninitialised value(s)
==2207== at 0x80F1000: ??? (in /bin/busybox)
==2207== by 0x80D8F49: ??? (in /bin/busybox)
==2207== by 0x8048866: ??? (in /bin/busybox)
==2207==
==2207==
==2207== HEAP SUMMARY:
==2207== in use at exit: 0 bytes in 0 blocks
==2207== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==2207==
==2207== All heap blocks were freed -- no leaks are possible
==2207==
==2207== For counts of detected and suppressed errors, rerun with: -v
==2207== Use --track-origins=yes to see where uninitialised values come from
==2207== ERROR SUMMARY: 2390 errors from 117 contexts (suppressed: 0 from 0)
The total heap usage is all zero. And I find someone has already meet the same problem:
cross-compiled Valgrind does not detect obvious leaks
Is it a known issue?
The link above did not tell how to fix it, anyone know?

Resources