views:

128

answers:

3

here is code

+ (SalesCollection*)sharedCollection {
@synchronized(self) {
    if (sharedInstance == nil) {
        [[self alloc] init]; // assignment not done here
    }
}
return sharedInstance;

}

+ (id)allocWithZone:(NSZone *)zone {
@synchronized(self) {
    if (sharedInstance == nil) {
        sharedInstance = [super allocWithZone:zone];
        return sharedInstance;  // assignment and return on first allocation
    }
}
   return nil; //on subsequent allocation attempts return nil
}
 - (id)copyWithZone:(NSZone *)zone {
     return self;
}
 - (id)retain {
  return self;
 }

- (unsigned)retainCount {
    return UINT_MAX;  //denotes an object that cannot be released
 }

- (void)release {
 /* Problem in Here */
    [myDict release];
sharedInstance = nil;
[sharedInstance release];
 }

 - (id)autorelease {
  return self;
 }

 // setup the data collection
  - init {
if (self = [super init]) {
    [self setupData];
}
return self;
  }

and here my .h file

@interface MyCollection : NSObject {
NSMutableDictionary *myDict;
}

@property (nonatomic,retain) NSMutableDictionary * myDict;
+ (MyCollection*)sharedInstance ;
- (void)setupData;

and i have one NSMutableDictionary (myDict) which contains array of object. now my problem is i want to refresh this data on button click. so i am releasing this instance in - (void)release method then try to Init again but that creates lots of leaks because may it does not release array of objects form the myDict so how to achieve this. i follow same example "TheElement" from apple to create singleton.

Thanks

A: 

You should come up with some other way to refresh the object, rather than releasing it. The whole point of a singleton is that there can be only one, and no more will ever be created. It's no clear what myDict is, but if it's an instance variable, perhaps you could add a method such as this:

- (void) refresh {
    [myDict release];
    myDict = nil;
    [self setupData];
}
Ben Gottlieb
Thanks Ben,i have added more code. myDict has 2 array which contains object of type Sale. hope this helps.
Nnp
Ben, i have tried this, but it still creates over 1000 leaks(same as before).let me know i can provide you more specific info.
Nnp
i use same function setupData in first place to get all data at that time it does not create any leaks, but it create leaks for second time.
Nnp
i am stuck here..pls help....
Nnp
+1  A: 

If you want to get rid of the code analyzer warnings then do this:

static SalesCollection gSharedSalesCollection = NULL;

+ (id) sharedCollection {
    @synchronized(self) {
        if (gSharedSalesCollection == nil) {
            gSharedSalesCollection = [[self alloc] init];
        }
    }
    return gSharedSalesCollection;
}

And have regular init and dealloc methods. That way you can use the class as a singleton (by accessing it with sharedCollection) or use it as a non-singleton in for example unit tests with regular alloc/init/release style.

St3fan
thanks St3fan, but i am not trying to get rid of that warning. Instrument shows me memory leaks when i tries to re create this instance again.
Nnp
A: 

i found a way...here is what i was doing

[self performSelectorInBackground:@selector(refreshData:) withObject:overlayView];

then i wrap up refreshData within autoreleasepool like this

NSAutoreleasePool *arPool = [[NSAutoreleasePool alloc] init];
[[MyCollection sharedCollection] refresh];
[arPool release];

and suddenly all leaks are gonna..it work like charm .....( :)))) )

Nnp