New() and Delete()
BlitzMax Forums/BlitzMax Programming/New() and Delete()
| ||
Hiya all, Something I just learned this evening is that you can see if you types are being destroyed and collected. I knew that you could add code into the New() method but you can also add code into the Delete() method too. I didn't know this! There is something on my mind though if you try the following code :- Type TestType Field x,y,z Method New() Print "New" EndMethod Method Delete() Print "Delete" EndMethod EndType Local Test:TestType = New TestType Test.x=10 Print Test.x 'comment this to see Test ISN'T collected???? Test = Null For Local x:Int = 0 To 1000 GCCollect() Next Print "Finished" Does 'Test' get optimised away if its not used elsewhere? Regardless, I didn't know about the 'Delete' method!! |
| ||
You can't tell for shure when an objectr will be collected by the GC. Therefore you should not put any critical code into delete(). The garbage collector will not free every object as soon as possible as it is often "cheaper" to keep the memory in case another object of this type is needed. |
| ||
I understand. Setting the GC to manual acts the same too. However, it would be a nice 'feature' to be able to collect manually and accurately, as well as automatically. Thanks BladeRunner. Last edited 2011 |
| ||
IIRC it's also possible to perform operations during Delete() that can cause the object to not actually be deleted. How's that for a mind f*ck ;) Steer clear I tells ya.... |
| ||
How's that for a mind f*ck ;) Well, we wouldn't want that now would we :D |
| ||
[offtopic] @matibee: what's happened to your website? It's 404ish. Was trying to download your framework for a test drive. [/offtopic] Cheers |
| ||
I find delete handy from time to time but it should be used only with the knowledge that it isn't instantly called when you're done with something, just later on when the computer is... One example I use. MiniB3D requires you to manually collect resources when you're done with them by freeing them (meshes, textures, etc.) I put those free calls into the delete method of the game object that manages the resources. e.g. I have a player object which contains health, gun damage, momentum information for a space ship, and a mesh that is the ship itself. When all references to the player object are released, some time later when the GC gets to it, it will call the delete method on the player, which will free the ship mesh, removing it from the 3D world. I don't just throw away a ship when I'm done with it because it won't be cleared when I want it to, but at the end of the match, while I'm loading a new level I can clear the ships, and load new ones letting the GC handle all the frees of the meshes along with disposing of all the other old data I no longer need from the last level. Of note, prior to this all the ships should be moved off the screen etc. so they don't display if they're not cleared by the time the new level starts (this is quite unlikely but still possible... never trust the GC!). It would be better to manually manage the resources (which I often do as well, a free method that nulls the mesh field after freeing it so that the delete method won't try to free it again...) but it's nice to have a fall back incase you miss something, or to help automate cleanup, etc. Again, use with caution, know it's limits and so forth, but it can be a time saver from time to time. |
| ||
I remember two years ago when I start my project, I was using delete() to free up memory with my game objects. It was a mistakes because the Delete() was nearly never called while changing level, creating a huge memory grow (could we say leak). Now I use a Remove() Method I call myself instead. I don't worry too much with the memory foot print on recent 2D games considering the worst machine of today comes at least with 256 Mo of free Ram. Now for VRam and mobile handled it's a different story. |
| ||
Does anyone remember the Dx9 memory leak issues? Zeke found an issue in the that texture wasn't released which would obviously cause problems. Maybe its because the Delete() method in the Dx9 driver isn't getting called at all? This method is supposed to release the texture memory and also the cpu memory used by itself. I wonder....hmmm. |