I am setting up a simple memory management in my program and I would like some feedback.
During the "loading" portion of my program I use something like this to create the dynamically loaded objects:
new CObject(0, 0, 1);
new CObject(0, 6, 4);
new CObject(21, 0, 21);
As you can see, I do not assign the address of any of these objects to pointers - I just create the objects and assign their constructor values. However, the constructor of "CObject" contains this bit of code:
ObjectList.push_back(this);
This places the address of the object in ObjectList. "ObjectLIst" is a vector of pointers for CObject, and it exists within the global scope of the program. Now during the "clean-up" phase of my program I use this to delete the allocated memory:
for(int i = 0;i < CObject::ObjectList.size();i++) {
if(!CObject::ObjectList) continue;
delete CObject::ObjectList;
}
CObject::ObjectList.clear();
I should mention that CObject is always created dynamically during the "loading" phase. It is never created anywhere else in the program.
Is there anything wrong with handling memory in this manner? Am I overlooking any potential problems with this method?
Edit: Just a note for future readers - I've realized that this is bad coding practice for the following reason: an object should not manage its own memory. That is the responsibility of the memory manager.