Manuel
Manuel

Reputation: 73

UIImageJPEGRepresentation

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

Upvotes: 3

Views: 16770

Answers (1)

beryllium
beryllium

Reputation: 29767

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.

Upvotes: 4

Related Questions