Note that there are some explanatory texts on larger screens.

plurals
  1. POMemory Management
    primarykey
    data
    text
    <p>How is method removeFromSuperView: really works? I got a problem of memory bad access when I want to reinit the view</p> <pre><code>- (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]); } </code></pre> <p>I have NSLog every times there are any retain or alloc or release keyword. And the result is very weird.</p> <pre><code>//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 </code></pre> <p>The weird thing is why it didn't crash on 1st time pressed??</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload