 Hey everybody, this is Brian and welcome to the 102nd Qt tutorial with C++ and GUI programming. We're going to make a console application today, we're going to be going over memory management. We'll call this memory, put it in the usual location here, next, next, next. Now I have to admit I avoided C++ for a very long time, simply because the idea of managing my own memory and pointers, the heap and the stack and all this confusing stuff just boggled me and I didn't want to deal with it. So I avoided it for a very long time. Let's create a new class while I'm chit-chatting here. And we're just going to call this test, going to inherit QObject. I mean I came from a pre.net background and I didn't really have to deal with memory. So every time I jumped into the C++ realm it just confused me and I didn't like it. But Qt makes it very easy. If you look in our class here, you'll see the constructor because we've inherited QObject. The constructor is QObject, a pointer to parent. What is that? Well, Qt has what's called a parent-child relationship with QObject, meaning every QObject can have a parent and every QObject can have children. So when you make an object and give it a parent and that parent is destroyed, all the children are destroyed too. That's one way of handling memory and we're going to demonstrate that real quick. So let's just add new and we're going to make it into the class. Let's call this child and in tests let's just include this and we're going to say child. Speaking of children, I had to bribe my cats to leave me alone so if they come in here and start whining I have to apologize in advance. And we're just going to make a pointer to a child object. Now we haven't initialized this yet. That's one thing you cannot do in a header file is to initialize an object. So what we need to do is go into, you guessed it, our implementation file. We're going to have to say mchild equal new child. Now when we do that we're creating what's called a dangling pointer and what that means is this pointer, this object, is getting created at a specific memory location and we're throwing it out on the heap, the free store and it's just going to sit there until we delete it. But as you can see from our code, we're not deleting it. So we're creating what's called a memory leak or a dangling pointer. Let's proof that. In our main we're just going to say test, test and we'll say ctest. So we are creating a test object out on the stack. You can tell it's on the stack because it's not a pointer. Anything that's not a pointer goes on the stack. And then in our test class we have a child object. This child is getting created when test is being constructed and it's going on the heap. You can tell it's going on the heap because it's a pointer. So when we create this, and let's build and run this thing, we're creating two objects. Notice how there's no error message, no nothing, just two objects are created in memory, one on the stack, one on the heap. When we close this, the one on the stack is deleted, test, but the child which we created in the heap is not deleted. And the reason for that is because we have to do our own manual memory management. Now I know some of you are going to email me and I'm just going to say this up front. Some operating systems will carve out a chunk of memory. And I think Windows 7 does this, I'm on Windows 7 right now. It will carve out a chunk of memory and create your application. Then when your application is destroyed it will wipe that memory out. That kind of flushes out some of that memory. It marks the memory as used or unused is really all it does. But memory leaks are a big cause of program crashes. They're also a very severe security risk. So to adhere to cute child-parent relationships, when we create our object we want to give it a parent. And let's just type this. This is a pointer to the current object, which is in this case test. We're creating tests, so that's the current object. So now when test is destroyed, all of its children are going to be destroyed too, regardless of whether they're on the stack of the heap. Run it again, same effect. So that's one method of memory management. Another method of memory management, let's jump back into here, cue pointer. What cue pointer does is cue pointer automatically deletes an object when it goes out of scope. Look this up in the help window. If you go out to the help, and to do that you just select cue pointer and hit F1, read about this. Basically what it's going to tell you is that it is a guarded pointer, behaves like a normal C++ pointer, except that it automatically set to zero when the reference object is destroyed, unlike normal C++ pointers, which become dangling pointers in such cases. T must be a subclass of cue object. That's a mouthful. What does all that mean? Plain English, what that means is you can create a pointer using cue pointer, and as long as the object you're creating inherits cue object, you're good. Let's actually do this real quick here. Let's just say cue pointer, and because this is a template of class, we have to actually give it an object here. We're going to say child. Let's flip back to the help here. Here's a handy little demo they give you here. Syntax here is cue pointer, and then the type, and then the name equal new. Now you notice how you don't have the asterisk in there. You don't need it, because what this is doing, this is automatically creating an object in the background and holding the pointer internally in that class. And then you treat it just like a normal pointer. So let's actually include the child class just for giggles here. Now, looking at this, you would think we just created a dangling pointer, because we're creating a new object. New is such a dangerous keyword in C++ that some companies actually forbid the use of new. Can you imagine that? Using C++ without its main feature, pointers. Other companies, you have to sign paperwork and document, I mean to the point it's ridiculous, and then you have stress testing, etc., etc., because pointers are so dangerous with memory leaks. Now, what's happening here? We can access this as if it were a child object. I mean, there's all the properties of Q object that we've inherited. And if we add properties and methods in there, they will propagate. So let's actually just go out and say public. And let's just add a little filler code here. Q to bug, void, do something, I'm going to add this in here. And we're just going to say Q to bug. So we're just going to do something. Whoops, I cannot type today. So that's really all that does. Save that, yep. Now when we grab our child class, you can see it automatically fills that in. So it truly retains your object. I mean, it's templated. And it acts like a pointer, because you can see we have this going on right there. But when this goes out of scope, this object will automatically be deleted. I should say this object will be deleted. And during its deletion, it will delete that object, because it has an internal pointer to that object. Makes more sense. So when do you use which? Actually, let's run this, just kind of show you that, hey, it works. Tick tock, tick tock, okay, yep, do something. So when do you use which method? Typically, when you are inside of a Q object, you will use the child-parent relationship. And when you are in a non-Q object, like this is just a normal main function, or if you're in a normal C++ class that is not inherent Q object, then you would use Q pointer. That will virtually eliminate all your memory management problems, just between those two things. Very simple, very easy to use, easy to understand. Now, if you're like me, one thing that you do is you do your object, and then you hit dot, because, well, I'm used to the dot-net world or Java world and everything's a dot. But you notice how Qt Creator doesn't automatically switch it. I get in the habit of just typing dot, and Qt Creator will automatically do this. So if you do that, those are the properties of the Q pointer. You notice how you don't really have a whole lot. So then you do the operator instead, and that gets you the properties of the pointer, or I should say the object you're pointing to. Very simple tutorial, thank you guys for watching. I hope you found this educational and entertaining. And if you're looking for the source code, you can go out to my website voidrealms.com, click on tutorials, and then click on Qt, and there's even a little search function here. You can click on a link, and it'll bring up the YouTube video, and if there's code associated with it, which that one doesn't have it, there'll be a little box here that says, you know, click here to download the source code. I mentioned that simply because I've gotten just an ungodly amount of email saying, hey, where do I find the source code to tutorial X? So there you go. That's all for this tutorial, talk to you later. Keep up the feedback. I am slowly, again, once again, I'm sorry, I'm really trying to get through you guys' messages.