0

我有一个名为 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
4

2 回答 2

0

release使用 ARC 时无需调用

如果你dealloc在 ARC 下重写方法,你不能调用[super dealloc],因为它是由编译器自动生成的

于 2013-07-25T22:02:59.380 回答
0

我假设您没有使用ARC。如果是这样,请执行以下操作:

self.rc = [[[RestClient alloc] init] autorelease]; //released in dealloc

在非弧中,我相信强与保留相同。这意味着您正在使用 init 创建它(因此需要将其释放),然后将其与属性一起保留。所以你需要两个版本。

于 2013-07-25T23:20:45.663 回答