Re: NSImage TIFFRepresentation memory leak
Re: NSImage TIFFRepresentation memory leak
- Subject: Re: NSImage TIFFRepresentation memory leak
- From: Roland Torres <email@hidden>
- Date: Fri, 1 Dec 2006 09:39:27 -0800
On Dec 1, 2006, at 6:30 AM, Daniel Jalkut wrote:
I just caught sight of this thread, and went back to read Roland's
original description. I have seen what appear to be NSImage-related
leaks in a multi-threaded environment, too.
Roland, I see that you point out the "TIFFRepresentation" call is
being made on the main thread, but what of all the other use of the
NSImage? All on the main thread, too? I suspect you're using the
same NSImage in more than one thread, and this is messing up the
NSImage caching mechanism. In my experience NSImage leaks when
given the opportunity to try to cache on more than one thread.
It's been a long time but in my case I came to the conclusion that
I would work around the problem by turning off caching on the
images in question. Perhaps it was a brute-force way of fixing the
problem, but it did the trick and I didn't notice a major
performance hit. This is a simple category method I added to NSImage:
- (void) makeThreadSafe
{
[self setCachedSeparately:YES];
[self setCacheMode:NSImageCacheNever];
}
I basically call [theImage makeThreadSafe] as quickly as possible
after creating instances of any NSImage in the app.
Hope this helps, at least to know that you're not alone :) Perhaps
as a test you could try the above technique just to see if we're
talking about the same phenomenon.
Daniel
On Dec 1, 2006, at 8:06 AM, j o a r wrote:
On 1 dec 2006, at 09.33, Hidetomo Katsura wrote:
FYI, this is a textbook case of an OS leak. you (or at least i)
can easily tell from the backtrace. you are only responsible for
releasing a reference that's returned by a method or function.
just file a bug and move on. if it's a significant leak, you have
to come up with a workaround.
in this case, a reference returned by CGLCreateContext (or
allocate_context. it doesn't matter) is the leak, and it was
never returned to you since -[NSImage TIFFRepresentaion] only
returns an autoreleased NSData object (not a CGLCreateContext).
obviously, you can't directly release a reference returned by
CGLCreateContext since you don't even have access to it.
in short, if you didn't create (allocate, copy, or retain) it,
it's not your leak.
Based on the data presented in this thread so far I would not
agree that your claim is necessarily true.
ObjectAlloc shows backtraces to things that are allocated, but
that's not the same thing as leaked.
If the piece of data that is allocated in this backtrace is still
live, that is most likely because its "owner" has not released /
freed it yet. Note that the "leak event" is typically not the same
thing as the allocation event!
Presented with something that could be either [1] a memory leak in
a framework, or [2] a memory leak in your code, you should always
suspect your code - Unless the evidence to the contrary is
overwhelming.
In this case we don't even know what's being leaked - is it opaque
malloced data? It should be of primary interest to see if the
NSData instances returned from "-TIFFRepresentation" are leaked or
not. This, and many other similar points of data, is still unknown.
The best thing would of course be if this proposed leak could be
isolated in a small sample program, and posed for others to have a
look at.
I'm not saying that you're necessarily wrong, only that I don't
think we have enough information to assert that you're right.
j o a r
This is an interesting analysis. The app does manage NSImages from
different threads, but only simple things like displaying them in
NSImageViews (again, in the main thread). I call TIFFRepresentation
since it's the only way I know to write the NSImage out to a file,
but this is called in only one place, and the leak only occurs when I
do that. I've implemented the category Daniel describes, but the leak
still results. There must be some complex interactions going on
because I can't reproduce the leak in a standalone multithreaded app.
If this is a known bug, I guess I'll have to live with it for now,
unless I can find another way to generate an image file from the
NSImage.
Roland
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden