Cannot release UIView in cocos2d
I've got this sample code I got from somewhere around the web.
My .h file looks like:
@interface PopupWindowString : CCNode <UITextFieldDelegate> {
UIView *landscapeView;
UITextField *username;
}
and my .m file in init method has:
landscapeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
CGAffineTransform tr = landscapeView.transform;
tr = CGAffineTransformRotate(tr, (M_PI * -2.0));
landscapeView.transform = tr;
landscapeView.center = [[CCDirector sharedDirector] openGLView].center;
username = [[UITextField alloc] initWithFrame:CGRectMake(160, 100, 160, 90)];
[username setTextAlignment:UITextAlignmentCenter];
[username setDelegate:self];
[username setPlaceholder:@"New name"];
[username setTextColor:[UIColor colorWithRed:255 green:255 blue:255 alpha:1.0]];
[landscapeView addSubview:username];
[[[CCDirector sharedDirector] openGLView] addSubview:landscapeView];
everything is cool and all but when I close the popup dealloc is not called.
It seems that the r开发者_StackOverflowetainCount on landscapeView is still 1 after this code:
[username release];
[username removeFromSuperview];
[landscapeView release];
[landscapeView removeFromSuperview];
I have no clue about what to do with it... I don't have any other places I would use landscapeView, it's only in here.
I have tried subclassing UIView to try and figure out where from retain and release are called and here's the list:
Opening the popup:
- Retain call from my PopupWindowString init method
- Retain from [CALayer layoutSublayers]
- Release from CALayerLayoutIfNeeded
Closing the popup: [I hit a CCMenuItemImage]
- retain from [__NSArrayM addObject] in [UIView(Hierarchy) subviews]
- release from my code [the one I pasted previously]
- release from my code [remove from subview]
As you can see one release is missing. I think, when closing the popup, this first retain is caused by me touching the view. However it is never released.
Anyone know how to soleve this one?
Thanks, Krystian
If you are balancing all of the retains implied or directly called with releases, then your job is done.
Are you sure the object is being leaked? How have you determined that (note that retainCount
is never the right answer)?
As written, that code does not appear to leak the landscapeView
. Note that if the code that releases it is not in your dealloc
, you'll want to set landscapeView
to nil to prevent a crash when you [landscapeView release]
in dealloc
.
精彩评论