Delete a type?
BlitzMax Forums/BlitzMax Beginners Area/Delete a type?
| ||
Ok...I have a type. I when I create a new instance of it I store it in a TList. Ok np. When I am done with the instance I remove it from the TList. So how do you actually delete the instance of the type? Example: Type boom Field x,y End Type BoomList:TList = New TList b:boom = new boom b.x=1 b.y=1 BoomList.Addlast b 'Now to delete it fully BoomList.Remove b Delete b As you can see the b.boom is removed from the list. But the Delete command is disabled. How do you fully delete the b.boom type? |
| ||
b=NULL |
| ||
B is removed automatically when you can't reference it any more. The way you work in a garbage collected environment is to simply forget about deleting objects - the runtime will do it for you. If b is declared in the main scope, then it will never be freed, but this is the behaviour you actually want in real programs. If that code was called in a function, then b would be deleted next time the garbage collector runs. The important thing to remember is that this is not any slower than deleting it manually (depending on the implementation - but Blitz is pretty solid) - it has no real downside. So forget about trying to 'delete' objects. Type boom Field x,y End Type Function Blah( ) BoomList:TList = New TList b:boom = new boom b.x=1 b.y=1 BoomList.Addlast b 'Now to delete it fully BoomList.Remove b End Function |
| ||
Thanks guys. |
| ||
but what's the since of this function? (I've MyList.remove(self) in a method) |
| ||
If you're talking about Michael's example function then it is just that and isn't a 'real-life' function. It's an example of creating the instance, adding it to a list, removing it from a list and, when the function finishes, the variable holding the instance pointer is out of scope so GC will reclaim the memory. I don't think it is If you create the variable within the mainloop then the variable will never go out of scope so the memory will never be reclaimed. In that case you'd need to null the variable. Hope that makes sense. |
| ||
A lot of the "move over from B3d" people dont use member functions or Methods. This is not a bad methodology in essence, just more in line with the way they programmed under b3d. If you look at Chromas post history you will see that most of his (even resent) posts are b3d posts, and so I thought it better to simply answer the question put, rather than disscuss the pros and cons of membership. Also, when it comes down to it, I much prefer haveing "Remove and Delete", in a Member function rather than a Method. (But that again is just prefferance) |
| ||
Most people using bmax are the "move over from B3d" people. And I'm sure a lot are using member functions and methods(I am). Most of my recent code has been in b3d because I'm prototyping stuff that will eventually be written in bmax once the 3d module is released. Just to shed some light on the subject. @tonyg...yep, I understand fully..thanks. |
| ||
@Chroma, I wasnt having a go at you, I was just defending the simplicity of my answer. |