我有一個名爲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
遷移到ARC? – onnoweb