Ok, this is a tricky one, and not even easy to explain or find a suitable title for ;)
I have an object, which is managed externally. I want to create my own BlitzMax objects to wrap it. The external objects are given integer handles, so all I need is an Int field and off I go.
Now the external objects are "managed". What I mean by this is that very often you'll create a new one but an existing one will already fit the bill so it will return the int handle to this one instead of creating a new one. So if I set my BMax object's destructor to destroy it ( It's an integer handle remember, so it's not reference-counted, it has to be explicitly destroyed ) then the other BMax objects I have referencing the same external object will now point to an object which does not exist. When new external objects are created, they may even end up referencing completely different objects.
And that is no good. That'll crash heaps if you're referencing objects which don't exist.
So the sensible way to handle this is to reissue my BMax objects as well. So if the handle I get back when creating a new external object is one that's already been issued and assigned to a previous object, I don't create a new one at all, I resupply the handle to the last one. And that's great because when the BMax object is destroyed, I note that the corresponding external object is now dead, kill it, and if the handle comes through again, I'll know that it's a new external object which is just reusing the same handle.
Problem is.. this means I can't have MY BMax objects managed automatically. Because in order to give out the handles, I need to have copies of them don't I? Probably in an array, since that allows me to retrieve them quickly by looking up the external object's handle as the array index. And if I have a copy of every BMax object then simply letting the handles go out of scope or nulling them won't work, will it? Because there's always that one leftover handle I keep internally preventing the reference count hitting zero and the GC taking it all away to Junksville.
Soooo.. assuming that my description has made any sense at all ( and if it hasn't please tell me how I can clarify ) is there any possible way to set this up so that I can still have my BMax objects cleaned up automatically without explicitly destroying them, but at the same time, reissue existing BMax objects so that the external objects are not destroyed before all my BMax objects have finished with them?
I have an object, which is managed externally. I want to create my own BlitzMax objects to wrap it. The external objects are given integer handles, so all I need is an Int field and off I go.
Now the external objects are "managed". What I mean by this is that very often you'll create a new one but an existing one will already fit the bill so it will return the int handle to this one instead of creating a new one. So if I set my BMax object's destructor to destroy it ( It's an integer handle remember, so it's not reference-counted, it has to be explicitly destroyed ) then the other BMax objects I have referencing the same external object will now point to an object which does not exist. When new external objects are created, they may even end up referencing completely different objects.
And that is no good. That'll crash heaps if you're referencing objects which don't exist.
So the sensible way to handle this is to reissue my BMax objects as well. So if the handle I get back when creating a new external object is one that's already been issued and assigned to a previous object, I don't create a new one at all, I resupply the handle to the last one. And that's great because when the BMax object is destroyed, I note that the corresponding external object is now dead, kill it, and if the handle comes through again, I'll know that it's a new external object which is just reusing the same handle.
Problem is.. this means I can't have MY BMax objects managed automatically. Because in order to give out the handles, I need to have copies of them don't I? Probably in an array, since that allows me to retrieve them quickly by looking up the external object's handle as the array index. And if I have a copy of every BMax object then simply letting the handles go out of scope or nulling them won't work, will it? Because there's always that one leftover handle I keep internally preventing the reference count hitting zero and the GC taking it all away to Junksville.
Soooo.. assuming that my description has made any sense at all ( and if it hasn't please tell me how I can clarify ) is there any possible way to set this up so that I can still have my BMax objects cleaned up automatically without explicitly destroying them, but at the same time, reissue existing BMax objects so that the external objects are not destroyed before all my BMax objects have finished with them?