removeFromSuperview后释放UIView崩溃的应用程序

我有一个UIView对象,我添加它作为一个子视图它使用[self.view addSubview:mySubView]; 。 之后我打电话给[mySubView removeFromSuperview]; 然后我尝试做[mySubView release]; 但应用程序崩溃在这里。

RemoveFromSuperview是否RemoveFromSuperview调用release

请张贴您的代码,否则难以回答

崩溃代码

 UIView *subview = [[UIView alloc] init]; //manual allocation [self.view addSubView:subview]; //view retained automatically [subview release]; //released the manual allocated memory for(UIView *subview in [scrollView subviews]) { [subview removeFromSuperview]; //released the view retained automatically. Now retain count reach 0/ [subview release]; // crash..... } 

代码没有崩溃

 UIView *subview = [[UIView alloc] init]; //manual allocation [self.view addSubView:subview]; //view retained automatically [subview removeFromSuperview]; //released the view retained automatically. Now retain count reach 1 [subview release]; // No crash .retain count reach 0 

你只是不发布你没有明确分配或保留自己的东西。

是的, removeFromSuperview释放视图。

你检查了文件吗? 会花费大约5秒钟:

讨论
如果接收者的超级视图不nil ,则超级视图释放接收者。 如果您打算重新使用视图,请确保在调用此方法之前retain该视图,并在适当的时候再次release视图。

是的,superview保留所有的子视图,所以当你删除你的视图时,它的超级视图将会释放它。