English 中文(简体)
在下载大量图像时,记忆泄漏问题
原标题:Memory leak issue while downloading large number of images

我正试图向用户下载600多幅在屏幕上标有进展的坡道的图像。 我在展示活动和进展时,用微薄的层挡住我的屏幕。

我正在收到在坠毁之间的记忆警报信息。

我走到会堂的道路是:

  1. On app delegate, I check first core data table for all rows which is having "0" value in isImageAvailable bool field.
  2. If shows me some count (say 600), and I show and alert with YES and NO option.
  3. On YES : [self performSelector:@selector(myDownload:) withObject:nil afterDelay:0.2];
  4. 页: 1

    NSOperationQueue *queue = [NSOperationQueue new];
    // Create our NSInvocationOperation to call loadDataWithOperation, passing in nil 
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
                                                                     selector:@selector(startUpdatingRecords:) object:nil];
    
    // Add the operation to the queue 
    [queue addOperation:operation];
    [operation release];
    [queue release];
    
  5. 一开始 更新记录:

    -(void)startUpdatingRecords:(id)sender 
    {    
    [self performSelectorInBackground:@selector(updateProgressMeter:) withObject:    [NSString stringWithFormat:@"%d",self.loopStartIndex]];
    
    // Variable declarations             
    CGSize newSizeLarge ;
    NSPredicate *predicate;
    NSMutableArray *MatchingID;
    Image_DB *data;
    
    // Cache Directory path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,  YES);
    
    NSData *responseData; // = [[NSData alloc]init] ;
    
    NSURL *url = [[[NSURL alloc]init] autorelease];
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc]init] autorelease];
    UIImage *imgSelected_Large = [[[UIImage alloc]init] autorelease];
    
    // Loop through all IDs
    for (int i = 0; i < [self.arrayOfID count]; i++) //for (int i = loopStart; i < loopEnd; i++) 
    { 
    if (self.abortDownload)
    {           
        break;
    } 
    
    NSString *documentsDirectory = [[[NSString alloc] initWithFormat:@"%@",[paths objectAtIndex:0]] autorelease];
    documentsDirectory = [paths objectAtIndex:0];
    documentsDirectory = [documentsDirectory stringByAppendingFormat:@"/ImageFolder"]; // Image folder path
    
    myClass *classObj = [self.arrayOfID objectAtIndex:i];   
    
    NSString *strURl = [[[NSString alloc] initWithFormat:@"%@%@", self.MyURL,recipeObj.recipeImageStr] autorelease];
    //NSLog(@"URL = %@",strURl);           
    
    url = [NSURL URLWithString:strURl];
    request = [NSMutableURLRequest requestWithURL:url];
    responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL]; // Get Image Data into NSData
    
    //imgSelected_Large = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strURl]]];
    
    NSLog(@"Download Count = %d",i+1);
    
    if (responseData != nil)
    {
        imgSelected_Large = [UIImage imageWithData:responseData];
    
        // Resizining image
        newSizeLarge.width = 320;    
        newSizeLarge.height = 180;     
    
        imgSelected_Large = [self imageWithImage:imgSelected_Large scaledToSize:newSizeLarge]; // New sized image
        NSData *dataPhoto; // no need to release it because UIImageJPEGRepresentation gives autoreleased NSData obj.
        dataPhoto  = UIImageJPEGRepresentation(imgSelected_Large, 0.6);  // Set new image representation and its Compression Quality
    
        documentsDirectory = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"Image_%d", classObj.nodeID]];            
        [dataPhoto writeToFile:documentsDirectory atomically:YES]; //Write file to local folder at default path
    
        predicate = [NSPredicate predicateWithFormat: @"(image_ID = %d )",recipeObj.nodeID];
        MatchingID = [CoreDataAPIMethods searchObjectsInContext:@"Image_DB" :predicate :@"image_ID" :YES :self.managedObjectContext];
    
        // Setting flag variable for available image
        for (int j = 0; j< [MatchingID count]; j++)
        {
            //Assign the Authors Records in Class Object and save to Database 
            data = (Image_DB*) [MatchingID objectAtIndex:j];                
            // data.image_large = dataPhoto; // Code for storing BLOB object to DB
            data.extra_1 = @"1";
            //NSLog(@"Flag updated");
        }
    }
    // Exit out code
    if ( i == [self.arrayOfID count] - 1 || i == [self.arrayOfID count]) // Its the last record to be stored
    {    
        NSError *error;             
        if (![self.managedObjectContext save:&error])
        {
            // Handle the error...
            NSLog(@"Error in updating %@",error);
        } 
    
        self.isUpdateImageCalled = NO;
        [self performSelectorOnMainThread:@selector(removeProgressMeter) withObject:nil waitUntilDone:NO];
    
    }
    // Update UI screen while in downloading process
    [self performSelectorInBackground:@selector(updateProgressMeter:) withObject:[NSString stringWithFormat:@"%d",self.loopStartIndex+i+1]];
    }
    }
    

如果我拿不出电回击Data,那么我的照片就显示我的记忆警示和失事。 如果我公布的话,[NSConcrete MutableData]:发送去处理01x1e931de0错误的信息就会出现。

如何完善我的法典。 任何人都可以就我的守则和工作建议我,并拟订一部经过改进的法典。

请帮助我。

最佳回答

页: 1 我看不出你们的《法典》中的记忆泄漏。 页: 1 过多地利用记忆而不泄露。 3. 在您的周期中安装自动释放池:

for (...) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // your original code with a lot of autoreleased objects

    [pool release];
}

如果您在自动取款机库内填平你的代码,all 发送autorelease/code>的物体,在封面上填满时,将实际放行:这样,你就每个周期都记得。

另见Using Autorelease Collectives

问题回答

暂无回答




相关问题
How to speed up Visual Studio 2008? Add more resources?

I m using Visual Studio 2008 (with the latest service pack) I also have ReSharper 4.5 installed. ReSharper Code analysis/ scan is turned off. OS: Windows 7 Enterprise Edition It takes me a long time ...

What should I load into memory when my app loads?

I have objects I am saving to the file system using serialization. When I load the app, should I load all the objects into memory or just stubs (for searching capabilities)? If I load just stubs, ...

Data Destruction In C++

So, for class I m (constantly re-inventing the wheel) writing a bunch of standard data structures, like Linked Lists and Maps. I ve got everything working fine, sort of. Insertion and removal of ...

Java: Finding out what is using all the memory

I have a java application that runs out of memory, but I have no idea which code is allocating the memory. Is there an application with which I can check this? I use Eclipse.

View ram in DOS

Is there a way in dos (im using a dos boot disk on a linux machine) to view portions of ram? ie. some form of command to read the binary at a given address? edit: my bootable floppy doesnt have ...

does argument in printf get located in memory?

in c, when I write: printf("result %d ",72 & 184); Does "72 & 184" get a a block in memory (for example 72 takes 4 bytes, 184 takes 4 bytes?...)