我有一个名为RestClient的类,它实现了NSURLConnectionDataDelegate。
在CCLayerColor类的Menu中,(我的cocos2d应用程序的菜单)我有一个RestClient类型的属性定义如下:
@property(nonatomic, strong) RestClient *rc;
在onEnterTransitionDidFinish中,我有以下代码:
[super onEnterTransitionDidFinish];
AppController *appDel = (AppController *)[[UIApplication sharedApplication] delegate];
if (appDel.FreeVersion) {
if (!self.rc) {
self.rc = [[RestClient alloc] init]; //released in dealloc
}
[rc GetMessage];
}
Menu类的dealloc如下所示:
- (void) dealloc
{
NSLog(@"Menu dealloc");
[rc release];
[super dealloc];
}
退出菜单后,我看到调用dealloc,但在[rc release] 之后,我没有看到RestClient 触发的dealloc。 知道为什么吗?
以下是RestClient类中的代码:
@implementation RestClient
-(void)GetMessage
{
NSString *lng = NSLocalizedString(@"lng",nil);
NSString *urlStr = [NSString stringWithFormat:@"http://MyLink/%@", [lng uppercaseString]];
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *req = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
[NSURLConnection connectionWithRequest:req delegate:self];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
messageData = [[NSMutableData alloc] init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[messageData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//show the message
if (!errorReceivingMessage) {
id json = [NSJSONSerialization JSONObjectWithData:messageData options:kNilOptions error:nil];
msgDict = (NSDictionary*)json;
NSString *msgText = [msgDict objectForKey:@"MessageText"];
NSString * msgTitle = [msgDict objectForKey:@"MessageTitle"];
NSString * buyButtonText = [msgDict objectForKey:@"BuyButtonText"];
NSString *cancelButtonText = [msgDict objectForKey:@"CancelButtonText"];
[messageData release];
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:msgTitle message:msgText delegate:self
cancelButtonTitle:cancelButtonText
otherButtonTitles: buyButtonText , nil];
[alert show];
[alert release];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:@"https://someLink"]];
}
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if (messageData) {
[messageData release];
}
errorReceivingMessage = YES;
}
-(void)dealloc
{
NSLog(@"Dealloc RestClient");
//[messageData release];
[super dealloc];
}
@end
答案 0 :(得分:0)
使用ARC
时无需致电release
如果您在ARC下重写dealloc
方法,则不得调用[super dealloc]
,因为它是由编译器自动生成的
答案 1 :(得分:0)
我假设您没有使用ARC。如果是这样的话:
self.rc = [[[RestClient alloc] init] autorelease]; //released in dealloc
在非弧形中,我认为强与保持相同。这意味着您使用init创建它(因此需要释放它),然后使用属性保留它。所以你需要两个版本。