I've come accross something in my program that has me curious.
Suppose I have this scenerio:
Class 2 creates an instance of class 1. class 1 creates and manages an instance of DataClass, as does class 2. When class2 is created, class1->data is set to be a copy of class2->data;Code:class CLASS_1{ public: DataClass *data; CLASS_1(){ data = new DataClass(); } ~CLASS_1(){ delete data; data = NULL; } }; class CLASS_2{ public: DataClass *data; CLASS_1 *class1; CLASS_2(){ this->data = new DataClass(); class1 = new CLASS_1(); class1->data = this->data; } ~CLASS_2(){ delete data; data = NULL; delete class1; class1 = NULL; } };
So, my understanding is that there will be memory allocated for CLASS_1, CLASS_2, and two instances of DataClass contained in these classes. Both instances of DataClass will be the same.
Now, if I delete class1 before class2's deconstructor, is it's memory freed? In my program, class2 would be the main window, and class1 would be a child window. So, if I close the child window before the main window, I still want to free it's resources. DataClass of each window is made the same, so that the main window can keep a "master copy", and pass it to each child window as necessary.
What I've noticed though, by monitoring the applications memory usage in the task manager, is that when I close the child window, the applications memory usage doesn't go down. What i'm wondering then, is if the memory isn't being freed like I assumed it should be, perhaps due to the copying (the data is made equal at other points at runtime too, not just at initialization)?



LinkBack URL
About LinkBacks



Look at CLASS_2's constructor. 