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 4616946
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:03:17+00:00 2026-05-22T02:03:17+00:00

How is method removeFromSuperView: really works? I got a problem of memory bad access

  • 0

How is method removeFromSuperView: really works?
I got a problem of memory bad access when I want to reinit the view

- (id)init {
   if (!(self = [super init]))
      return nil;

   _mainView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
   NSLog(@"retainCount :%d", [_mainView retainCount]);
   UIButton *reInitButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f,0.0f,90.0f,35.0f)];      
   [reInitButton addTarget:self action:@selector(buttonDidTapped:) forControlEvents:UIControlEventTouchUpInside];
   [[self view] addSubView:_mainView];
   NSLog(@"retainCount :%d", [_mainView retainCount]);
   [_mainView release];
   NSLog(@"retainCount :%d", [_mainView retainCount]);

   return self;
}

- (void)buttonDidTapped:(id)sender {
   [_mainView removeFromSuperView]; //crash during second times press the button
   NSLog(@"retainCount :%d", [_mainView retainCount]);
   _mainView = [[UIView alloc] initWithFrame[[UIScreen mainScreen] bounds]];
   [[self view] addSubView:_mainView];
   NSLog(@"retainCount :%d", [_mainView retainCount]);
   [_mainView release];
   NSLog(@"retainCount :%d", [_mainView retainCount]);
}

I have NSLog every times there are any retain or alloc or release keyword. And the result is very weird.

//init
retainCount : 1
retainCount : 2
retainCount : 1
//1st time pressed button
retainCount : 1 //remove super view didn't decrease
retainCount : 2
retainCount : 1
//2nd time pressed button
retainCount : 0 //crash. Memory bad access

The weird thing is why it didn’t crash on 1st time pressed??

  • 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-22T02:03:18+00:00Added an answer on May 22, 2026 at 2:03 am

    I think your problem is here:

    [_mainView release];
    

    You’ve dropped your reference to _mainView, and yet, by my reading, that’s a member variable that you’ll keep around and continue to invoke methods on. That’s not valid. Once you’ve called -release, you’ve essentially told the system you’re not going to use that object again, and you can’t do anything useful with a stale pointer to that object, like you do when you call -removeFromSuperView on it later.

    If you want to continue to keep _mainView around and call code on it, you need to keep a reference. Perhaps you should move the release to your object’s -dealloc method. Alternatively you could -release it in the button method and re-create a new view the next time you need to.

    As a helpful tip, a lot of programmers like to reset objects to NULL (or nil in objC-speak) after releasing them, as a reminder that you can’t use that object again. If you -release something, you’d better mean it.

    Lastly, I suggest you Google the term “reference counting” and read up on it; it’s a more generic idiom than the specifics of NSObject, and it is likely to be useful to think of the basics and how you might implement this in another language, like say, C. This will help you reason better about reference counted objects.

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

Sidebar

Related Questions

Does UIView's removeFromSuperView method remove the UIView from memory? I just want to be
i have view based application, and i want to call a method in superview(parentview)
when I call removeFromSuperview from my view.m file( [self removeFromSuperview]; ), it seems working
Method keySet() . Examples works fine, but I not sure if I'm right.
What method do you use when you want to get performance data about specific
Which method would you recommend to pass data from a child view back to
The removeFromSuperview method actually pulls off the image very quickly, is there a way
I wan't to perform an animation before quitting a view. The problem is that
I've got a UISearchBar on my UITableView and a method -finishSearching which looks like
I added a subview using myViewController *myVC = [[myViewController alloc] initWithNibName:@myView bundle:nil]; [self addSubview:myVC.view];

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.