• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: self release
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: self release


  • Subject: Re: self release
  • From: Wolfgang Ante <email@hidden>
  • Date: Tue, 13 May 2003 11:45:35 +0200

if aTarget has retain count 1 and is not autoreleased then the
retainCount goes to 2 on calling, and 1 on exit. It does not
deallocate.

Yes, of course.

If you call [aTarget autorelease] in the main thread then,
according to the docs at least, you risk colliding the call stacks,
same if you call [aTarget release] after detachThread...

Please point me to this documentation, I could not find it.

Note this result:

2003-05-12 18:28:10.202 test[801] Doing Test
2003-05-12 18:28:10.202 test[801] 1 [self retainCount]: 1
2003-05-12 18:28:10.204 test[801] 2 [self retainCount]: 2
2003-05-12 18:28:20.204 test[801] 3 [self retainCount]: 1

for this code:

- (void)test111:(id)sender
{
NSLog(@"2 [self retainCount]: %d\n", [self retainCount]);

[NSThread exit];

}

- (void)doTest:(id)sender
{

NSLog(@"Doing Test");

NSLog(@"1 [self retainCount]: %d\n", [self retainCount]);

[NSThread detachNewThreadSelector:@selector(test111:) toTarget:self
withObject:nil];

sleep(10);

NSLog(@"3 [self retainCount]: %d\n", [self retainCount]);

This is maybe because you do all inside your object? I had no problem with following code:

- (void)awakeFromNib // just to test it somewhere
{
TestClass *test = nil;

test = [[TestClass alloc] init];
[test runInBackground];
[test release];
NSLog (@"released in main thread");
}

@implementation TestClass

- (id)init
{
NSLog (@"init");
return [super init];
}

- (void)dealloc
{
NSLog (@"dealloc");
}

- (void)runInBackground
{
NSLog (@"detaching thread");
[NSThread detachNewThreadSelector:@selector (_process) toTarget:self
withObject:nil];
}

- (void)_process
{
NSAutoreleasePool *pool = nil;

pool = [[NSAutoreleasePool alloc] init];
NSLog (@"start working");
sleep (3); // do the real work here
NSLog (@"end working");
[pool release];
}

@end

Which produces following output:

2003-05-13 11:23:35.868 Tester[795] init
2003-05-13 11:23:35.897 Tester[795] detaching thread
2003-05-13 11:23:35.911 Tester[795] start working
2003-05-13 11:23:35.930 Tester[795] released in main thread
2003-05-13 11:23:38.912 Tester[795] end working
2003-05-13 11:23:38.912 Tester[795] dealloc

Works as expected, and I think this is the design the Cocoa developers had in mind when they retained aTarget. I think this, because it is elegant. Every time I find elegant code, I know I found the Cocoa way. ;-)

Why do I risk colliding my call stacks with this?

Bye, Wolfgang :-)

BTW. Where is 'sleep' defined? It links fine, but I miss the header file and get a warning. I hate warnings! ;-)
_______________________________________________
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.
  • Follow-Ups:
    • Re: self release
      • From: Chris Ridd <email@hidden>
References: 
 >Re: self release (From: lbland <email@hidden>)

  • Prev by Date: Re: [obj performSelectorOnMainThread: ...] vs. [runLoop performSelector: ...]
  • Next by Date: Re: [obj performSelectorOnMainThread: ...] vs. [runLoop performSelector: ...]
  • Previous by thread: NSWorkspace
  • Next by thread: Re: self release
  • Index(es):
    • Date
    • Thread