0

I'm currently using a singleton as a data store for my app. I essentially store a number of events that are pulled and parsed from a web service and then added as needed. Each time I make a request from the web service, I parse the results and see if the items already exist. If they do, I delete them and add the updated version provided by the web service.

Everything appeared to be working properly until I fired up the Instruments panel to find out that my system is leaking the objects every time it loads them from the web service (from the second time on). The core method where things appear to be messing up is this one, which is located in my HollerStore singleton class:

- (void)addHoller: (Holler *)h
{
    //Take a holler, check to see if one like it already exists
    int i = 0;

    NSArray *theHollers = [NSArray arrayWithArray:allHollers];
    for( Holler *th in theHollers )
    {
        if( [[th hollerId]isEqualToString:[h hollerId]] )
        {
            NSLog(@"Removing holler at index %i", i);
            [allHollers removeObjectAtIndex:i];
        }
        i++;
    }
    [allHollers addObject:h];
}

Quick explanation: I decided to copy the allHollers NSMutableArray into theHollers because it's being updated asynchronously by NSURLConnection. If I update it directly, it results in a crash. As such, I switched to this model hoping to solve the problem, however the Instruments panel is telling me that my objects are leaking. All the counts are exactly the # of items I have in my data set.

From what I can tell removeObjectAtIndex isn't effectively removing the items. Would love to get the thoughts of anybody else out there on three things:

  1. Is my analysis correct that something else must be retaining the individual hollers being added?
  2. Should I be using CoreData or SQLite for storing information pulled from the web service?
  3. Do you know how long data stored in a Singleton should be available for? Until the app is killed?

Update I think I've found the source, however perhaps someone can provide some clarity on the proper way to do this. I've created a method called parseHoller which takes a dictionary object created through SBJSON and returns my own model (Holler). Here are the last couple lines:

Holler *h = [[[Holler alloc] initFromApiResponse:hollerId 
                                       creatorId:creatorId 
                                     creatorName:creatorName 
                                 creatorImageUrl:creatorImage 
                                        comments:comments 
                                       attendees:attendees 
                                          wishes:wishes 
                                        invitees:invites 
                                       createdAt:createdAt 
                                            text:text 
                                           title:title 
                                            when:when]autorelease];
//(some other autorelease stuff is here to clean up the internal method)
return h;

I figured that since I'm returning an autoreleased object, this should be fine. Do you see anything wrong with this?

Nick ONeill
  • 7,041
  • 9
  • 38
  • 56

1 Answers1

-2

Have you tried to do a retain count on the objects that is leaking? Maybe that could clear up when or where it is being retained. The code should be

[putObjectHere retainCount];

and then write to an NSLog

Hope it gives you something

Peter

Luffen
  • 413
  • 1
  • 5
  • 17
  • Yep ... that's what I'm in the process of doing ... I have a feeling that I know where the issue is but I can't seem to resolve it. Perhaps you can help? I'm pulling content via the web service and then sending it to my own method named "parseHoller" which returns an individual Holler after parsing through the JSON. I'll post the last couple lines above – Nick ONeill Aug 10 '11 at 15:50
  • This answer is absolutely, totally wrong. `retainCount` should never be used for memory debugging and leak detection under any circumstances. It is not returning what you think it's returning! See Dave DeLong's excellent answer here as to why: http://stackoverflow.com/questions/4636146/when-to-use-retaincount – lxt Aug 11 '11 at 15:38
  • I gotta be honest ... it's absurd that one person's answer has quickly been accepted as fact in this community. I've used it to track issues down. – Nick ONeill Aug 12 '11 at 20:14