[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
NSTextView memory leak
From: |
Caba Conti |
Subject: |
NSTextView memory leak |
Date: |
Sun, 20 Oct 2002 17:45:45 +0100 |
The following code is from NSTextView's dealloc method:
(CVS version from october 19th)
-------------------
/* Balance the RELEASE we sent to us to break the retain cycle
in initWithFrame: or initWithCoder: (otherwise releasing the
_textStorage will make our retain count go below zero ;-) */
RETAIN (self);
/* This releases all the text objects (us included) in
* fall. */
DESTROY (_textStorage);
/* When the rest of the text network is released, we'll be
* released again and be deallocated for real. That will
* likely happen during the DESTROY of the _textStorage, or
* later if parts of the text network are maybe retained
* elsewhere (in autorelease pools etc) for slightly longer.
*/
return;
-------------------
When the dealloc method is executed, then the view's reference count
got decremented once less than it had received release calls (See
NSDecrementExtraRefCountWasZero() in NSObject.m).
So there is already an additional count when program flow gets to
the RETAIN() call. Retaining the view again makes its retainCount
become 2 which is one too much to get deallocated in the DESTROY call.
Here is a little test program:
-------------------
#include <AppKit/AppKit.h>
@interface MyTextView : NSTextView
- (void)dealloc;
@end
@implementation MyTextView
- (void)dealloc
{
static int count = 0;
NSLog(@"dealloc was called %d times", ++count);
[super dealloc];
}
@end
int main(int argc, char *argv[])
{
CREATE_AUTORELEASE_POOL(pool);
[[MyTextView new] release];
NSLog(@"The dealloc method should have been called twice!!!");
RELEASE(pool);
return 0;
}
-------------------
Its output is:
-------------------
2002-10-20 14:55:15.130 MyTest[16167] Warning - mouse/pointer seems to have
more than 5 buttons - just using one to five
2002-10-20 14:55:16.122 MyTest[16167] Failed to recurse into directory
'/opt/GNUstep/Local/Library/Colors' - No such file or directory
2002-10-20 14:55:16.126 MyTest[16167] dealloc was called 1 times
2002-10-20 14:55:16.127 MyTest[16167] The dealloc method should have been
called twice!!!
-------------------
The RETAIN call must be either deleted,
or should be written in this way:
if (![self retainCount])
RETAIN(self);
The output with the patch applied:
-------------------
2002-10-20 15:08:41.397 MyTest[16378] Warning - mouse/pointer seems to have
more than 5 buttons - just using one to five
2002-10-20 15:08:42.387 MyTest[16378] Failed to recurse into directory
'/opt/GNUstep/Local/Library/Colors' - No such file or directory
2002-10-20 15:08:42.392 MyTest[16378] dealloc was called 1 times
2002-10-20 15:08:42.392 MyTest[16378] dealloc was called 2 times
2002-10-20 15:08:42.393 MyTest[16378] The dealloc method should have been
called twice!!!
-------------------
Caba Conti
CabaConti@web.de
- NSTextView memory leak,
Caba Conti <=