Why is Self=Null wrong?
BlitzMax Forums/BlitzMax Beginners Area/Why is Self=Null wrong?
| ||
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? |
| ||
self is not a var and thus cannot be assigned. try: function Destroy(todestroy:TMyType) todestroy = null flushmem End Function |
| ||
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! |
| ||
Nope, you can't. |
| ||
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) |
| ||
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... |
| ||
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. |
| ||
uh, if you understood all that you would know that nothign can null-ify a type. you can only nullify its handles. |
| ||
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 |
| ||
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. |
| ||
@Hakea - BlitzWiki to the rescue... Oh, and...What are the forum codes? |
| ||
[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 |