Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3678228
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:18:58+00:00 2026-05-19T03:18:58+00:00

After 3 hours or so, I’ve finally managed to fix a memory leak in

  • 0

After 3 hours or so, I’ve finally managed to fix a memory leak in a view controller. The leak was caused by a UIPickerView that has its property set to ‘retain’ in the header file.

The following code managed to fix it:

- (void)viewDidLoad {
    [super viewDidLoad];    
    myPicker = [[[UIPickerView alloc] initWithFrame:CGRectZero]autorelease];
}

- (void)dealloc {
    [super dealloc];
    [myPicker release];
    myPicker = nil;
}

Please don’t tell me how shocking this code is… I know it’s bad. I’ve got a release, and an autorelease. Problem is, if I change or remove any part of the above, the memory leak returns.

I though I knew how objective C memory management works, obviously not…

Why does the above code fix the memory leak, and what might a correct version of the code look like?

–

EDIT:

If anyone has the same problem, or is interested – the problem was that one of the other objects in my class was set to ‘retain’ rather than ‘assign’. (If you don’t own an object, it should have the property assign, not retain).

Like Cannondale said, removing the extra retain fixes everything, and only one release is necessary.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:18:58+00:00Added an answer on May 19, 2026 at 3:18 am

    You must be doing a retain on myPicker somewhere else in your code. Your myPicker allocation line will release that memory as soon as the stack unrolls for the viewDidLoad call (that is what the autorelease is telling it do do).

    You must be doing a retain somewhere after that point, if not then your [myPicker release] will be trying to free unallocated memory with unpredictable results.

    What you should be doing is allocating the memory in viewDidLoad (so remove the autorelease). Make sure you don’t retain the object anywhere else and release myPicker from the dealloc.

    Also … what bbum said re the dealloc 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After hours of debugging, it appears to me that in FireFox, the innerHTML of
after hours of tracking mysterious one or two seconds long lasting freeze I finally
After hours of work (I'm not a java programmer) I've managed to pack and
How do I iterate over a timespan after days, hours, weeks or months? Something
After being told by at least 10 people on SO that version control was
I'm working on a small little thing here for school. After hours of researching,
I'm no php expert (a mere beginner) but need some help! After hours searching
After years of ASP.NET development I'm actually quite surprised that I can't seem to
I recently (after hours of debugging) tracked down a segfault in an Objective-C iPad
I have a few questions regarding stored procedures. After hours of googling, I really

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.