1

私はメインスレッドでこれをやっています:

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];

ここで、メモリ管理に関してこれで問題ないかどうかを尋ねたいと思います。

4

2 に答える 2

1

あるスレッドでオブジェクトを割り当て、別のスレッドで割り当てを解除することができます。ただし、アプローチ方法によっては、コードが正しく実行されない可能性があります。

可能であればanim、プロパティに変換して、メモリ管理についてあまり心配する必要がないようにします。それができない場合は、アクセサー パターンを適用できますが、自分で実装する必要があります。

static CCAnimation *anim=nil;

+(CCAnimation*)anim {
    @synchronized(self) {
        return [[anim retain] autorelease];
    }
}
+(void)setAnim:(CCAnimation*)animation {
    @synchronized(self) {
        if (anim != animation) {
            [anim release];
            anim = [animation retain];
        }
    }
}
-(void)loadAnimation {
    NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
    [[self class] setAnim:[[[CCAnimaton alloc] init] autorelease]];
    [autoreleasepool drain];
}
于 2011-01-15T06:05:07.403 に答える
0

もちろん、ポインター変数へのアクセスを保護している場合は問題ありません。

于 2011-01-15T05:54:25.687 に答える