iPhone - UIImage Leak, ObjectAlloc Building
Asked Answered
F

1

0

Alright I am having a world of difficulty tracking down this memory leak. When running this script I do not see any memory leaking, but my objectalloc is climbing. Instruments points to CGBitmapContextCreateImage > create_bitmap_data_provider > malloc, this takes up 60% of my objectalloc.

This code is called several times with a NSTimer.

How do I clear that reUIImage after I return it?

...or How can I make it so that UIImage imageWithCGImage does not build my ObjectAlloc?

    //I shorten the code because no one responded to another post
    //Think my ObjectAlloc is building up on that retUIImage that I am returning
    //**How do I clear that reUIImage after the return?**

-(UIImage) functionname {
    //blah blah blah code
    //blah blah more code

    UIImage *retUIImage = [UIImage imageWithCGImage:cgImage];
            CGImageRelease(cgImage);

            return retUIImage;
    }
Foramen answered 15/9, 2009 at 23:45 Comment(0)
M
1

this method you use instantiates a UIImage and sets it as autorelease. If you want to cleanup these, you will need to empty the pool periodically

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

Note that these can be nested:

NSAutoreleasePool *pool1 = [[NSAutoreleasePool alloc] init];
NSAutoreleasePool *pool2 = [[NSAutoreleasePool alloc] init];
..
..
..
[pool2 release];
[pool1 release];

Common practice is to place these around for loops and other methods that make many autoreleased objects.

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (Thing *t in things) {
  [thing doAMethodThatAutoreleasesABunchOfStuff];
}
[pool release]
Millepede answered 16/9, 2009 at 1:0 Comment(3)
I have seen NSAutoreleasePool released like this...[pool drain] which way is correct? [pool drain] or [pool release]?Foramen
From the docs: In a reference-counted environment, this method behaves the same as release. Since an autorelease pool cannot be retained (see retain), this therefore causes the receiver to be deallocated. When an autorelease pool is deallocated, it sends a release message to all its autoreleased objects. If an object is added several times to the same pool, when the pool is deallocated it receives a release message for each time it was added. The phone has no garbage collectionMillepede
Unclear from the comment, but that description is from the drain methodMillepede

© 2022 - 2024 — McMap. All rights reserved.