Why is Self=Null wrong?

BlitzMax Forums/BlitzMax Beginners Area/Why is Self=Null wrong?

Gabriel(Posted 2005) [#1]
My first day of actually trying to learn BlitzMax after owning it and not using it for six months. ( Translation: Expect many more stupid questions soon. )

I have a type with a create function and delete method. In the delete method, I nullify the instance with Self=Null but the compiler won't have it. Am I not supposed to assign anything to Self? What's the preferred way then?


Perturbatio(Posted 2005) [#2]
self is not a var and thus cannot be assigned.

try:
function Destroy(todestroy:TMyType)
    todestroy = null
    flushmem
End Function



N(Posted 2005) [#3]
Pert: Don't you mean..

Function Destroy( Destroyable:Object Var )
     Destroyable = Null
     FlushMem
End Function


Which is rather weird, because you'd be better off just setting the variable to Null. But then, can you pass Self to a Object Var that nullifies it?

To the testing grounds!


N(Posted 2005) [#4]
Nope, you can't.


Gabriel(Posted 2005) [#5]
That works, thanks. It still seems wrong that you can't destroy a type instance from a method.

Cube.Free()


makes much more sense than

MeshType.Free(Cube)



Bot Builder(Posted 2005) [#6]
setting it to null doesn't actually free it though.

All null does is set the handle to null

Once the object has no handles, it is destroyed by flushmem. This way you cannot have memory problems by trying to access something that's been freed. In many cases an object will only have one handle. this is why "=null" works. Handles can also go out of scope (especially in strict mode). Just dont worry about it, set all handles you wont need afterwords to null and flushmem occasionally. The only time you really have to worry about it is with cyclic types like TList...


Gabriel(Posted 2005) [#7]
I know all that. What I don't get is why we have to go the long and ugly route of doing it with a static function and passing the instance when a method could null-ify it-self. Just seems like that would be the cleaner syntax.


Bot Builder(Posted 2005) [#8]
uh, if you understood all that you would know that nothign can null-ify a type. you can only nullify its handles.


Jay Kyburz(Posted 2005) [#9]
so umm. in my game, all i am doing is removing the instance of my type from a list of said type so that is no longer refrenced by anything. Is this freeing its memory? (i have freemem in my main loop.


Methond New()
myList.addlast self
End Method

Method Free()
mylist.remove self
End Method

oh yeah.. and how do you do the code thing


Gabriel(Posted 2005) [#10]
uh, if you understood all that you would know that nothign can null-ify a type. you can only nullify its handles.


And if Self is the handle you used when calling the method, that's exactly how the method should see it. The handle I passed should be nullified.


Yan(Posted 2005) [#11]
@Hakea - BlitzWiki to the rescue...

Oh, and...What are the forum codes?


N(Posted 2005) [#12]
[code]
Type MyEntity
Global _list:TList
Field _link:TLink

Method New( )
_link = _list.AddLast( Self )
End Method

' Of course, you also have to nullify your reference to it then call FlushMem. But this gives you a chance to free any other resources.
Method Dispose( )
_link.Remove( )
End Method
End Type