why can i not release this?

0 views
Skip to first unread message

mlecho

unread,
Nov 20, 2009, 1:19:11 AM11/20/09
to iPhone SDK Development
hi all, i know this is basic, but i have seen this time and time
again...why can i not release this view controller after adding its
view to the super view? And when is a good time to release it?
#pragma mark ApiDelegate
-(void) dataReceived:(id)data sender:(id)sender
{
UITableViewController *tbvc = [[UITableViewController alloc]
initWithStyle:UITableViewStylePlain];
[vw.view addSubview:tbvc.view];
[tbvc release]; //throws an error
}

Mike Chen

unread,
Nov 20, 2009, 4:07:34 AM11/20/09
to iphonesdkd...@googlegroups.com
The first line returns a view controller called 'tbvc' with retain
count 1. The second line adds tbvc.view to superview. It increases
tbvc.view's retain count to 2. On the third line, the view controller
itself is released. The retain count is 0 and it's deallocated. The
view controller itself should not be deallocated, unless you don't use
it any more. So you should have a property points to this view
controller and hold it until you don't need it any more.
> --
>
> You received this message because you are subscribed to the Google Groups "iPhone SDK Development" group.
> To post to this group, send email to iphonesdkd...@googlegroups.com.
> To unsubscribe from this group, send email to iphonesdkdevelop...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/iphonesdkdevelopment?hl=.
>
>
>

Anne-Lise Hassenklover

unread,
Nov 20, 2009, 1:24:57 PM11/20/09
to iphonesdkd...@googlegroups.com
You are adding the tbvc.view NOT tbvc as a subview in [vw.view addSubview:tbvc.view]; Therefore tbvc is not reference counted after the release. You need to keep the table view controller somewhere. But this entire method is weird, shouldn’t your push a new view controller (tbvc) or just use a UITableView as the subview?

- Anne-Lise



From: Mike Chen <ccp...@gmail.com>
Reply-To: <iphonesdkd...@googlegroups.com>
Date: Fri, 20 Nov 2009 17:07:34 +0800
To: <iphonesdkd...@googlegroups.com>
Subject: Re: why can i not release this?

mlecho

unread,
Nov 22, 2009, 1:44:57 AM11/22/09
to iPhone SDK Development
duh...yes....push push...omg, i can't believe i did not see that

On Nov 20, 10:24 am, Anne-Lise Hassenklover <annel...@yahoo-inc.com>
wrote:
> You are adding the tbvc.view NOT tbvc as a subview in [vw.view
> addSubview:tbvc.view]; Therefore tbvc is not reference counted after the
> release. You need to keep the table view controller somewhere. But this
> entire method is weird, shouldn¹t your push a new view controller (tbvc) or
> just use a UITableView as the subview?
>
> - Anne-Lise
>

lvcha

unread,
Nov 20, 2009, 1:35:00 AM11/20/09
to iphonesdkd...@googlegroups.com
Are you kidding me?
You have done this way is quite right,or you could run [self.tbcv
retainCount] to debug in the comsole,any problem? Please feel free to
email me.

2009/11/20 mlecho <saltle...@gmail.com>:

Ed Wrenbeck

unread,
Nov 20, 2009, 8:48:13 AM11/20/09
to iphonesdkd...@googlegroups.com
I kind of looks like what you really want to do is push the view
controller. If a view is just a subview of another view you would
typically not have a view controller for that subview.

Of course that's not a rule, just an observation from practice.


Best regards,

Ed Wrenbeck
Reply all
Reply to author
Forward
0 new messages