Zeropointer
Zeropointer

Reputation: 510

Memory-management in iOS Programming

I have a little problem with memory management in my iOS App. I load an XML and set all Values in this XML to Spezific Objects. Now my problem is when i reload the XML every 15 - 20 reloads of this XML my app Crash on Parsing here is a sample of my parser.

EDIT: Here ist the ERROR when NSZombie is Enabled if NSZombie is disabled I didn't get an ERROR message. -[CFNumber retain]: message sent to deallocated instance

thanks for help.

EDIT:

the beginning of my Code:

 - (id)init
{
    self = [super init];
    if (self) {
        TheObjects *theObjects = [[TheObjects alloc] init];

        [self parse];
    }
    return self;
}

- (void) reload{
    reload = YES;
    TheObjects *theTmpObjects = [[TheObjects alloc] init];
     [self parse];
}
- (void)parse{

for (id xmlOBject in xmlObjects){
    MyObject *object = [[MyObject alloc] init];
    object.number1 = [NSNumber numberWithInt:1];
    object.number2 = [NSNumber numberWithInt:2];
    object.number3 = [NSNumber numberWithInt:3];

    if (reload)
        [theTmpObjects.objects addObject:object];
    else 
        [theObjects.objects addObject:object];

    [object release];
}
//later in my code

TheObjects *localTheTmpObjects = nil;
if (reload){
    localTheTmpObjects = theObjects;
    theObjects = theTmpObjects;
}

if ([delegate respondsToSelector:@selector(finished:)]){
    [delegate performSelector:@selector(finished:) withObject:theObjects];
}

if(reload)
    [localTheTmpObjects release];

}

Upvotes: 0

Views: 292

Answers (1)

Saad
Saad

Reputation: 8947

remove the line [localTheTmpObjects release]

you don't own the object

at the end, call the  `[localTheTmpObjects autorelease];`
this is because if you release array, all its objects are released and hence may cause crash, when your array may in use

    - (id)init
    {
        self = [super init];
        if (self) {
            TheObjects *obbjects = [[TheObjects alloc] init];
    theObjects = objects;
[objects releas];
            [self parse];
        }
        return self;
    }

    - (void) reload{
        reload = YES;
TheObjects *obbjects = [[TheObjects alloc] init];
    thetmpObjects = objects;
[objects releas];         [self parse];
    }
    - (void)parse{

    for (id xmlOBject in xmlObjects){
        MyObject *object = [[MyObject alloc] init];
        object.number1 = [NSNumber numberWithInt:1];
        object.number2 = [NSNumber numberWithInt:2];
        object.number3 = [NSNumber numberWithInt:3];

        if (reload)
            [theTmpObjects.objects addObject:object];
        else 
            [theObjects.objects addObject:object];

        [object release];
    }
    //later in my code

    TheObjects *localTheTmpObjects = nil;
    if (reload){
        localTheTmpObjects = theObjects;
        theObjects = theTmpObjects;
    }

    if ([delegate respondsToSelector:@selector(finished:)]){
        [delegate performSelector:@selector(finished:) withObject:theObjects];
    }


    }

Upvotes: 1

Related Questions