What is the best way to find a memory leak?
I already use smart pointers for a big part but not everywhere. However i would like to tackle this problem without using smart pointers for the purpose of learning.
I use xcode in case it matters.
In Xcode, you can use the memory leaks “Instrument” to try and detect / isolate them.
Hold Xcode’s run button and you’ll see a pop-up with options “run” “test” “profile” “analyze”. Choose “Profile”, and then “Leaks” in the next window.
You’ll have to figure out how to use the Instruments app, but the gist is that memory leaks will show up as red bars on the Leaks row, and setting the timeline playhead to the leak’s position will give you a breakdown of the responsible code. It’s probably helpful to turn on the “Invert Call Tree” and “Hide System Libraries” options.
Apple’s-Instruments-User-Guide
EDIT: More generally, it’s usually helpful to use shared pointers (i.e. ofPtr) and use RAII wherever possible, though this isn’t always practical.
Thank you so much!!!
Hold Xcode’s run button and you’ll see a pop-up with options “run” “test” “profile” “analyze”. Choose “Profile”, and then “Leaks” in the next window.
I looked at instruments before. But i had to select an .app file. So the leaks it reported weren’t helpfull. Now it pointed almost to the method causing it. Also my app runs faster now