小さなRTSプロジェクトには、ボタンのリストがあり、それぞれに建物オブジェクトが割り当てられています。非常に一般的な建物もあれば、非常に特殊なものもあるため、一部の建物をサブクラスとして作成しました。
それぞれが PEHouse オブジェクトを持つボタンのリストを作成すると、すべて正常に動作します。
しかし、これらの PEHouse の 1 つがサブクラス化された PEHouse になると、すぐに解放されますが、その理由がわかりません。
ボタンには次のプロパティがあります。
@property (nonatomic, strong) PEHouse *linkedHouse;
家には次の値があります。
@interface PEHouse : NSObject {
NSString *title;
}
@property (nonatomic) BOOL is_locked;
そして、私のタワーには次のセットアップがあります:
#import "PEHouse.h"
@interface PETower : PEHouse <NSObject>
-(void)secondAction;
+ (PETower*)createTower;
@end
このような通常のオブジェクトのリストを生成すると:
DLog(@"Building menu");
if ([key isEqualToString:@"button_income"]) {
[GMBuildMenu buttonFromBuilding:[PEHouse newSawmill] withIndex:1 inDict:dict];
[GMBuildMenu buttonFromBuilding:[PEHouse newQuarry] withIndex:2 inDict:dict];
[GMBuildMenu buttonFromBuilding:[PEHouse newIronMine] withIndex:3 inDict:dict];
[GMBuildMenu buttonFromBuilding:[PEHouse newMiscShop] withIndex:4 inDict:dict];
[GMBuildMenu buttonFromBuilding:[PEHouse newWeaponsShop] withIndex:5 inDict:dict];
[GMBuildMenu buttonFromBuilding:[PEHouse newArmorShop] withIndex:6 inDict:dict];
[GMBuildMenu buttonFromBuilding:[PEHouse newBazaar] withIndex:7 inDict:dict];
[GMBuildMenu buttonFromBuilding:[PEHouse newTreasury] withIndex:8 inDict:dict];
return YES;
}
すべてがうまく機能します。次のように、サブセット化された建物のボタンの 1 つだけをスポーンすると:
if ([key isEqualToString:@"button_military"]) {
[GMBuildMenu buttonFromBuilding:[PEHouse newTower] withIndex:1 inDict:dict];
}
オブジェクトは直後に解放されます...
なぜこの分化が起こるのか、誰にも手がかりがありますか?
アップデート
コードは次のようになります。
+(OpenGLButton*)buttonFromBuilding:(PEHouse*)house withIndex:(int)index inDict:(NSMutableDictionary*)dict {
OpenGLButton *new = [GMButtonMenu addButtonToMenu:dict withLabel:house.button_art andShowname:house.title];
new.linkedHouse = house;
CGRect buttonFrame = CGRectMake(20, ((30)*index), (28*[GMButtonMenu globalGUIScale]), (28*[GMButtonMenu globalGUIScale]));
new.location = buttonFrame;
new.index = index;
new.details = house.desc;
return new;
}
PEHouse には次のものがあります。
+ (PETower *)newTower {
PETower *new = [PETower createTower];
return new;
}
+ (PEHouse *)newSawmill {
PEHouse *new = [PEHouse createDefault];
new.title = @"Sawmill";
new.is_large = YES;
return new;
}