Linked List Remove problem
BlitzMax Forums/BlitzMax Beginners Area/Linked List Remove problem
| ||
I am having some problems removing objects from the linked lists, and after a number of attempts I am going to get your help on this.Global zObjectList:TList = CreateList() Type zObject Field x Field y Method New() zObjectList.AddLast(Self) End Method Function create:zObject( x, y ) Local obj:zObject = New zObject obj.x = x obj.y = y Return obj End Function End Type z1:zObject = zObject.create( 10, 10 ) z2:zObject = zObject.create( 20, 20 ) z3:zObject = zObject.create( 30, 30 ) z4:zObject = zObject.create( 40, 40 ) z5:zObject = zObject.create( 50, 50 ) Print z1.x + ","+z1.y Print z2.x + ","+z2.y Print z3.x + ","+z3.y Print z4.x + ","+z4.y Print z5.x + ","+z5.y WaitKey In the above code, how would you remove, lets say, object z3 ? Also, should I be creating the objects differently? |
| ||
zObjectList.Remove(z3) z3=Null FlushMem Not quite sure if that will work, but give it a shot. I generally create the list within the Create function, but first check if the list already exists... If zObjectList=Null Then zObjectList=CreateList() I've not seen your way of doing the creation before, but it looks neat and I can't think why it wouldn't work a treat. |
| ||
Global zObjectList:TList = CreateList() Type zObject Field x Field y Method New() zObjectList.AddLast(Self) End Method Method remove() ListRemove(zObjectList,Self) FlushMem End Method Function create:zObject( x, y ) Local obj:zObject = New zObject obj.x = x obj.y = y Return obj End Function End Type z1:zObject = zObject.create( 10, 10 ) z2:zObject = zObject.create( 20, 20 ) z3:zObject = zObject.create( 30, 30 ) z4:zObject = zObject.create( 40, 40 ) z5:zObject = zObject.create( 50, 50 ) z4.remove Print "Here are the Zobjects referenced outside of ny list" Print z1.x + ","+z1.y Print z2.x + ","+z2.y Print z3.x + ","+z3.y Print z4.x + ","+z4.y Print z5.x + ","+z5.y Print "here are the items in the zObjectList" For n:zObject = EachIn zObjectList Print n.x +","+ n.y Next z4 = Null FlushMem Print z1.x + ","+z1.y Print z2.x + ","+z2.y Print z3.x + ","+z3.y Print z4.x + ","+z4.y Print z5.x + ","+z5.y WaitKey |
| ||
Does this help any... may be way off for what you are looking forGlobal zObjectList:TList = CreateList() Type zObject Field Name:String Field x Field y Method New() zObjectList.AddLast(Self) End Method Method remove() ListRemove(zObjectList,Self) FlushMem End Method Function create:zObject(Name:String, x, y ) Local obj:zObject = New zObject obj.name = name obj.x = x obj.y = y Return obj End Function Function zdelete:zObject(Name:String) For n:zObject = EachIn zObjectList If n.name = Name zObjectList.Remove(n) 'Return True EndIf Next End Function End Type zObject.create( "Z1", 10, 10 ) zObject.create( "Z2", 20, 20 ) zObject.create( "Z3", 30, 30 ) zObject.create( "Z4", 40, 40 ) zObject.create( "Z5", 50, 50 ) Print "here are the items in the zObjectList" For n:zObject = EachIn zObjectList Print n.name +":"+n.x +","+ n.y Next zObject.zDelete("Z4") For n:zObject = EachIn zObjectList Print n.name +":"+n.x +","+ n.y Next |
| ||
I see always varxyz=Null but are some other people here, they see, that here is a problem with this???? Sample: 1) i create window-object (window:Twindow) and i add this objekt to window list 2) i create button-object (button:Twindow) and i add this object to button-list 3) i click to close - and now i call window.close()-function how can my program delete all objects in this window without mem-leaks??? I don't see it. If i remove this objects from lists - how can my programm set all variables to null???????? and if i have 100 gadgets in my window should i do this: window.close() 'delete win+gadgets from lists - no problem and than: button1=null button2=null button3=null button4=null button5=null button6=null button7=null button8=null button9=null ... button999=null yes - i could create a array for this - but this is bad, if i have other variable names: button_ok button_cancel button_back button_stop checkbox_blub tabber_main ... or should i use a array with constants: gadget[button_ok] gadget[button_stop] ... but oh my god, i have so large names! i think BBmax-types are bad - BB2D/2D/Plus-Types was fantastic!!! |
| ||
"i think BBmax-types are bad - BB2D/2D/Plus-Types was fantastic!!!" i thought that at first as well, but as soon as you get to grips with them you realise how powerful they are |
| ||
powerful yes - but very hard to delete |
| ||
@ Bill, I got it working by using your example. The reason it didn't really work on the main code I was working with, is that the type had fields that were references to other type lists, and the garbage collector doesn't remove the type then, so I had to null out those fields and then it works. Thanks for helping out. |
| ||
powerful yes - but very hard to delete ummm... are they? |
| ||
how can my program delete all objects in this window without mem-leaks??? I don't see it. Use a Composite Pattern. That's the main problem it attacks. When you need to treat a collection of objects as a single object, use the Composite Pattern. |
| ||
@Perturbatio: you do not understand. BBmax newer delete somethink, if you set a reference to a type-object in other variables Local test1:Tmytype=New Tmytype Local test2:Tmytype=test1 test1=NULL FlushMem and if you forgot test2 - you do not delete this object... ok - but what, if you create somewhere lists and you forget this list The problem is other: In classic BB: DELETE - if you call it - you delete your object in this time (ok memory was not reallocated, but its ok - you have no access to this, and new object take the memory from deleted object - and all other variables that linked to this type have value NULL - nice!!!!!!!!) In BBmax: var=NULL - you don't know. do you remove it or not. you have only a chance with user definded delete-functions, that remove objects from lists. after you call FlushMem you don't know what obj. was deleted or not-you have no guarantie for this. Other variables can link to a object (<>NULL) and bbmax don't kill this objects... this is abit harder to write applications. if you create list and newer reference obj. in other variables - you have good clean programs (partikel effects). if you create gui - where you use returned object - you have unclean sources (postet on top)... you have only a chance using ID-number or something... |