 Hey everybody, this is Brian and welcome to the 107th Qt tutorial with C++ and GUI programming. So we're just going to go File New. We're going to select a Qt console application and let's just call this 107, actually let's call it Qt 107. I need to get better with my naming convention here. Just put it wherever you normally put it. All right, and we get this beautiful console application. So let's build it and sure enough, there it is. So what are we going to be talking about today? We're going to be talking about memory management with Qt. Now I shouldn't say with Qt, I should say with C++ in general because well, the ugly side of C++ is that you do have to manage your own memory. Now that can be good and that can be bad. We're going to discuss a little bit of both today. So first thing we're going to do is include Qtabug that we can see what's going on here and we're going to just say int, we're going to make a function that returns a pointer to an integer and we're just going to say int i equal new int and we're going to say 22 just because I like the number 22. Don't ask me why, I just like it. Of course we're going to return i and down here we're going to consume that function. We're going to say int z, oops sorry, into pointer to z and we're going to say this equals test. Now let's actually just throw out some Qtabugs here and we want to say i actually we want to say and of course Qtabug. You can tell it's been a while since I've done one of these, wow I can't type. I've got a whole new system, I mean not just a new keyboard, I mean a whole new computer, new headset, new mic, new speakers. I think the only thing that really still exists was the monitor. So let's run this and see what happens here and sure enough we get 22 and 22 because in test we're doing Qtabug 22 and in the main we're doing the same thing. So if we remove the star out of that, let's just take that one out, you can see the actual address and memory of where that pointer is. Remember a pointer in case you slip through this part of your C++ class, a pointer simply points to a location and memory. You have what's called the heap. Now the heap is memory. Just think of it like a big heap of hay. You just throw whatever you want on it. The more memory they have, the more places you can hide things. So when you're making a pointer to a new int, you're actually saying go out into the heap and allocate a chunk of memory. Now that chunk of memory is going to stay out there. So what's wrong with this program? I mean it worked, but what's wrong with it? Well we never deleted it. We've created these things out in the heap but we never got rid of them. So then we've got to go, all right well let's, geez we should delete it here, right? Well I can't do that because we have to return it. So wow we can't do that either. So all right well let's delete it here. That would tentatively solve the problem. But what if you've got a program that has hundreds upon hundreds if not thousands of functions across multiple modules, how are you going to track when that actually needs to be deleted? What if you introduce threads into this? Oh my, yeah you can see the complexity just goes through the roof. So what we're going to do here is we're going to talk about my friend and your friend, the Qscoped Pointer. Now what is Qscoped Pointer? It is a automatic pointer, meaning it deletes itself and you can see how from that tooltip it says dynamically allocated object that deletes its upon destruction, meaning it knows the scope of that pointer and it'll destroy it as needed. So let's actually, we're just going to comment that out and we're going to do the Qscoped Pointer way. Qscoped Pointer, int, we're going to say I, new, int, and we're going to say 23 just so we know it's different, then we're going to say Qdebug. Now we can't just say I because then we're just Qdebugging the Qscoped Pointer. We have to actually do the data here. The data is actually the dpointer inside of the Qscoped Pointer object. Now we're going to return this and let's actually comment these out and we're going to say, I'm going to show you here, let's just copy this guy, ooh, now you see the problem here, can't convert Qscoped Pointer int to int i and return. So what we can do is actually return the data, but you notice how that didn't work out well. What do we do here? What we need to do is call take. What take does is it says, okay, we're going to take control over this Qscoped Pointer, so it'll no longer be encapsulated and it's going to pass the actual value of that over. Well we still run the risk right here of having that memory leak, we haven't actually deleted it. You can see how there's the address, there's the data, that 23 is coming from this Q to bug Z, so it's still out in memory, so we need to take care of that. So what we can do here is actually fumble around with this a little bit. I love copy and paste, I'm the copy and paste king, say if we add that in, now we got the 23. So let's actually recompile this one more time, sure enough 23, 23. Now when we close it, the memory is deleted automatically. Why? Because it's encapsulated in the Qscoped Pointer. Now you may be asking, why are you doing this twice, why don't you just do this right here and then return it and then handle it down here. Well that's the thing, if you ever do any error handling, you would want to do a try catch, which we've talked about error handling before, and you're going to want to know that when that fails, it's going to go out of scope so it'll automatically delete the memory, so even if it fails it has an error, it's going to delete it because it goes out of scope. That's the magic of the Qscoped Pointer. This is Brian. I hope you found this educational and entertaining and stay tuned. I hope to make many more of these videos in the future.