Objective-C:在一个线程中分配并在另一个线程中释放
我在我的主线程中执行此操作:
CCAnimation *anim; //class variable
[NSThread detachNewThreadSelector:@selector(loadAimation) toTarget:self withObject:nil];
在 loadAimation 中:
-(void) loadAnimation {
NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
anim = [[CCAnimaton alloc] init];
[autoreleasepool drain];
}
在主线程中我释放它:
[anim release];
现在我想问这对于内存管理是否可以。
I am doing this in my Main Thread:
CCAnimation *anim; //class variable
[NSThread detachNewThreadSelector:@selector(loadAimation) toTarget:self withObject:nil];
In loadAimation:
-(void) loadAnimation {
NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
anim = [[CCAnimaton alloc] init];
[autoreleasepool drain];
}
And in main thread I release it:
[anim release];
Now I want to ask if this is fine regarding memory management.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以在一个线程中分配一个对象并在另一个线程中释放它。但是,根据您的处理方式,您的代码可能会错误地执行此操作。
如果可能的话,将
anim
转换为属性,这样您就不必太担心内存管理。如果不能,您可以应用访问器模式,但您必须自己实现。It's possible to allocate an object in one thread and deallocate it in another. However, depending on how you approach it, your code could do it incorrectly.
If possible, turn
anim
into a property so you don't have to worry so much about memory management. If you can't, you can apply the accessor pattern, but you have to implement it yourself.当然,如果您要保护对指针变量的访问,那应该没问题。
It should be ok, of course if you are protecting access to the pointer variable.