Thought i’d do some work on an old project during my ‘leisure time’, and was interested in seeing what other people did in this common scenario. [size=75]Plus, it always gives me a big headache when trying to figure out what is really safe - it’s all too easy to just assume you’re running a risk even when you may not be![/size]
When doing stuff where there’s a dynamic collection of offscreen objects represented by GUI Components, what sort of approach to people use to safely couple them? specifically, when there is every chance that an object may be removed at any moment [e.g. in a plugin, the host calls for a preset to be loaded]
The most obvious option is a simple pointer to the source data - e.g. ObjectComponent(Object* source)- and change notifications instruct it to pull the latest data. But then there’s a risk - if the component is able to set using that pointer, then it might be caught doing so whilst the source object is getting deleted.
e.g. a ‘removeObject()’ would
(A) remove the object from the collection
(B) send a ‘refresh components’ notification [destroy components and create new ones for the ones currently in the collection]
© destroy the object
but that would probably only be sure-fire safe if the ‘refresh’ call was synchronous - which isn’t really desirable for a UI update - and so this approach isn’t really particularly safe in a real app. Unless I’m just scaring myself.
So an alternative is to use reference counted objects/ptrs instead - that way if a component is caught fiddling with the source data while it’s being removed, it’ll still be there until the component is dead too. It’s a pretty straightforward solution, though I always feel uncomfortable using refcounted objects. In a purely ‘spooky’ sense [i.e. you know there’s nothing wrong but…]
The other approach in my head atm is to use IDs to refer to the objects, and have the components retrieve/set data from the model like that. That seems to be the most sensible/safe bet.
I’m just curious as to which approach people prefer to use when doing such things, or if there’s another (better) one I’ve not thought of.