Re: NSOperation and NSAutoreleasePool
Re: NSOperation and NSAutoreleasePool
- Subject: Re: NSOperation and NSAutoreleasePool
- From: Aaron Wallis <email@hidden>
- Date: Sat, 7 Mar 2009 17:18:05 +1100
The property was assigned as:
@property (retain) id delegate;
when I walk through the code (and log pretty much everything out)
I get the following:
- (void)processString:(NSString *)tString withDelegate:(id)tDelegate {
NSLog(@"1. %@", tDelegate); // I get <TMPSTProcessDropOperation:
0x175930>
self.delegate = tDelegate;
NSLog(@"2. %@", self.delegate); // I get <NSAutoreleasePool: 0x187e10>
[self.delegate doSomething]; // I get -[NSAutoreleasePool
doSomething]: unrecognized selector sent to instance 0x187e10
}
if I do it the old skool way and don't use the @property decleration
and do this:
- (void)processString:(NSString *)tString withDelegate:(id)tDelegate {
NSLog(@"1. %@", tDelegate); // I get <TMPSTProcessDropOperation:
0x175930>
[delegate release];
delegate = tDelegate;
[delegate retain];
NSLog(@"2. %@", delegate); // I get <TMPSTProcessDropOperation:
0x175930>
[delegate doSomething]; // the method 'doSomething' is executed
}
It's all very peculiar.
On 07/03/2009, at 5:02 PM, Roland King wrote:
this isn't making sense. You need to get to the bottom of this and
not just throw in random code which *seems* to work.
For a start how is delegate declared as a property? Show us the
definition please, and are you synthesizing it or have you written
code for the methods yourself? If you have declared it
@property( readwrite, retain ) ...
and synthesized it, then far from releasing it early that
self.delegate = .. call will be retaining it, whereas your new
assignment isn't.
Your original code,
self.delegate = [ tDelegate retain ]
seems probably wrong if delegate is a retained property, that will
just extra-retain it. Your new code
delegate = tDelegate;
doesn't retain it at all, one of the following should be the right
way to do it
self.delegate = tDelegate; // the property mutator does the retain
for you
delegate = [ tDelegate retain ]; // you do the retain and then
assign it
[ self setDelegate:tDelegate ]; // which is the same as the first
of the three but clearer.
Have you tried NSLog earlier on tDelegate before you set it into
self.delegate, what is it then? Have you looked at this in the
debugger to see what's going on?
I'd have to agree with Kyle's suggestion to read the memory
management documentation again. And single-step your way through
those lines, look at the addreses of tDelegate and what ends up in
self.delegate.
On Mar 7, 2009, at 1:44 PM, Aaron Wallis wrote:
I've actually found a workaround - it seems that if I use Obj-C 2.0
style properties the objects somehow get released earlier than they
should.
in my example I used the code:
self.delegate = tDelegate
where tDelegate is the delegate supplied through the method call.
However, if I just use:
delegate = tDelegate
the script runs fine without a hitch...
it must have something to do with the self.delegate property being
set to retain or something?
maybe it's causing the nsautoreleasepool to release early?
On 07/03/2009, at 4:35 PM, Kyle Sluder wrote:
It sounds like you have some sort of memory issue. Since you
refer to
things like "NSAutorelease objects" (which don't exist) and are
apparently calling -retain on an object that you immediately
assign to
a property, I suggest you go back and re-read the Memory Management
Guide.
--Kyle Sluder
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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