在一个小型 RTS 项目中,我有一个按钮列表,每个按钮都有一个分配给它们的建筑对象。有些建筑物非常通用,但有些则非常特殊,因此我将其中一些建筑物作为子类。
当我制作一个按钮列表时,它们每个都有一个 PEHouse 对象,一切正常。
但是一旦其中一个 PEHouse 是子类的 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;
}
一切正常。当我只生成一个这样的子集构建按钮时:
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;
}