List memory leak?
Community Forums/Monkey Talk/List memory leak?
| ||
Class List<T> Method Equals( lhs:Object,rhs:Object ) Return lhs=rhs End Method Clear() _head=New Node<T>() End Method Count() Local n,node:=_head._succ While node<>_head node=node._succ n+=1 Wend Return n End First, why would you create a new node when you clear a list? If I clear a list, I want it to be empty, not have a single node in it with no data. Why not use null pointers to indicate an empty list like Blitzmax? Second, if you simply create a new head node for the list when you go to clear it, where do all the old nodes go? They still reference eachother so the garbage collector should never collect them. And lastly, that count code is weird. On first glance it appears it would count the head node and return an incorrect count but then I notice that it skips the head node, then skips the node after that, then begins its count. So it will arrive at the correct count, but it does so because it skips counting the first real node. I would have used Repeat Until, and put the node incrementor after the count increment, just in case someone decides to modify it later to change the nodes in some way. Oh and what do lhs and rhs stand for? Last edited 2011 |
| ||
First, why would you create a new node when you clear a list? If I clear a list, I want it to be empty, not have a single node in it with no data. Why not use null pointers to indicate an empty list like Blitzmax? I thought that was weak as well, he could have done it like this: _head._succ = Null _head._pred = Null Second, if you simply create a new head node for the list when you go to clear it, where do all the old nodes go? They still reference eachother so the garbage collector should never collect them. according to this post from Mark: http://www.monkeycoder.co.nz/Community/post.php?topic=1269&post=11435 it's ok And lastly, that count code is weird. On first glance it appears it would count the head node and return an incorrect count but then I notice that it skips the head node, then skips the node after that, then begins its count. So it will arrive at the correct count, but it does so because it skips counting the first real node. I would have used Repeat Until, and put the node incrementor after the count increment, just in case someone decides to modify it later to change the nodes in some way. _head is used as the container for the nodes _head._succ and _head._pred are the handles of the first and last node. It makes cense to me. Oh and what do lhs and rhs stand for? I think only mark knows. |
| ||
Oh and what do lhs and rhs stand for? Left hand side, Right hand side. Here are some notes from original Amiga Exec (kernal) manual that may show origin of pattern: http://amigadev.elowar.com/read/ADCD_2.1/Libraries_Manual_guide/node02D8.html Last edited 2011 |
| ||
LHS = Left Hand Side RHS = Right Hand Side Doh! Beaten by Skid Last edited 2011 |