[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Question about retaining delegates
From: |
Pascal Bourguignon |
Subject: |
Re: Question about retaining delegates |
Date: |
Sun, 16 Dec 2001 23:38:17 +0100 (CET) |
Willem Rein Oudshoorn <woudshoo@xs4all.nl> wrote:
>
> Recently I was bitten by the fact that NSBrowser does not
> retain it's delegate. Now I can not easily check this
> behaviour on OpenStep, but isn't this a bug?
> I would expect that, because the NSBrowser needs
> it's delegate later on, will retain the delegate.
>
>
> Wim Oudshoorn.
Well, the delegates are not retained, because they may "own" the
objects whom they're delegate.
Controller x owns NSWindow w ==> Controller x retains NSWindow w.
Controller x is delegate of NSWindow w.
If NSWindow w would retain its delegate, there would be a loop in
the retain chain.
The objects who send setDelegate: should remember to setDelegate:nil
when they're going to be deleted (in their dealloc).
Alternatively, you could add a retain when doing setDelegate:, but
beware of the loops.
------------------------------------------------------------------------
#include <Foundation/Foundation.h>
#include <AppKit/AppKit.h>
int main(int argc,const char* argv[]){
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString* classNames[]={
@"NSApplication",
@"NSBrowser",
@"NSFontManager",
@"NSImage",
@"NSLayoutManager",
@"NSMatrix",
@"NSSavePanel",
@"NSSpellServer",
@"NSSplitView",
@"NSTableView",
@"NSText",
@"NSTextField",
@"NSTextStorage",
@"NSTextView",
@"NSWindow",
nil};
int i;
i=0;
while(classNames[i]!=nil){
NSObject* object=[[NSClassFromString(classNames[i]) alloc]init];
NSObject* delegate=[[NSObject alloc]init];
int oldCount=[delegate retainCount];
[object setDelegate:delegate];
NSLog(@"-[%@ setDelegate:] %@ retain the delegate.\n",classNames[i],
oldCount==[delegate retainCount]?@"doesn't":@"does");
i++;
}
[pool release];
exit(0); // insure the process exit status is 0
return 0; // ...and make main fit the ANSI spec.
}//main.
/*
On OPENSTEP 4.2:
[pascal@despina TestDelegateRetain]$ ./TestDelegateRetain 2>&1 | sed -e 's/.*\]
-/-/'
-[NSApplication setDelegate:] doesn't retain the delegate.
-[NSBrowser setDelegate:] doesn't retain the delegate.
-[NSFontManager setDelegate:] doesn't retain the delegate.
-[NSImage setDelegate:] doesn't retain the delegate.
-[NSLayoutManager setDelegate:] doesn't retain the delegate.
-[NSMatrix setDelegate:] doesn't retain the delegate.
-[NSSavePanel setDelegate:] doesn't retain the delegate.
-[NSSpellServer setDelegate:] doesn't retain the delegate.
-[NSSplitView setDelegate:] doesn't retain the delegate.
-[NSTableView setDelegate:] doesn't retain the delegate.
-[NSText setDelegate:] doesn't retain the delegate.
-[NSTextField setDelegate:] doesn't retain the delegate.
-[NSTextStorage setDelegate:] doesn't retain the delegate.
-[NSTextView setDelegate:] doesn't retain the delegate.
-[NSWindow setDelegate:] doesn't retain the delegate.
On GNUstep:
[pascal@thalassa TestDelegateRetain]$
shared_obj/ix86/linux-gnu/gnu-gnu-gnu/TestDelegateRetain_GS 2>&1 | sed -e
's/.*\] -/-/'
-[NSApplication setDelegate:] doesn't retain the delegate.
-[NSBrowser setDelegate:] doesn't retain the delegate.
-[NSFontManager setDelegate:] doesn't retain the delegate.
-[NSImage setDelegate:] doesn't retain the delegate.
-[NSLayoutManager setDelegate:] doesn't retain the delegate.
-[NSMatrix setDelegate:] doesn't retain the delegate.
-[NSSavePanel setDelegate:] doesn't retain the delegate.
-[NSSpellServer setDelegate:] doesn't retain the delegate.
-[NSSplitView setDelegate:] doesn't retain the delegate.
-[NSTableView setDelegate:] doesn't retain the delegate.
-[NSText setDelegate:] doesn't retain the delegate.
-[NSTextField setDelegate:] doesn't retain the delegate.
-[NSTextStorage setDelegate:] doesn't retain the delegate.
-[NSTextView setDelegate:] doesn't retain the delegate.
-[NSWindow setDelegate:] doesn't retain the delegate.
You have new mail in /var/spool/mail/pascal
*/
------------------------------------------------------------------------
--
__Pascal_Bourguignon__ (o_ Software patents are endangering
() ASCII ribbon against html email //\ the computer industry all around
/\ and Microsoft attachments. V_/ the world http://lpf.ai.mit.edu/
1962:DO20I=1.100 2001:my($f)=`fortune`; http://petition.eurolinux.org/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/IT d? s++:++(+++)>++ a C+++ UB+++L++++$S+X++++>$ P- L+++ E++ W++
N++ o-- K- w------ O- M++$ V PS+E++ Y++ PGP++ t+ 5? X+ R !tv b++(+)
DI+++ D++ G++ e+++ h+(++) r? y---? UF++++
------END GEEK CODE BLOCK------