1

all. i have test codes as below:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [NSThread detachNewThreadSelector:@selector(test) toTarget:self withObject:nil];
}

-(void)test
{
    MyClass *obj = [[[MyClass alloc] init] autorelease];
    NSLog(@"%@",[my description]);
}

i create a autorelease object in NSThread's method but no user-created autoreleasepool. when NSThread exit,obj just dealloced(i have a breakpoint int method delloc). why? dose NSThread create it's own autoreleasepool by itself?

eliudnis
  • 239
  • 3
  • 6
  • Yes, this has been done quite some time ago now. You used to get warnings that there is no autorelease pool. – Matic Oblak Jul 29 '14 at 07:50
  • 1
    since when,is there any documents to make clear of this point? – eliudnis Jul 29 '14 at 08:05
  • Since ARC I believe.. or around there somewhere. I don't know of any docs, this was just a notice. Since the ARC it would seem that the concept of memory management itself in objective C is deprecated so all such things were removed. – Matic Oblak Jul 29 '14 at 08:16
  • @MaticOblak: But the OP is obviously not using ARC. – user102008 May 22 '15 at 00:48
  • @user102008 Even when not using the ARC. I just wanted to explain about when this had happened. What used to be you got warnings that the object is leaking since no autorelease pool is present. I am not sure but most likely what happens now is the pool is created for you when you call the autorelease. At what point is it drained is another mystery then. Anyway it has nothing to do with the ARC but seemed to be included at the same time. – Matic Oblak May 22 '15 at 09:00

1 Answers1

-1

I think it is that. normally you need to create a autoreleasepoll,because you may need your thread to be always runing,if you dont create a autoreleasepoll,memory using will increase all the time.

but in your code,you just run a method in another thread, after run it, the thread exit.so the memory that used in the thread are all released.

Ning Su
  • 11
  • 2