Skip to content
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

fixing memory leaks #36

Open
amiremohamadi opened this issue Oct 31, 2024 · 6 comments
Open

fixing memory leaks #36

amiremohamadi opened this issue Oct 31, 2024 · 6 comments
Labels
enhancement New feature or request

Comments

@amiremohamadi
Copy link
Collaborator

there are lots of places in which the program doesn't free the allocated memory properly (esp lexer.c). let's fix them at some point.

valgrind

@keyvank
Copy link
Owner

keyvank commented Nov 1, 2024

Can we replace the malloc with our custom one (Like idk: gcmalloc, garbage-collected malloc) which keeps track of mallocs in a linked-list and free them all at once at the end of compilation?

@amiremohamadi
Copy link
Collaborator Author

awesome! totally agree.

@keyvank keyvank added the enhancement New feature or request label Nov 10, 2024
@ookami125
Copy link
Contributor

Not try to butt in to much, but wouldn't doing that be kind of pointless? All allocations are already cleaned at the end of execution by the OS. The point of detecting memory leaks is more so you can free memory at appropriate times so your program itself has more memory to work with. Dropping everything in a custom allocator doesn't really fix the major problems with the leak, it just hides it from the leak analyzer.

But love looking at the project so far. It'll be interesting to see how it progresses.

@keyvank
Copy link
Owner

keyvank commented Dec 9, 2024

Not try to butt in to much, but wouldn't doing that be kind of pointless? All allocations are already cleaned at the end of execution by the OS. The point of detecting memory leaks is more so you can free memory at appropriate times so your program itself has more memory to work with. Dropping everything in a custom allocator doesn't really fix the major problems with the leak, it just hides it from the leak analyzer.

But love looking at the project so far. It'll be interesting to see how it progresses.

Tnx!

Imagine the compiler takes several files as input, and the allocations are all freed when we go compiling the next file. It wouldn't be pointless anymore, right?

@ookami125
Copy link
Contributor

Tnx!

Imagine the compiler takes several files as input, and the allocations are all freed when we go compiling the next file. It wouldn't be pointless anymore, right?

That's fair, in that case I could see it being more useful. Something more akin to a frame allocator used in games. I don't know what the plan is around multi-file compilation is though so I can't say anything concrete. Having an allocator that just holds onto everything till the end of a compilation however could cause congestion and slow down multi-threaded compilation in process (if implemented) or out of process (like cmake.)

Note: If everything is going to remain single threaded, then everything below might not be useful.

But at that point it's more about trade-offs since calling the system allocator can be slow. As before not the decision maker, but I think I would do the gcmalloc with a builtin leak reporting system (only enabled in debug?) and maybe something like a ref counted pointer. That would still allow you to fix or reduce leaks over time, both the simple and complex ones. That could also allow you to do a page based allocation so when a page is completely cleared it could be reused or freed to the system. That could allow you to fix leaks while still allowing you to just wipe out everything per compilation in the short term.

That might be a little complex though depending on the scope of the project.

@keyvank
Copy link
Owner

keyvank commented Dec 9, 2024

That might be a little complex though depending on the scope of the project.

Another option is to just ignore the leaks and keep it simple, since this is just an educational compiler hah

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants