Deleting objects allocated in other objects [modified]
-
Hi, I use a c++ class library. All of the class are derived from one base class (ex 'CObject') Some class's methods return a pointer(allocated using 'new') to class objects. So i have to delete those objects. (its hard because of heavy usage of such classes/methods all over the code) and i have a plan to delete all pointers when app close using a pointer array. // File : cobject.h std::vector vecObjPtr; class CObject { CObject::CObject() { vecObjPtr.push_back(this); } }; // File : keyboard.h class Keyboard: public CObject { Keys* getKeys() { return new Keys(); } }; // File : keys.h class Keys : public CObject { Key* getKey(int value) { return new Key(value); } }; // File : key.h class Key : class CObject { Key(int value) { //... } }; #include #include ... ... void main() { Keyboard *keybrd = new Keyboard(); Key *key = keybrd->getKeys()->getKey(10); // done delete the pointer returned by getKeys,getKey and keybrd by poping items from vecObjPtr and delete; } is it a right way to do or is there any standard way of freeing memory like this ? thanks in advance -- modified at 11:36 Sunday 18th November, 2007
-
Hi, I use a c++ class library. All of the class are derived from one base class (ex 'CObject') Some class's methods return a pointer(allocated using 'new') to class objects. So i have to delete those objects. (its hard because of heavy usage of such classes/methods all over the code) and i have a plan to delete all pointers when app close using a pointer array. // File : cobject.h std::vector vecObjPtr; class CObject { CObject::CObject() { vecObjPtr.push_back(this); } }; // File : keyboard.h class Keyboard: public CObject { Keys* getKeys() { return new Keys(); } }; // File : keys.h class Keys : public CObject { Key* getKey(int value) { return new Key(value); } }; // File : key.h class Key : class CObject { Key(int value) { //... } }; #include #include ... ... void main() { Keyboard *keybrd = new Keyboard(); Key *key = keybrd->getKeys()->getKey(10); // done delete the pointer returned by getKeys,getKey and keybrd by poping items from vecObjPtr and delete; } is it a right way to do or is there any standard way of freeing memory like this ? thanks in advance -- modified at 11:36 Sunday 18th November, 2007
Why do you need a pointer? e.g. if Key has a constructor Key(int) then instead of "return new Key(10)" with return type Key* you should be doing "return Key(10)" with return type Key. This way the compiler handles the allocations and frees for you. The only time I ended up keeping track of pointers as you described is when generating strings for an object, and only that one time because I didn't think hard enough about a solution. You can always avoid these situations with better design.
-
Hi, I use a c++ class library. All of the class are derived from one base class (ex 'CObject') Some class's methods return a pointer(allocated using 'new') to class objects. So i have to delete those objects. (its hard because of heavy usage of such classes/methods all over the code) and i have a plan to delete all pointers when app close using a pointer array. // File : cobject.h std::vector vecObjPtr; class CObject { CObject::CObject() { vecObjPtr.push_back(this); } }; // File : keyboard.h class Keyboard: public CObject { Keys* getKeys() { return new Keys(); } }; // File : keys.h class Keys : public CObject { Key* getKey(int value) { return new Key(value); } }; // File : key.h class Key : class CObject { Key(int value) { //... } }; #include #include ... ... void main() { Keyboard *keybrd = new Keyboard(); Key *key = keybrd->getKeys()->getKey(10); // done delete the pointer returned by getKeys,getKey and keybrd by poping items from vecObjPtr and delete; } is it a right way to do or is there any standard way of freeing memory like this ? thanks in advance -- modified at 11:36 Sunday 18th November, 2007
As a general rule, it's better to delete objects deliberately as soon as they are no longer needed. The mechanism you have described will continuously use more memory as the program runs. The longer it runs, the more memory will be consumed. You could eventually exhaust system resources, which is poor behavior ;). I can see circumstances where your approach might be useful, however. Make sure that you mark your destructors
virtual
, and write a destructor for each class derived from your base object.
Software Zen:
delete this;
-
Why do you need a pointer? e.g. if Key has a constructor Key(int) then instead of "return new Key(10)" with return type Key* you should be doing "return Key(10)" with return type Key. This way the compiler handles the allocations and frees for you. The only time I ended up keeping track of pointers as you described is when generating strings for an object, and only that one time because I didn't think hard enough about a solution. You can always avoid these situations with better design.
Thanks Jheriko++, The c++ library i use returns the allocated pointers and expect from the user of the library to delete these objects. The library has many such methods that returns newly allocated pointers so i cant change it. every getSomethingX returns a new pointer getSomething1->getSomething2->getSomething3->getSomething4.... for the flexibility reason i write code like the above otherwise i have to declare new pointer variable for every getSomethingX returned pointer so i can delete it later.
-
As a general rule, it's better to delete objects deliberately as soon as they are no longer needed. The mechanism you have described will continuously use more memory as the program runs. The longer it runs, the more memory will be consumed. You could eventually exhaust system resources, which is poor behavior ;). I can see circumstances where your approach might be useful, however. Make sure that you mark your destructors
virtual
, and write a destructor for each class derived from your base object.
Software Zen:
delete this;
thanks Gary,
Gary R. Wheeler wrote:
As a general rule, it's better to delete objects deliberately as soon as they are no longer needed. The mechanism you have described will continuously use more memory as the program runs. The longer it runs, the more memory will be consumed. You could eventually exhaust system resources, which is poor behavior
Thats true i will declare pointer variables for newly returned pointers and delete them when they are no longer needed.
-
Thanks Jheriko++, The c++ library i use returns the allocated pointers and expect from the user of the library to delete these objects. The library has many such methods that returns newly allocated pointers so i cant change it. every getSomethingX returns a new pointer getSomething1->getSomething2->getSomething3->getSomething4.... for the flexibility reason i write code like the above otherwise i have to declare new pointer variable for every getSomethingX returned pointer so i can delete it later.