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

  • SEARCH
  • Home
  • 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 9157617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:04:17+00:00 2026-06-17T13:04:17+00:00

I have a question. There may be a very simple solution to this question

  • 0

I have a question. There may be a very simple solution to this question but I am not able to figure it out yet. If I use a property say @property(nonatomic, retain)UIView *mainView.

Now I synthesize it in .m file and release it in the dealloc method as following:

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

Then in my viewDidLoad, I’m allocating it and adding it as the subview of my self.view like following:

- (void) viewDidLoad {

  mainView = [[UIView alloc] init];
  .
  .
  .
  [self.view addSubView: mainView];
}

Now I understand that at this point my mainView would have 3 reference counts (one from alloc, one because it’s a retained property, and the third one when I added it to self.view), its parent controller would own it too.
Now, my question is if after adding my view to self.view, I release my mainView using

[mainView release];

My app crashes when I go back to the previous view as I am sending release to already deallocated object. Now my question is how am I overreleasing my view here. what am I missing because when I use following code it works fine and no crashes occur.

- (void) viewDidLoad {
  UIView *newView = [[UIView alloc] init];
  self.mainView = newView;
  [newView release];
  .
  .
  .
  [self.view addSubView: mainView];
}

I know why this second viewDidLoad method works but what I dont know is why first one fails, I am supposed to release my view after adding it to self.view. Right?

NOTE: I understand that in the first viewDidLoad, I can use autorelease method to release the view that is being assigned to the ivar and it wont crash but the whole point is I am trying to reduce the use of autorelease as much as possible. And I am not using ARC at all

I would really appreciate the explanation and suggestions.

  • 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-06-17T13:04:18+00:00Added an answer on June 17, 2026 at 1:04 pm

    In order for your @property to retain the mainView, you should use it as self.mainView and not just mainView. If you use the latter alone, it will not retain it. Basically if you call self.mainView = ... it is calling a setter method for mainView which does a [mainView retain]; internally. When you are directly assigning it, it wont execute this setter and retain will not be executed.

    You should try it like this,

    self.mainView = [[[UIView alloc] init] autorelease];
    [self.view addSubView:self.mainView];
    

    or as shown in your question.

    UIView *newView = [UIView alloc] init];
    self.mainView = newView;
    [newView release];
    [self.view addSubView:self.mainView];
    

    You can also try using ARC for your project. Your code will look like this in ARC,

    self.mainView = [[UIView alloc] init];
    [self.view addSubView:self.mainView];
    

    Check the documentation for more details.

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

Sidebar

Related Questions

I think this has to be a very simple question but I am not
I have a very theoretical question: Is there a way to ban the use
May be very simple question for you guys. But I just want to clear
I am new to iphone and xcode. This may be a very simple question
This seems to me like a very simple question, but I can't seem to
Sorry. This may be very simple for you... I have a Button in my
I have a job interview tomorrow and I'm trying to answer this question: There
I have a question, and I'm sure there is a relatively simple answer to
This is supposedly a very easy question, but I just can't seem to find
This is my first attempt to use WCF so there may be something fundamentally

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.