Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6106421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:04:39+00:00 2026-05-23T14:04:39+00:00

I am implementing an object reuse scheme using a singleton class. What I do

  • 0

I am implementing an object reuse scheme using a singleton class.

What I do basically is:

MyClass* obj = [[MyClassBank sharedBank] getReusableItem];

The bank is just an NSMutableSet tweaked for optimum reusability. When I was happily implementing this Singleton, I had in mind that I will just do the following when I am done with “obj”:

[[MyClassBank sharedBank] doneWithItem:obj];

Currently, My code would work if I where to use it this way, but I later realized that I sometimes add “obj” to an “NSCollection”, and sometimes I call:

[theCollection removeAllObjects];

At first I thought about making my own class that is composed of a collection, then I would iterate the objects within the collection and call:

[[MyClassBank sharedBank] doneWithItem:obj];

But, that’s too much of a hassle, isn’t?

A neat idea (I think) popped into my mind, which is to override: -(oneway void)release;, so, I immediately jumped to Apple’s documentation, but got stuck with the following:

You would only implement this method to define your own reference-counting scheme. Such implementations should not invoke the inherited method; that is, they should not include a release message to super.

Ao, I was reluctant to do that idea .. basically:

-(oneway void)release{
    if ([self retainCount] == 1) {
        //This will increment retain count by adding self to the collection.
        [[MyClassBank sharedBank] doneWithItem:self];
    }
    [super release];
}

Is it safe to do that?

PS: Sorry for the long post, I want the whole idea to be clear..

EDIT:

How about overriding alloc alltogther and adding [[MyClassBank sharedBank] getReusableItem]; there?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:04:39+00:00Added an answer on May 23, 2026 at 2:04 pm

    Suggested method:

    You’re playing with the reference counting system. 99.9999999999999999% of the time this is a bad idea. I would highly recommend going with a different mechanism. Perhaps these objects could implement their own reference count that’s independent of the retainCount? Then you could use that referenceCount to actually control when an object is ready to be re-used or not.

    Not suggested method:

    If, for some weird reason, you can’t do that, then you could do the following thing that is still a bad idea and that i don’t recommend you actually use:

    You can override dealloc:

    - (void)dealloc {
      [ivar release], ivar = nil;
      [anotherIvar release], anotherIvar = nil;
      somePrimitive = 0;
      // do not call [super dealloc]
    }
    
    - (void)_reallyDealloc {
      [self dealloc];  // clean up any ivars declared at this level
      [super dealloc]; // then continue on up the chain
    }
    

    Basically, the dealloc method would be the point at which the object is ready for re-use. When you’re totally done with the object and finally want it to go away, you can use the _reallyDealloc method to continue on up the chain, eventually resulting in the object getting freed.

    PLEASE don’t do this. With things like Automatic Reference Counting, this is going to introduce you into a world of hurt and really bizarre debugging scenarios. A lot of the tools and classes and stuff depend on the reference counting mechanism to be working without alteration, so screwing around with it is usually not a Good Idea™.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
I'm implementing object caching in a PHP application using APC. The issue is, that
I'm implementing an object that is almost identical to a set, but requires an
If I have an object implementing the Map interface in Java and I wish
I assume MembershipUser object and object implementing IPrincipal interface are connected in a sense
I have a basic form with controls that are databound to an object implementing
I'm implementing a datasource object for an UIScrollView. Is that part of the Controller,
When implementing a needle search of a haystack in an object-oriented way, you essentially
Currently I'm implementing a Persistant Storage Object for my Blackberry Application. It contains a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.