Jump to content

  • Log In with Google      Sign In   
  • Create Account


Memory Management


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 Ludus   Members   -  Reputation: 970

Like
0Likes
Like

Posted 06 June 2013 - 05:41 PM

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[i]) continue;

        delete CObject::ObjectList[i];
    }

 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.


Edited by Ludus, 09 June 2013 - 11:03 PM.


Sponsor:

#2 ApochPiQ   Moderators   -  Reputation: 14623

Like
5Likes
Like

Posted 06 June 2013 - 06:22 PM

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.

#3 nobodynews   Crossbones+   -  Reputation: 1795

Like
0Likes
Like

Posted 06 June 2013 - 06:33 PM

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!


#4 Ludus   Members   -  Reputation: 970

Like
0Likes
Like

Posted 06 June 2013 - 10:15 PM

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.



#5 0r0d   Members   -  Reputation: 797

Like
0Likes
Like

Posted 07 June 2013 - 05:31 AM

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.



#6 Álvaro   Crossbones+   -  Reputation: 12383

Like
2Likes
Like

Posted 07 June 2013 - 06:10 AM

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.



#7 King Mir   Members   -  Reputation: 1929

Like
2Likes
Like

Posted 07 June 2013 - 06:16 AM

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.



#8 Álvaro   Crossbones+   -  Reputation: 12383

Like
1Likes
Like

Posted 07 June 2013 - 06:35 AM

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));

Edited by Álvaro, 07 June 2013 - 06:36 AM.


#9 Tournicoti   Prime Members   -  Reputation: 683

Like
0Likes
Like

Posted 07 June 2013 - 07:03 AM

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



#10 achild   Crossbones+   -  Reputation: 1616

Like
0Likes
Like

Posted 07 June 2013 - 08:14 AM

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).


Edited by achild, 07 June 2013 - 08:15 AM.


#11 Álvaro   Crossbones+   -  Reputation: 12383

Like
0Likes
Like

Posted 07 June 2013 - 09:23 AM

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).

 

I don't think so. His code with `new CObject(0, 0, 1);' doesn't allow for a derived class to be instantiated.



#12 Ludus   Members   -  Reputation: 970

Like
1Likes
Like

Posted 07 June 2013 - 03:19 PM

I decided to use the following code to add an object to the list, rather than adding it using the object's constructor:

 

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

 

Thanks for all the feedback.


Edited by Ludus, 07 June 2013 - 03:20 PM.


#13 Álvaro   Crossbones+   -  Reputation: 12383

Like
0Likes
Like

Posted 07 June 2013 - 03:31 PM

I decided to use the following code to add an object to the list, rather than adding it using the object's constructor:

 

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

 

Thanks for all the feedback.

 

Could you explain why you are not following my recommendation to make ObjectList be a container of objects instead of a container of pointers?



#14 Ludus   Members   -  Reputation: 970

Like
0Likes
Like

Posted 07 June 2013 - 03:34 PM


Could you explain why you are not following my recommendation to make ObjectList be a container of objects instead of a container of pointers?

 

In my program, a single object sometimes needs to have its address in two different lists at the same.


Edited by Ludus, 07 June 2013 - 03:34 PM.


#15 0r0d   Members   -  Reputation: 797

Like
2Likes
Like

Posted 07 June 2013 - 03:44 PM


Could you explain why you are not following my recommendation to make ObjectList be a container of objects instead of a container of pointers?

 

In my program, a single object sometimes needs to have its address in two different lists at the same.

In other words, you have to deal with ownership.  I suggest you look into smart pointers, shared_ptr and weak_ptr.



#16 Ludus   Members   -  Reputation: 970

Like
1Likes
Like

Posted 07 June 2013 - 03:51 PM

In other words, you have to deal with ownership. I suggest you look into smart pointers, shared_ptr and weak_ptr.

 

I'll look into those. Thanks.



#17 Matt-D   Crossbones+   -  Reputation: 1424

Like
1Likes
Like

Posted 08 June 2013 - 01:05 PM

In other words, you have to deal with ownership. I suggest you look into smart pointers, shared_ptr and weak_ptr.

 

I'll look into those. Thanks.

 

Just go through this: http://www.informit.com/articles/article.aspx?p=1944072

// a chapter from this, a great book, BTW: http://www.informit.com/store/c-plus-plus-primer-9780321714114

 

And then this:

http://herbsutter.com/2013/05/29/gotw-89-solution-smart-pointers/
http://herbsutter.com/2013/05/30/gotw-90-solution-factories/
http://herbsutter.com/2013/06/05/gotw-91-solution-smart-pointer-parameters/

 

Should be all you need to know to get started and do it right! :-)


Edited by Matt-D, 08 June 2013 - 01:06 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS