-
Notifications
You must be signed in to change notification settings - Fork 69
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 leak in Surface and TileMap #653
Comments
I think I prefer your current approach of flagging when memory has been allocated (and thus needs tidying up) rather than getting into stuff like Maintainability shouldn't be an issue, because anyone creating code that allocated memory should be thinking about where it gets deallocated anyway. |
I would highly recommend to only use shared_ptr and such. Especially in such critical places. You are somewhat forced to think about who owns a pointer and who takes care of it and its destruction. Or at least it is clear depending on the kind of pointer you choose. Something which other languages like Rust depend heavily on. And regarding performance. If you do this:
you implicitly create two surfaces. One of which is thrown away immediately. I think not using shared_ptr because of performance is the least of your problems there... |
And please, nobody take this criticism personally. I try to find the accepted way to fix this before investing too much time into the wrong solution. |
It's a philosophy, for sure - I would personally prefer Keeping It Simple (mostly because I am), and would worry that bringing in "fancy C++" just because it's (this years) best practice might make the code less approachable to more people. It would certainly feel like a wholesale adoption of But as I said, I'm also aware that this is just a philosophy too, and there's nothing to say which is right 😄 Tis the joy of open source. On the I only mentioned the performance side because obviously |
Regarding set_screen_mode:
that auto variable is a surface you get from the API. The code then creates another surface with the same parameters of the surface just created. And adds a pointer to a palette. Coming from the same first surface. Who owns that pointer? The Regarding speed and performance I found this older article: https://www.modernescpp.com/index.php/memory-and-performance-overhead-of-smart-pointer Does not seem to make much of a difference in an optimised build. But will come with the added effect of pointers getting cleaned up when there are no more objects pointing to it. And regarding fancy. Smart Pointers are exactly what I would call making modern c++ more approachable by inexperienced users. Since the memory handling is done for them by the compiler. The language feels more like a high-level programming language where memory management is taken care of. And in that special case regarding fixing pointers within the surface, normal users of the 32blit framework will not even notice and see. Except their application not crashing when loading new resources when changing levels... |
However, this is probably getting wildly off topic; my personal opinion is a preference for the simple life, but I'm sure others will have alternate opinions and it can be up to @Gadgetoid to pick which PR best fits 🤣 |
In the case of the screen There are four things
I guess it ended up this way because there are more paths that require not deleting I think someone mentioned using a "smart" pointer for this before. Don't think changing anything is possible without breaking someone's code (don't think I'm the only one with (At one point I suggested at least adding an ownership flag and I think the response was that I was more likely to cause extra breakage 🤷 ) |
To be honest I rather like the ownership flag idea, in that it's in keeping with the "light touch" API approach of letting people do it whatever way they feel comfortable with, but defaulting to doing the right(ish) thing. As for breakages; I would imagine anyone already deleting that data would understand why changes were necessary - as it stands, you're deleting memory from user code that was never allocated in user code, which is icky. |
See Pull Request #655 |
Something I just realised: since |
The memory handling when dealing with surface is asking for troubles and is in my case resulting in memory leaks which crashes the application.
There are some ifs where memory is allocated with new. But never released:
ret->palette = new Pen[256];
ret->data = new uint8_t[needed_size];
The problem is, that
palette
as well asdata
can not be freed in a (missing) Destructor. Since it is not clear if memory was allocated with a new or not when destructing the struct. In our own copy of the 32blit code and for testing reasons we introduced variables we set when anew
is used which we then check in the Destructor. That solves the memory leak. But on a maintainability and robustness perspective this is only making things worse than better.Problem is also, that good practice would mean to use shared_ptr and similar instead of raw pointers. But since surface is used in multiple ways, this will probably mean a rewrite of the whole surface.
The text was updated successfully, but these errors were encountered: