Object scope
BlitzMax Forums/BlitzMax Programming/Object scope
| ||
Sorry if this has been asked before, but I can't seem to find a definite answer. I am working on a game that creates objects within objects, within objects, etc. However, although I know of the way the garbage collection works, I was wondering if someone could confirm that my thoughts on this are correct. With the following sample code: Type TObject1 Field a Method b() End Type Type TObject2 Field c Method New() Local o:TObject1=New TObject1 End Method Method DoStuff() 'Do something here End Method End Type Type TObject3 Method DoSomething() Local o2:TObject2=New TObject2 o2.DoStuff() End Method End Type Local o3:TObject3=New TObject3 While Not AppTerminate() o3.DoSomething() Wend Whenever o3.DoSomething is called, a new instance of TObject2 is created, and therefore a new instance of TObject1. Am I right in assuming that the garbage collection will clean up the other instances and therefore not cause a memory leak? |
| ||
Am I right in assuming that the garbage collection will clean up the other instances and therefore not cause a memory leak? Yes.As soon as any object has no more references (i.e. removed from all TLists, TMaps, no variables are set to it, etc.) it will be removed the next GC cycle. EDIT: In your case, o2 would be no-ref'd once DoSomething returns, thus deleting o2 (and thus o1). |
| ||
Thanks, I was hoping I had got it right. |