Creating Shared Named Objects
Creating Shared Named Objects
- Subject: Creating Shared Named Objects
- From: Steve Sheets <email@hidden>
- Date: Mon, 12 Jun 2006 16:12:48 -0400
Just another design question...
I want to create a custom class that reads data from inside the
bundle (XML data, strings & images). The class is complex (ie. single
object that hold other objects and collection of objects). Besides
other fields, objects of the class will be named, similar to the way
Sound and Images can be named.
Thus I want to have a creation class call that will return the object
for a given name:
+ (MyObject*) namedMyObject:(NSString*)p_name;
This object would check to see if that named object has already been
loaded in, if so, it returns the already loaded object. If not, it
loads the object into memory (if possible), and returns it. If two
locations in my code call this routine with same name, only a single
object would have been created, and returned to both instances of the
calls.
Obviously I need to store the created objects into some global
collection (array or dictionary) so that when I ask for given object
with given name, I can check previously created objects before
creating a new one.
Here is the problem. How do I handle releasing the object?
Each portion of my code will retain this created object when I need
it, and then autorelease it when that section is done. Of course, if
another section of the code is still using it, it will not be
released. However, if no other portion of the code is using the
object, the object would still be retained since the global storage
collection will still be retaining it.
Do I have to create my own garbage collection routine, that would
scan through the global collection, find named objects that only have
retain count of one (ie. only retained by the collection), and then
remove them from the collection (which would release them in this case)?
I think this would work.
Of course, when is the garbage collection routine called? Do I need
to create some NSTimer that is called ever 10 seconds or so?
Another option would be to have a special myObjectAutoRelease: method
that would autorelease the object as well as calling the garbage
collection routine. I dislike this approach because someone (ie
myself or other programmers on the project) might forget and
accidently only call autorelease, and thus leave memory behind.
Or can autorelease be overridden? (I didn't think it should be).
Or am I missing something very simple, and there is a better approach
to creating a class of named objects?
Thank you!
Steve Sheets
Midnight Mage Software
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden