Thread: Object destroy itself?

  1. #16
    C = C - 1
    Join Date
    Mar 2008
    Posts
    15
    Quote Originally Posted by matsp View Post
    I don't see any reason why the current design doesn't de-allocate the memory, however.
    when I try to build that code with
    Code:
    void dyn_obj::function(int & value)
    {
        value++;
    	
        if(del_now)delete this;
        else del_now = true;
    }
    -runtime error,

    and
    Code:
    void dyn_obj::function(int & value)
    {
        value++;
    	
        if(del_now)this -> ~dyn_obj();
        else del_now = true;
    }
    -works, but looking at the task manager: it doesn't deallocate the memory(objects to create set to 1 000 000, after the second call
    the list is not linked, but the memory consumption is the same)

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What runtime error do you get for the first code.

    Also de-allocating memory doesn't necessarily mean that the application's used memory goes down - it only means that it won't grow again. So using task manager to determine if memory is actually freed or not is a bad plan. Although I certainly believe that if you don't call delete it doesn't get freed - it's obvious, really.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A destructor does not deallocate memory. A destructor is merely called before the memory is deallocated or the object goes out of scope.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    C = C - 1
    Join Date
    Mar 2008
    Posts
    15
    Quote Originally Posted by Elysia View Post
    I would like to see something along the lines of this:
    Code:
    template<typename Type> class CLinkedList
    {
    public:
    	//CLinkedList(): pNext(NULL), pPrevious(NULL), pObject(NULL), pBeginOrEnd(NULL) { }
    	CLinkedList(CLinkedListBeginOrEnd<Type>* pBeginOrEnd): pNext(NULL), pPrevious(NULL), pObject(NULL), pBeginOrEnd(pBeginOrEnd) { }
    	//CLinkedList(const CLinkedList<Type, Allocator>& rCopy);
    	void InsertAfter(CLinkedList<Type>* pAfter);
    	void InsertBefore(CLinkedList<Type>* pBefore);
    	void InsertAtBeginning();
    	void InsertAtEnd();
    	void Remove();
    	//void Initialize(CLinkedListBeginOrEnd<Type, Allocator>* pBeginOrEnd);
    	//CLinkedList& operator = (const CLinkedList<Type, Allocator>& rCopy);
    	Type* pObject;
    
    private:
    	CLinkedList* pNext;
    	CLinkedList* pPrevious;
    	CLinkedListBeginOrEnd<Type>* pBeginOrEnd;
    };
    CLinkedListBeginOrEnd is optional, but is useful is you want your lists to have a head and tail pointer.
    Implementation is missing on purpose.
    Perhaps you might get an idea of what is possible via linked lists in C++...
    OK I think that's complete enought
    thanks Elysia

    I don't know very mutch about STL, how fast are the functions in it compared to a pro -made linked list, when the goal is to create,
    call and destroy a lot of objects in a short amount of time?

  5. #20
    C = C - 1
    Join Date
    Mar 2008
    Posts
    15
    Quote Originally Posted by Elysia View Post
    A destructor does not deallocate memory. A destructor is merely called before the memory is deallocated or the object goes out of scope.
    ok, so calling the destructor is a reallllly bad idea...

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by cminusminus View Post
    OK I think that's complete enought
    thanks Elysia

    I don't know very mutch about STL, how fast are the functions in it compared to a pro -made linked list, when the goal is to create,
    call and destroy a lot of objects in a short amount of time?
    The overall speed depends on the implementation, but you can be sure they are plenty fast.

    Quote Originally Posted by cminusminus View Post
    ok, so calling the destructor is a reallllly bad idea...
    Yes... it is. You should use an object after having called the destructor.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    C = C - 1
    Join Date
    Mar 2008
    Posts
    15
    Quote Originally Posted by matsp View Post
    So using task manager to determine if memory is actually freed or not is a bad plan.
    what's a good tool to do that, does the standard VC++ debugger have something (easy) for that purpose?

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not sure there is one good tool for this. I have been working on non-Windows systems for the last several years, and before that I was working on Windows graphics drivers. In most instances of that, it's a case of "custom built" memory tracker - which doesn't help you.

    A trivial test would be to create 1M objects, destroy them, then create another 1M objects - that shouldn't grow the size in task manager.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not really. A 3rd party tool may be necessary.
    I don't know what to monitor, but Process Explorer has a lot of them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Code:
    			object -> function(n);//call the object
    			if(object -> next != 0) object = object -> next;//if not last object, move to the next one
    Here is your bug. in function you use 'delete this' that means NOTHING is allowed to access the data in that object any more. You can't go object arrow anything - object is gone dead deleted destroyed erradicated eliminated erased... got it?

    The fix:
    Code:
    			dyn_obj *tempNext = object -> next;
    			object -> function(n);//call the object
    			if(tempNext != 0) object = tempNext ;//if not last object, move to the next one
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #26
    C = C - 1
    Join Date
    Mar 2008
    Posts
    15

    Lightbulb

    thanks iMalc!
    you solved my problem.
    I didn't even think about that possibility...
    Last edited by cminusminus; 03-26-2008 at 01:25 PM.

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is still a pretty poor implementation, however.
    I'd still suggest you try something like the example I made.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    C = C - 1
    Join Date
    Mar 2008
    Posts
    15
    actually I'm looking for tutorials on STL right now...

  14. #29
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Looking back over my post, I hope you realised I was being humourous... in a 'Johny 5' kind of way...

    btw regardless of whether or not you actually use 'delete this', the remembering of the next pointer before you delete the object is a technique you learn once you've written a linked list that uses non-recursive deletion. It's kind of hard to forget.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  2. Why Does The destructor destroy the original object??
    By chottachatri in forum C++ Programming
    Replies: 32
    Last Post: 03-12-2008, 08:46 AM
  3. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM