CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 22

Hybrid View

  1. #1
    Join Date
    May 2011
    Posts
    7

    Question Resource management--circular linked list

    I am working on code to create a type of circular linked list. I have a class CLNode for the actual nodes of the list and a second class CLPtr for pointers to the nodes. The node class is entirely private, and all access must be done through the pointers. The nodes contain a shared pointer to an integer which gives the number of "CLPtr"s that point to an element of the list. Whenever CLPtr's constructor and destructor keep this number up to date.

    If after CLPtr's destructor notices that it is the last CLPtr pointing to the list, it "delete"s the node. The node's destructor then recursively destroys the list. If the previous link is nonnull, it sets its "next" link to null, then if the next link is not null, it sets the next link's previous link to null and "delete"s it. The code for the destructor follows:

    Code:
    template <class T>
    CLNode<T,U>::~CLNode()
    {
         cout<<"Deleting node: "<<*this<<endl;
         if(links[1-direction]!=0)
         {
             cout<<"root"<<endl;
             getPrevious().links[direction]=0;
         }
         if(links[direction]!=0)
         {
             getNext().links[1-direction]=0;
             cout<<"about to delete "<<getNext()<<endl;         
             delete links[direction];
             cout<<"returning to "<<*this<<endl;
         }
         cout<<"finished "<<*this<<endl;
    
    }
    If the list is 1,2,3,4 and the pointer points to 3 it prints:

    Code:
    Destroying CLPTR.  1 CLPTRs remain for this CL list.
    Destroying CLPTR.  0 CLPTRs remain for this CL list.
    about to delete 3
    Deleting node: 3
    root
    about to delete 4
    Deleting node: 4
    about to delete 1
    Deleting node: 1
    about to delete 2
    Deleting node: 2
    finished 2
    returning to 1
    finished 1
    returning to 4
    finished 4
    ______________________
    finished 3
    At the line, a popup sometimes comes up that says "LinkedList.exe has stopped working". Any suggestions for why this happens? Is there a better way to do this?

    I'm working on a minimal working example to post.

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Resource management--circular linked list

    Provided code is not enough to say what is wrong. Generally, it is wrong design desision to delete another nodes from the node destructor. Every class must release only its own resources. Other nodes should be released by high level class (list).
    BTW, did you try to debug this?

  3. #3
    Join Date
    May 2011
    Posts
    7

    Re: Resource management--circular linked list

    The source of the error is definitely not apparent from the code that I posted. The problem that I'm having is that the source it isn't apparent at all. It's not even consistent---sometimes it causes a problem, sometimes it doesn't. From systematically removing different parts of the code, I have determined that the error comes from the end of the recursion in the destructor I posted.

    And yes, I definitely tried to debug it.

    That sounds like a good idea; I'll try it and see if it works better. Just do clarify, you are saying that the CLPtr destructor should directly "delete" each node?

    Edit: I tried making a member function in CLPtr that recursively did exactly what the code I had for the CLNode destroyer did. It didn't work any better. I also tried writing an iterative version; same problem.

    I'll post a minimal working copy of the code. At the moment, there's a lot of clutter/unrelated stuff that I have to remove.
    Last edited by hoodmane; May 5th, 2011 at 07:18 PM.

  4. #4
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Resource management--circular linked list

    Quote Originally Posted by hoodmane View Post
    Just do clarify, you are saying that the CLPtr destructor should directly "delete" each node?
    I don't know what is CLPtr. If this is kind of list class, which owns a node - yes, this is what I mean. Class should have knowledge about classes that it owns, but should not know anything about its owner and another instances of the same class (except of obvious next-previous pointers for the list node). Relation between different instances of the node class is list class responsibility.

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Resource management--circular linked list

    Make your node a dumb data structure.

    Move the logic from the node into the container.

    The container owns the nodes, so 'it' is responsible for their management.

    At best, the nodes can cleanup up their own owned data, if for example, your nodes had pointers to data.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  6. #6
    Join Date
    May 2011
    Posts
    7

    Re: Resource management--circular linked list

    @Alex F
    Yeah, CLPtr is an iterator for the list. It has a pointer to a node and a dereference operator that returns the datum contained in the node. I believe you are saying that even though the a CLPtr object only ever has a pointer to a single node, it is still the owner of all the nodes that are linked to that node. (Or rather it shares ownership with all the other CLPtr objects that point somewhere else in the same "cluster" of nodes.) So for that reason, the CLPtr code is responsible for all maintenance of the structure?

    @monarch_dodra
    Okay, I'll restructure the code in that way. In my current design, the nodes to which the data point are owned by other classes and it is not the Node's responsibility to clean them up.
    Is it okay to overload some operators in the Node class to simplify the CLPtr code?
    Do you think this will fix the problem or will it just improve the overall code design?
    Last edited by hoodmane; May 7th, 2011 at 12:18 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured