Here is the solution to a problem that has plagued me for a long time. Sometimes you need objects stored in some kind of global list or map because if the user loads the same path twice, you want to return the original object, or a copy of the object. But if you store it in a global list, the reference count will never reach zero and the object will never be freed without a manual free function. This allows you to use a global list with automatic deletion.
With this, GC went from being practically useless to the coolest thing ever. I eliminated the manual Free() method for almost all my classes.
It would be better if there was a ChangeRefCount() method in the base object class. Then you could make adjustments when the object changes, instead of iterating through an enumerator.
With this, GC went from being practically useless to the coolest thing ever. I eliminated the manual Free() method for almost all my classes.
It would be better if there was a ChangeRefCount() method in the base object class. Then you could make adjustments when the object changes, instead of iterating through an enumerator.
Function RefCount:Int(o:Object) Return Byte Ptr(o)[-4] EndFunction Type Texture Global map:TMap=New TMap Field index Field path$ Method New() For Local texture:TTExture=EachIn map.values() If RefCount(texture)=1 map.remove(texture.name) Next glgentextures 1,Varptr index EndMethod Method Delete() gldeletetextures 1,Varptr index EndMethod Function Load:TTexture(path$) Local texture:TTexture path=RealPath(path).tolower() texture=TTexture(map.valueforkey(path)) If texture Return texture texture=MyLoadTextureRoutine(path)'really load the texture here If Not texture Return Null texture.path=path map.insert texture.path,texture Return texture EndFunction EndType