-
Notifications
You must be signed in to change notification settings - Fork 183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory leaks with static variables #15
Comments
I would advise against putting variables on the stack. Stack space is typically much smaller than heap space. One of the fixes I recently made to PicoC was to move data off the stack and into the heap because putting it on the stack was causing "out of Memory" errors. |
Thanks for the advice. My quick&dirty fix is not a real solution indeed. I tried to locate all calls to
This makes 128 allocs and 128 frees, whereas On the other hand, when I activate
As far as I understand, 40+24+40 bytes were allocated and only 40 deallocated, which would suggest that 64 bytes were lost, as opposed to 40 reported by Any help to understand better what is going on here is welcome! |
Hi,
Thanks for the great job developing PicoC in the first place!
When playing around with the interpreter I noticed that using static variables triggers a memory leak. To reproduce the bug run the following minimal working example:
(saved e. g. in static.c) under
valgrind
(picoc compiled with-O0 -g
):On my Linux 3.16.0 with gcc 4.8.4 and valgrind 3.10.0.SVN it outputs:
The problem vanishes when
static
qualifier is removed. The issue becomes much more serious when static variables are used within a function called periodically, e. g.:The code above acquires heap memory pretty quickly (which you can see e. g. with
top
under Linux) and gets killed by the kernel as soon as it exhausts the physical memory, e. g. on my embedded device:My quick and dirty fix would be to put all global variables allocated with
VariableDefinePlatformVar
on the stack instead of the heap, i. e. replace line 377 ofvariable.c
:with:
This way
valgrind
reports no memory leaks on mystatic.c
above and the infinite loop callingmy_func
runs at constant memory. Moreover, all the regression tests keep passing with no errors.Could you advise if this thinking makes any sense? Or maybe the call to
VariableDefinePlatformVar
should be context sensitive, such that local-scope copies of static variables only should be allocated on the stack?Best regards,
Lukasz
The text was updated successfully, but these errors were encountered: