Re: Resize-window memory leak? (Was: [OT] Re: Don't leak memory!!)
Re: Resize-window memory leak? (Was: [OT] Re: Don't leak memory!!)
- Subject: Re: Resize-window memory leak? (Was: [OT] Re: Don't leak memory!!)
- From: Dietmar Planitzer <email@hidden>
- Date: Fri, 21 Nov 2003 23:22:06 +0100
On Nov 21, 2003, at 10:34 AM, Oliver Donald wrote:
Speaking of using Top to spot memory leaks...
My current app's memory usage slowly but surely creeps ever-upwards
whenever
I resize the window. It contains a customView who's drawRect: method
only
uses [NSBezierPath strokeLineFromPoint:toPoint:] and [NSColor
colorWithCalibratedRed:green:blue:alpha:]. The source is a plain C
array of
structures. But the memory goes usage up every second or so of resize
dragging. Is this normal? The memory doesn't ever seem to get released!
I am assuming that [NSBezierPath strokeLineFromPoint:toPoint:] is
autoreleased? [NSColor colorWithCalibratedRed:green:blue:alpha:] is a
'with'
method, so that should be auto-release also?
Yes those methods return autoreleased objects and that's very likely
the source of your problem.
When you resize a window what happens is that the internal NSFrameView
class (actually some subclass) sets up a modal event loop which tracks
the mouse until you end the window resize session by releasing the
mouse button. Prior to Panther the modal event loop did not create and
manage its own autorelease pool and so objects which got autoreleased
in the a view's -drawRect method ended up in the current autorelease
pool. This pool, however, only got cleaned up once the user stopped
resizing the window.
This problem should no longer exist for applications linked and running
on Panther because according to the AppKit release notes the system now
sets up a dedicated autorelease pool for each window resize.
Actually, this is just one example of a much larger problem which is
very common to Cocoa applications. I've seen many Cocoa apps which
enter a modal event loop in order to track the mouse (i.e. a loop which
allows the user to drag an image) but neglect to create an autorelease
pool. Consequently those apps consume more and more memory - the longer
you drag, the more memory they consume.
The solution to this problem is simple: Just create an autorelease pool
before you enter a modal event loop, then destroy and re-create the
pool once per event loop cycle (or every n-th cycle). This makes sure
that all objects created and autoreleased inside the event loop get
released as soon as possible.
Regards,
Dietmar Planitzer
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.