I sometimes have trouble understanding iOS’s memory management.
I took over some app and I’m developing it. App is developed under deployment target 4.3 and it cannot be changed due to client’s requirements.
There is a method that creates Picker.
NSMutableArray *arrayToLoad = [[NSMutableArray alloc] initWithObjects: obj, nil];
[arrayToLoad addObjectsFromArray: (NSMutableArray*)[valueArrays objectAtIndex: btn.tag]];
if( !picker )
{
picker = [[WybierzZListyViewController alloc] initWithValues: arrayToLoad useObjectType: YES selectedIndex:0];
[self.view addSubview: picker.view];
picker.delegate = self;
[picker animate];
}
else
{
picker = [[WybierzZListyViewController alloc] initWithValues: arrayToLoad useObjectType: YES selectedIndex:0];
[picker reuseWithValues: arrayToLoad useObjectType: YES selectedIndex: indexes[btn.tag]];
[picker animate];
}
This else block, and especially method reuseWithValues causes me a lot of troubles and I’d most likely wipe it out.
Was the previous developer right about reusing previously created picker instead creating new one all over again? Sounds resoanable but makes few things hard for me later, and I was wondering if I could have just:
picker = [[WybierzZListyViewController alloc] initWithValues: arrayToLoad useObjectType: YES selectedIndex:0];
[self.view addSubview: picker.view];
picker.delegate = self;
[picker animate];
without else block, and avoiding memory leaks. Theoretically new ViewController should be allocated under picker variable, but what about replaced one. Should I somehow destroy it before I replace it?
Try doing like this while initializing picker
Hope this will solve your problem.