Advertisement

Memory Management

Started by June 06, 2013 11:41 PM
15 comments, last by Matt-D 11 years, 5 months ago

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.

The main problem you'll have with this is exception safety.

Another thing you should probably know: "delete nullptr;" is a safe operation. You don't need to check for nullptr before invoking delete.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Advertisement

Is there anything wrong with handling memory in this manner? Am I overlooking any potential problems with this method?

Yes. You are violating all sorts of good coding practices.

First of all, the fact that you're using a global suggests there's other serious issues with your code.

Second of all, you're requiring that CObjects know about ObjectList for no good reason. Someday you will want to make a CObject without adding it to ObjectList and then you're design is totally screwed and you'll have to redo your design anyway. Don't think that will happen, do you? Well, the entire history of software development says otherwise. Since the only change to your code to *not* do that is just


ObjectList.push_back( new CObject(0, 0, 1) ); 

I don't see what you think your code gains you. Of course the above code isn't really safe either, in the event of an exception occurring.

I'm also curious over whether CObject even needs to be dynamically allocated like that. What does CObject look like? There might be a much better solution for your needs.

Side note: You don't need to check if for the pointer being non-null. C++ as defined considers delete 0; perfectly legal and safe. It contains the check for being null already.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

The main problem you'll have with this is exception safety.

Yes, exception handling is something I did not include here. I will be sure to include it in the final implementation. Thanks for the reminder.

First of all, the fact that you're using a global suggests there's other serious issues with your code.


Second of all, you're requiring that CObjects know about ObjectList for no good reason.

A detail I forgot to mention here is that ObjectList is not just used for memory management - it's accessed by a number of different functions throughout the program. CObjects need to know about this list because they need to access other objects in the list directly. For these reasons I decided to make ObjectList within the global scope.

Someday you will want to make a CObject without adding it to ObjectList and then you're design is totally screwed and you'll have to redo your design anyway. Don't think that will happen, do you? Well, the entire history of software development says otherwise. Since the only change to your code to *not* do that is just

ObjectList.push_back( new CObject(0, 0, 1) );

Because of the way CObjects work, there won't ever be a time when I'd want to make a CObject without adding it to ObjectList (the only way CObjects are of any use is by the rest of the program accessing them through this list - CObjects never do anything on their own). However, the code you gave does make a bit more sense so I may use it.

I don't see what you think your code gains you.

I'm also curious over whether CObject even needs to be dynamically allocated like that. What does CObject look like? There might be a much better solution for your needs.

The purpose of the way I'm handling memory is to make it very easy to make CObjects within the loading phase. During the "loading" phase, there will be many calls for "new CObject()", which is why I want it to be as simple of a statement as possible. CObjects mostly contain variables (as well as a few functions for calculations) that other parts of the program need to access.

Is there anything wrong with handling memory in this manner? Am I overlooking any potential problems with this method?

Your code made me want to cry. Where did you learn to manage your object memory like this? Is this just something you came up with? I honestly want to know.

As mentioned, it violates several good coding practices.

You should minimize surprises. If I join your project and I try to do something else with a CObject, the program will probably work fine and then crash in the cleanup phase, because it will either try to delete a pointer that I already deleted, or an invalid pointer to something that was once in the stack. Then I'll waste an hour figuring it out. If I find enough of these strange things, I will stop working on your project, because they are very frustrating.

Advertisement

Instead of doing what you're doing there, just make the list hold smart pointers. That will save you from worrying about deleteing the memory and will garuntee exception safety.

Instead of doing what you're doing there, just make the list hold smart pointers. That will save you from worrying about deleteing the memory and will garuntee exception safety.


That will still leave you with a class that cannot be used in the usual ways, because its objects self-register into some global container. Making the code to register these objects marginally shorter is a bad idea if you make your code harder to understand in the process.

I would either create a fuinction that creates the new object and registers it, or I would simply write this:
ObjectList.push_back(CObject(0, 0, 1)); // ObjectList should contain objects, not pointers, unless you have a good reason to need pointers
ObjectList.push_back(CObject(0, 6, 4));
ObjectList.push_back(CObject(21, 0, 2));

Another thing you should probably know: "delete nullptr;" is a safe operation. You don't need to check for nullptr before invoking delete.

I didn't know that huh.png Thanksrolleyes.gif

Instead of doing what you're doing there, just make the list hold smart pointers. That will save you from worrying about deleteing the memory and will garuntee exception safety.


That will still leave you with a class that cannot be used in the usual ways, because its objects self-register into some global container. Making the code to register these objects marginally shorter is a bad idea if you make your code harder to understand in the process.

I would either create a fuinction that creates the new object and registers it, or I would simply write this:

ObjectList.push_back(CObject(0, 0, 1)); // ObjectList should contain objects, not pointers, unless you have a good reason to need pointers
ObjectList.push_back(CObject(0, 6, 4));
ObjectList.push_back(CObject(21, 0, 2));

I'm guessing CObject is a base class of some sort (I'm referring to the push_back solution, which would otherwise be my choice as well).

This topic is closed to new replies.

Advertisement