UIImageJPEGRepresentation

16,696

Now you are using only one imageData object in your cycle. So, you always create JPEG representation without deleting. Try this code:

for(id file in files)
{
    if(file!=nil)
    {
        QueuedObjectData* imageData = [[QueuedObjectData alloc] init];
        [imageData setData:UIImageJPEGRepresentation(file, 0.8)];
        [imageData setFilename:[NSString stringWithFormat:@"image%d.jpg",i]];
        [imageData setContentType:@"image/jpeg"];
        [imageData setKeyValue:@"image"];
        [queuedObject addData:imageData];
        [imageData release];
        i++;
    }
}

Here you create object, use it and then delete.

Share:
16,696
Manuel
Author by

Manuel

Updated on June 04, 2022

Comments

  • Manuel
    Manuel almost 2 years

    I have troubles finding a memory leak which then again causes my App to crash. It seems that the memory allocated for the JPEGRepresentation does not get released. This is even more curious because the NSData Object created by UIImageJPEGRepresentation is only about 300 kb big (dependend on the image), but the memory usage jumps up by about 3-5 megabyte per image at this stage.

    This is the code

    QueuedObject* queuedObject = [[QueuedObject alloc] init];
    [queuedObject setUrl:url];
    
    QueuedObjectData* jsonQueuedData = [[QueuedObjectData alloc] init];
    [jsonQueuedData setData:jsonData];
    [jsonQueuedData setFilename:@"message.json"];
    [jsonQueuedData setContentType:@"application/json"];
    [jsonQueuedData setKeyValue:@"JSONMessage"];
    [queuedObject addData:jsonQueuedData];
    
    int i=1;
    
    QueuedObjectData* imageData = [[QueuedObjectData alloc] init];
    
    for(id file in files)
    {
        if(file!=nil)
        {
            [imageData setData:UIImageJPEGRepresentation(file, 0.8)];
            [imageData setFilename:[NSString stringWithFormat:@"image%d.jpg",i]];
            [imageData setContentType:@"image/jpeg"];
            [imageData setKeyValue:@"image"];
            [queuedObject addData:imageData];
            i++;
        }
    }
    
    [[UploadQueue sharedInstance] addObject:queuedObject];
    
    
    [jsonQueuedData release];
    [jsonData release];
    [url release];
    [imageData release];
    [queuedObject release];
    

    Maybe you have an idea to help me

    best regards Manuel

  • Manuel
    Manuel over 12 years
    This is actually the way I had it before and then changed it while tracking down the issue. The curious thing is that Instruments does not show up any leaks, but then suddenly the app crashes after Memory Low Warning Level 1 - without any other information in the debugger.
  • beryllium
    beryllium over 12 years
    But it this approach wrong? Do you have any problems with this code?
  • Manuel
    Manuel over 12 years
    Sorry for the confusion. The code seems to be right, but in Instruments (Activity View - Memory Usage) I still see lots and lots of Memory beeing used by the App. Is there a way I can find out where the memory is beeing used at? If i look in the Instruments Allocation tool there is nothing even close to the amount beeing used here ;/ (I am speaking about 10+ megabytes for adding 3 pictures (which are only about 300 kb each!) to my queuedObject object)
  • Brad Larson
    Brad Larson over 12 years
    @Manuel - The Allocations instrument will hide some memory usage that Memory Monitor and others expose. Images that are 300kB when compressed may be much larger in memory, because they are used in their uncompressed RGBA state there. If you want to see what's accruing in your application through multiple passes, take a heap shot in Allocations at each point that you repeat the same action in your application. The objects you see there at each pass are building up in memory, and you should be able to identify the line on which they are created.
  • Manuel
    Manuel over 12 years
    Thank you very much, this helps alot. I will happily accept berylliums - and your - answer.