簡體   English   中英

在obj-c中釋放NSMutable數組

[英]release NSMutable array in obj-c

在哪里釋放/釋放我的NS可變數組a1

看到這個

- (void)viewDidLoad {

[NSThread detachNewThreadSelector:@selector(loadImage) toTarget:self withObject:nil];


}

- (void) loadImage
{

    NSLog(@" THREAD METHOD");





    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  


    NSUserDefaults *imgg = [NSUserDefaults standardUserDefaults];
    myimg= [imgg stringForKey:@"keyToimg"];
    NSLog(@"RES image sssssssss  is = %@",myimg);

    a1 = [[NSMutableArray alloc] init];
    [a1 addObjectsFromArray:[myimg componentsSeparatedByString:@"\n\t"]];



    //[a1 removeAllObjects];
    //// 
    //[myimg release];
    [pool release];


}

在第3部分的表格單元格中,我正在顯示圖像

switch(indexPath.section)
{

                                    NSString *urlE=[a1 objectAtIndex:1];
            NSLog(@"url is %@",urlE);

            NSData *backgroundData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlE]]; 


            image = [UIImage imageWithData:backgroundData];
            myImageView= [[UIImageView alloc] initWithImage:image];
            [myImageView setUserInteractionEnabled:YES]; 

            CGRect rect=CGRectMake(20 ,10, 270, 180);

            myImageView.frame = rect;
            myImageView.tag = i;    
                    [cell.contentView addSubview:myImageView];

}

並根據拍打圖像在變化

實用標記工作圖像水龍頭

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@" life count %d",[myimg retainCount]);

    NSLog(@" life  array count %d",[a1 retainCount]);     

    //NSLog(@" GITSHffffffffffffffffffffffffffffffC");
    NSUInteger sections = [indexPath section]; 
    //NSLog(@"row is %d",sections);
    if (sections  == 3)
    { //Its either 1 or 0 I don't remember, it's been a while since I did some tableview  
        if(tap<[a1 count]-1) {
            NSLog(@" life  array count %d",[a1 retainCount]);     
            tap++;

            NSString *sa=[a1 objectAtIndex:tap];
        //////////////////////
            image= [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat: sa,[a1 objectAtIndex:tap ]]]]];


            NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0)]; 


            myImageView.image = image;
            //[myimg release];
            //[a1 release];

        }
        else {
            tap=1;
            //[myimg release];
            //[a1 release];
        }



        }



        //[a1 release];


}   

所以應該在哪里釋放我的a1和myimg

代替 :

a1 = [[NSMutableArray alloc] init];
[a1 addObjectsFromArray:[myimg componentsSeparatedByString:@"\n\t"]];

考慮:

a1 = [NSMutableArray arrayWithArray:[myimg componentsSeparatedByString:@"\n\t"]];

這將使用自動釋放的NSMutableArray對象初始化a1,然后您不必擔心手動釋放它。

我不知道的是您的[pool release]是否會發布它,但是...我真的希望您不要將該業務放在后台線程中,而應該使用異步網絡方法來獲取圖像數據。

順便說一句,當我學習iPhone開發時,我經歷了關於背景網絡的三到四個“啊哈時刻”。 其中之一與在后台線程上運行選擇器有關。 持續了大約一周的時間,直到我發現ASIHttpRequest,這就是我現在的做法。 最簡單的方法將網絡交互置於后台,而不必弄亂線程或任何廢話。 參見http://allseeing-i.com/ASIHTTPRequest/

如果您查看我的答案,那么每次HTTP客戶端網絡啟動時,我都建議使用ASI。 我真的不是故意要丟人,它只是讓我的生活變得如此輕松,我想每個人都需要了解它。

使用此代碼永遠不會釋放a1。 您應該將其放在成員變量上,或者在init之后添加自動發布。

順便說一句,將myImageView添加到單元格視圖后應將其釋放。 可能由於保留/釋放邏輯:當分配myImageView時,保留計數為+1,一旦將其添加到單元格視圖中,現在計數為+2,則應釋放它,以便保留返回到+ 1,然后當單元格視圖將進一步釋放時,它將減少保留計數為0。

最后一個函數中可變圖像的邏輯相同

關於Meir assayag

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM