Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Deleting objects allocated in other objects [modified]

Deleting objects allocated in other objects [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicsdata-structuresperformancequestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    codeprojecter_
    wrote on last edited by
    #1

    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

    J G 2 Replies Last reply
    0
    • C codeprojecter_

      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

      J Offline
      J Offline
      Jheriko
      wrote on last edited by
      #2

      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.

      C 1 Reply Last reply
      0
      • C codeprojecter_

        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

        G Offline
        G Offline
        Gary R Wheeler
        wrote on last edited by
        #3

        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;

        Fold With Us![^]

        C 1 Reply Last reply
        0
        • J Jheriko

          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.

          C Offline
          C Offline
          codeprojecter_
          wrote on last edited by
          #4

          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.

          J 1 Reply Last reply
          0
          • G Gary R Wheeler

            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;

            Fold With Us![^]

            C Offline
            C Offline
            codeprojecter_
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • C codeprojecter_

              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.

              J Offline
              J Offline
              Jheriko
              wrote on last edited by
              #6

              Its a shame that you are restricted by the library... you can at least avoid this trap in your own code though. :)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups