objective-c - 静态实例使用 ARC 过早释放属性

标签 objective-c ios ios5

我有一个在整个应用程序中使用的静态实例变量。它有属性。这些属性在我的应用程序中使用并且似乎工作得很好。但是,有时属性会过早释放。奇怪的是,拉动这些属性的对象保留了一些并释放了另一些。什么是确保我的对象的属性不会过早释放的好方法。

编辑:事实证明,这个问题根本不是过早发布的问题。这是一个转换问题。感谢大家的帮助。

@interface Game : NSObject

@property (nonatomic, strong) PFObject     *gameObject;
//@property (nonatomic, strong) Concept     *concept; // Will need to add Concept Object to GameObject once it's wrapper is done
@property (nonatomic, strong) User         *initialPlayer;
@property (nonatomic, strong) User         *invitedPlayer;         
@property (nonatomic, strong) User         *lastPlayedPlayer;
@property (nonatomic, strong) NSDate       *lastPlayedDate;
@property (nonatomic, strong) NSDate       *timeOutDate;
@property (nonatomic, assign) int           timerTicks;
@property (nonatomic, assign) int           currentRoundNumber;
@property (nonatomic, strong) User          *winnerPlayer;         
@property (nonatomic, assign) int           initialPlayerPoints;
@property (nonatomic, assign) int           invitedPlayerPoints;
@property (nonatomic, assign) int           currentPlayerPoints;
@property (nonatomic, assign) GameStatus    status;
@property (nonatomic, assign) int           initialPlayerTimeouts;
@property (nonatomic, assign) int           invitedPlayerTimeouts;
@property (nonatomic, assign) BOOL          isInitialPlayer;
@property (nonatomic, strong) NSMutableDictionary *rounds;
@property (nonatomic, strong, readonly)     Round *currentRound;

+(void)getActiveUserGameObjects:(PFUser *)user target:(id)target selector:(SEL)selector;
+(void)getYourTurnGameObjects:(PFUser *)user target:(id)target selector:(SEL)selector;
+(void)getTheirTurnGameObjects:(PFUser *)user target:(id)target selector:(SEL)selector;
+(void)getGameObjects:(PFUser *)user yourTurn:(id)yourTurn target:(id)target selector:(SEL)selector;

+(Game*)currentGame;
+(void)setCurrentGame:(Game*)currentGame;

..

//.m @implementation   
..

static Game *sharedInstance = nil;
..


+(Game*)currentGame
{
    return sharedInstance;
}

+(void)setCurrentGame:(Game*)currentGame
{
    sharedInstance = currentGame;
}


...

#pragma mark - Player Setters and Getters

-(void)setInvitedPlayer:(User *)invitedPlayer
{
    if (nil != invitedPlayer.userObject)
    {
        [self.gameObject setObject:invitedPlayer.userObject forKey:GAME_INVITED_PLAYER];
    }
}

-(User*)invitedPlayer
{
    NSObject *value = [self.gameObject objectForKey:GAME_INVITED_PLAYER];
    if ([value isKindOfClass:[PFUser class]])
    {
        return [User userFromPFUser:(PFUser*)value];
    }

    return nil;
}

最佳答案

What would be a good way to insure that the properties of my object are not released prematurely.

通过保持强引用来保留它们。 ARC 随机释放您的数据的可能性极小。更有可能的是,您在无意中放弃了它。

我首先要看的是您对 setCurrentGame: 的使用,确保您不会意外地同时处理不同的 Game 对象程序的不同部分。首先,确保 t

您的 +get... 方法的命名很笨拙(get 前缀在 ObjC 中意味着非常具体的事情,这不是您在这里所做的事情)。像这样采用目标和操作的 Havings 类方法似乎很可能会遇到麻烦。这让我想知道里面发生了什么。

您在两种 User 对象之间的转换有点可疑,我会确保您不会在无意时意外删除 User 或 PFUser 对象。

不过,一般来说,这个问题过于模糊了。您是否正在结束悬挂的强指针?您的强指针似乎变成了 nil 吗?您的游戏对象本身是否变为 nil?你怎么知道什么时候被“释放”?或者你的意思是他们正在取消分配?您是否在 dealloc 中放置了断点以查看谁最后引用了该对象?

关于objective-c - 静态实例使用 ARC 过早释放属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9932885/

相关文章:

iphone - 我确信变量范围存在问题

xcode - iOS 5屏幕初始自动旋转

iOS:如何使用Restkit设置上传到服务器的图像的名称

ios - 计算文本在居中对齐的位置开始

objective-c - Objective-C 中的全局变量 - extern 和 .m 文件声明顶部的差异

iphone - iPhone内存管理背后/内部到底是什么?

ios - 如何以编程方式在 iOS 上创建和拨号 VPN 连接?

ios - 如何将 UIImage 切成碎片(如披萨)并将每个碎片保存在数组中?

objective-c - 在 Objective-C 中生成随机数

iphone - 从 key 链中的 key 对中提取公钥