126: Data Types: Smart Pointers Part 1.
Take Up Code - En podcast af Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Kategorier:
The C++ language guarantees that destructors run at specific times even if an exception is thrown. You can use this to make sure that other cleanup work gets done. That’s what smart pointers do. Let’s say you create a new object by calling new. You’re going to get a pointer to the new object that lives somewhere in the main heap memory. This is not a local variable on the stack. Even though you might have a local pointer variable that is on the stack, it’s where that pointer points to that’s important. When you’re done using the newly created object instance, you need to delete it. What if you forget to delete it? Or what if you have code to delete it but somebody modifies the code later and adds a new place where the current method can return without deleting the object? Or what if an exception is thrown that completely jumps out of the method into some other method higher in the call stack where there’s a catch block? In all these cases, the pointer goes out of scope and gets cleaned up from the method call stack. But the newly created object lives on in main memory. It’s now completely forgotten about by the application. This is a memory leak. One or two of these in a normal application as long as they aren’t too big will probably go unnoticed. The operating system will make sure to reclaim the memory when the application ends. But if this is a game that needs to squeeze as much performance as possible from the computer and that some user will play for hours on hours, then a few small memory leaks over time can add up and affect the game. And if this is a service application running on a server and designed to run for weeks or months at a time, then a small memory leak can eventually bring the whole server to a stop. The C++ language gets a lot of bad reputation for things like this. And it’s strange because it’s so easy to prevent. All you have to do is stop using raw pointers and start using smart pointers. Your code will for the most part not even know the difference. Your application is protected from accidental forgetfulness and from errors that cause your application to jump to another place. This episode explains more about how smart pointers work. Listen to the full episode or you can also read the full transcript below. Transcript Episode 16 explains scope and episode 19 explains C++ destructors. When you put these two concepts together, then you know exactly when your destructors will be run. You can put code in your destructors to do interesting things. Let’s say you create a new object by calling new. You’re going to get a pointer to the new object that lives somewhere in the main heap memory. This is not a local variable on the stack. Even though you might have a local pointer variable that is on the stack, it’s where that pointer points to that’s important. When you’re done using the newly created object instance, you need to delete it. What if you forget to delete it? Or what if you have code to delete it but somebody modifies the code later and adds a new place where the current method can return without deleting the object? Or what if an exception is thrown that completely jumps out of the method into some other method higher in the call stack where there’s a catch block? In all these cases, the pointer goes out of scope and gets cleaned up from the method call stack. But the newly created object lives on in main memory. It’s now completely forgotten about by the application. This is a memory leak. One or two of these in a normal application as long as they aren’t too big will probably go unnoticed. The operating system will make sure to reclaim the memory when the application ends. But if this is a game that needs to squeeze as much performance as possible from the computer and that some user will play for hours on hours, then a few small memory leaks over time can add up and affect the game.