0

Hi guys can somebody please advise how to cure the memory leaks in the code below

i've tried just about every combination of release and autorelease i can think of but every time either the app crashes or the leak remains

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

//get refereance to the textfield
UITextField *currentTextField = (UITextField*)[self.view viewWithTag:200];

//check which picker
if(pickerView.tag ==1)
{   
    // Only calls the following code if component "0" has changed.
    if (component == 0) {

        // Sets the global integer "component0Row" to the currently selected row of component "0"
        component0Row  = row;

        // Loads the new values for the selector into a new array in order to reload the data.
    newValues = [[NSMutableArray alloc] initWithArray:[pickerData objectForKey:[selectorKeys objectAtIndex:component0Row]]];
        currentValues = newValues;


        // Reloads the data of component "1".
        [pickerView reloadComponent:1];


    }

    //run the selector logic
    [self textFieldDidEndEditing:currentTextField];

}

hope someone can advise

many thanks

3 Answers 3

2

Your problem is these two lines:

newValues = [[NSMutableArray alloc] initWithArray:[pickerData objectForKey:[selectorKeys objectAtIndex:component0Row]]];
currentValues = newValues;

The first line allocated a new instance of NSMutableArray. The second line copies the pointer from newValues to currentValues, overwriting the pointer value in currentValues. Whatever currentValues was pointing to is lost. That's the leak.

You could fix it like this:

newValues = [[NSMutableArray alloc] init...
[currentValues release];
currentValues = newValues;

This way, whatever was pointed to by currentValues has its reference count decremented before you lose access to it.

You could also solve the problem by making currentValues an Objective-C property, and using the accessor methods via self.currentValues or [self setCurrentValues:]; those methods will handle retain/release for you.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Benzado thanks for your suggestions but i have tired this release before,and just now again and the app crashes with exc-bad-access when i scroll through the picker it driving me crazy at the moment
You asked about a leak. These answers will plug your leak. The crash is another problem. Google for instructions and put a breakpoint on objc_exception_throw so you can find out what the bad pointer is. Then ask a new question if you can't figure that out.
the crash happens if i adopt the code you suggested to plug the leak is it likely that the plugged leak can create a crash in another part of the code? i don't really understand this so hope you ca elaborate and explain thanks
0

Your NSMutableArray allocation is never released.

newValues = [[NSMutableArray alloc] initWithArray:[pickerData objectForKey:[selectorKeys objectAtIndex:component0Row]]];

You should autorelease that or release it later on when you know you don't need it anymore.

1 Comment

Hi Alex i already tried autorelease but the app crashes i have release in the dealloc but thats not stopping instruments from finding the leak after
0

Not sure how you have currentValues defined, but this should work without leaks:

In your .h file:

@property (nonatomic, retain) NSArray * currentValues;

In your .m file:

@synthesize currentValues;

self.currentValues = newValues;

4 Comments

Hi perception currentValues is allocated in viewdidload like this currentValues = [[NSMutableArray alloc] initWithArray:[pickerData objectForKey:[selectorKeys objectAtIndex:component0Row]]];
@superllanboy - is currentValues a property of your view or just a variable you declare in the function? If its the latter then you found your leak.
hi perception currentvalues is a property
Ok then your leak is most likely happening because you are using current values as an ivar (whenever you replace its value you are leaking the old object that was in its place). Instead of doing currentValue = someobj, do self.currentValue = someobj.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.