2013-07-25 36 views
0

我有一個名爲RestClient的類實現NSURLConnectionDataDelegate。在Cocos2d應用程序中釋放對象

在CCLayerColor類,菜單,(我的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發佈] 後,我沒有看到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 
+1

遷移到ARC? – onnoweb

回答

0

我假設你沒有使用ARC。如果是這樣的話:

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

在非弧我相信堅強是一樣的保留。這意味着你用init創建它(所以它需要被釋放),然後保留它的屬性。所以你需要兩個版本。

+0

我會在稍後嘗試並回復並回答。 –

+0

它適合你嗎? –

0

你不需要在使用ARC

如果你是ARC下重寫dealloc方法,你不能調用[super dealloc]調用release ,因爲它是由編譯器自動生成的

+0

我沒有使用ARC。 –