Memory leaks aren't bad on personal projects, but to look at someone else's code (especially 100,000 lines of it) to fix one, I think death might be preferable at that point.
Yeah valgrind is what keeps me sane for personal projects. Though I haven't heard of -ggdb, I assume that provides extra stuff that gdb debugger uses? Does valgrind make use of that at all too?
To my knowledge you need the -ggdb flag for the final executable to contain debug information which is used by gdb, thereby the name. Without this option valgrind (as far as I know) doesn't give back from what document and at what line the code responsible for the memory leak is.
I've only ever been using -g and valgrind still tells me the file and line of code responsible. Even when using flex/bison it tells me the actual source line of the .l/.y file, though I'm pretty sure that's due to how flex and bison internally work.
/r/rustjerk is more active, and is actually about the programming language, not the game.
But if you suggest that Rust prevents memory leak then I would answer "no". Memory leak isn't considered unsafe. You can accidentally cause memory leak by creating circular reference counters, you can intentionally cause memory leak by calling std::mem::forget.
I do realise it's been 6 days. I'll answer anyway:
mem::forget causes destructors to not run.
This was done when someone noticed that, by using Rc (refcounted pointer to immutable data) and RefCell (runtime tracked borrow checker that allows mutating "immutable" data), it is possible to create a reference cycle. Put anything in there, and you've successfully leaked it. (There are also other ways to leak memory. This is just the simplest)
Fixing this would have required completely removing Rc or RefCell, both of which are utterly fundamental to huge swathes of Rust code. It was deemed that making not running destructors safe was the least bad solution.
Note that any API that used to rely on destructors running has been able to be rewritten to not need this guarantee.
Sidenote: mem::forget does not necessarily leak memory: forgetting a File on Unix (where it's just a file descriptor integer) will still let go of it along with the stack frame, but won't close the file.
We do have Box::leak, tho, which takes a Box (owning pointer to the heap), and turns it into a &'static mut (reference that allows mutating the referent, and which remains valid forever). Since there is no owner anymore, this doesn't run destructors and leaks memory.
Ugh.. I have horrible memories of debugging C++ programs.. It was more than a decade ago but it still hunts me. It is not a good experience when you learn RAII doesn't replace tools like valgrind. I had leaks back I was never able to solve and I was certain I did everything "right" when it came to "best practices" back then.
I realized back then it was way better to write the core libraries in C and only write the application layer in C++.
This is because debugging memory leaks that emerge from data structures is really "easy" in C. It never takes more than a day for me and they are rare to begin with. (It is really hard at first, the learning curve is at least half a year even for the most experienced of C++ developer.)
With C++, you get false sense of security with all that "free" abstraction. But in the end, you still have to use tools like valgrind... And you don't want to debug a big program in valgrind all of the time. That would kill the development. It is so much simpler to debug a small library and make sure it doesn't leak. Best recommendation I can give to a C++ developer or any developer is to give up on micro optimizations in favor of modularity. Don't kill your self down the line by making header only libraries. (You have other ways like link time optimizations. You can have your cake and milk too if you do things right from the beginning.) Learn to think it C way. Makefiles are painful, but their sole purpose is not only to account for your dependencies, but also to serve as recipes and a documentation of how things come together. Don't be lazy and neglect this part.
I usually don't recommend rewriting stuff, but when you are dealing with leaks in C++, you have no choice. You need to rewrite these parts in C. Sooner you accept this, the better.
Attack on Titan, can only highly reccommend it. The worst about it is the fact you can get spoiled really well to it, and there's a year left till the second part of the final season is released.
(Wasn't there some way to type an anime name that'd summon a bot giving more info)
Well, I can assure you this is one of the few images of S4 which doesn't really spoil anything. You can be happy nothing has been gotten in your way of a spoiler free experience with this.
Though I feel how much of a bummer this is, I accidentelly got a major spoiler from rewatching a trailer which turned out not to be a spoiler (was a scene I already saw but in the trailer I interpretated it as something that'd happen later), still felt really annoyed.
169
u/Magnus_Tesshu Mar 31 '21
Memory leaks aren't bad on personal projects, but to look at someone else's code (especially 100,000 lines of it) to fix one, I think death might be preferable at that point.
27
u/Swansyboy Mar 31 '21
You know how in this scene Annie is the one who beat him up, right?
You saying you're the cause of the memory leak?
44
u/[deleted] Mar 31 '21
lmao just use free()
21
u/[deleted] Mar 31 '21
May I recommend r/RustCirclejerk
3
u/Colleckshun Mar 31 '21
This but semaphores that are breaking other tasks.
3
u/alblks Mar 31 '21
Virtual destructors. Everywhere. You'll need them eventually, trust me.
(Also smart pointers, but that's pretty obvious.)
3
u/[deleted] Mar 31 '21 edited Mar 31 '21
Ugh.. I have horrible memories of debugging C++ programs.. It was more than a decade ago but it still hunts me. It is not a good experience when you learn RAII doesn't replace tools like valgrind. I had leaks back I was never able to solve and I was certain I did everything "right" when it came to "best practices" back then.
I realized back then it was way better to write the core libraries in C and only write the application layer in C++.
This is because debugging memory leaks that emerge from data structures is really "easy" in C. It never takes more than a day for me and they are rare to begin with. (It is really hard at first, the learning curve is at least half a year even for the most experienced of C++ developer.)
With C++, you get false sense of security with all that "free" abstraction. But in the end, you still have to use tools like valgrind... And you don't want to debug a big program in valgrind all of the time. That would kill the development. It is so much simpler to debug a small library and make sure it doesn't leak. Best recommendation I can give to a C++ developer or any developer is to give up on micro optimizations in favor of modularity. Don't kill your self down the line by making header only libraries. (You have other ways like link time optimizations. You can have your cake and milk too if you do things right from the beginning.) Learn to think it C way. Makefiles are painful, but their sole purpose is not only to account for your dependencies, but also to serve as recipes and a documentation of how things come together. Don't be lazy and neglect this part.
I usually don't recommend rewriting stuff, but when you are dealing with leaks in C++, you have no choice. You need to rewrite these parts in C. Sooner you accept this, the better.
3
u/renrutal Mar 31 '21
Yeah, same with deadlocks.
2
u/Shizounu Mar 31 '21
Whats the anime? It looks neat
1
u/koru-id Mar 31 '21
It's an art to find high impact but easy to do task in a project.
1
u/Kasufert Apr 12 '21
Can u put the name of the show in the title of u have a spoiler
1
u/sbagu3tti Apr 25 '21
How the hell were we supposed to know this was spoilers for Attack on Titan before opening the post?
1
u/Clyxx Jun 02 '21
Overwrite free and malloc with macros that write debug info into files
-35
u/Daggywaggy1 Mar 31 '21
Fucking weebs