在iPhone应用程序中为经常重新分配给新分配的内存的实例变量管理内存的正确方法是什么?

时间:2022-09-07 11:08:30

I'm having trouble figuring out how to manage memory for an instance variable that needs to maintain it's current state for a period of time, then be reassigned to newly allocated memory.

我无法弄清楚如何管理一个实例变量的内存,该实例变量需要在一段时间内保持其当前状态,然后重新分配给新分配的内存。

Take the following example for the instance variable "importantData".:

以下示例为实例变量“importantData”:

-(void)Update
{
   importantData = [[self getObject] retain];
}


- (SomeObject *)getObject 
{
   SomeObject *objInstance = [[SomeObject alloc] init];
   [objInstance autorelease];  
   return objInstance;
}       

In my actual project, the getObject procedure is in a different class but I've simplified it just to get my point across. importantData must stay around be valid in between calls to Update.

在我的实际项目中,getObject过程是在一个不同的类中,但我简化它只是为了得到我的观点。 importantData必须在两次调用Update之间保持有效。

Every time getObject is called, I'm allocating new memory and assigning it to importantData, correct? I figure I have to release the memory that importantData was pointing to before, right? I'm not sure how to do this properly without leaking memory or trying to reference deallocated memory. Thanks!

每次调用getObject时,我都会分配新内存并将其分配给importantData,对吗?我想我必须释放importantData指向的内存,对吧?我不确定如何在不泄漏内存或尝试引用释放内存的情况下正确执行此操作。谢谢!

2 个解决方案

#1


2  

You just need update to look like this:

你只需要更新看起来像这样:

-(void)Update
{
   [importantData release];
   importantData = [[self getObject] retain];
}

Basically, just remember to release before you assign a new value.

基本上,只需记住在分配新值之前发布。

#2


1  

You could use a static variable.

您可以使用静态变量。

static SomeObject *importantObject = nil;

@implementation SomeObject

+ (SomeObject*)getObject {
  if (!importantObject) {
    importantObject = [[SomeObject alloc] init];
  }
  return importantObject;
}

@end

This will keep it around until the app exists. But if you want to invalidate or recreate it you could add a method like:

这将保持它,直到应用程序存在。但是,如果您想要使其无效或重新创建它,您可以添加如下方法:

+ (void)expireObject {
  [importantObject release];
  importantObject = nil;
}

Or even

甚至

+ (void)setObject:(SomeObject*)newObject {
  [importantObject release];
  importantObject = [newObject retain];
}

And you can now use importantObject in SomeObject's class and instance methods, or fetch it from other classes via SomeObject's class method getter.

现在,您可以在SomeObject的类和实例方法中使用importantObject,或者通过SomeObject的类方法getter从其他类中获取它。

#1


2  

You just need update to look like this:

你只需要更新看起来像这样:

-(void)Update
{
   [importantData release];
   importantData = [[self getObject] retain];
}

Basically, just remember to release before you assign a new value.

基本上,只需记住在分配新值之前发布。

#2


1  

You could use a static variable.

您可以使用静态变量。

static SomeObject *importantObject = nil;

@implementation SomeObject

+ (SomeObject*)getObject {
  if (!importantObject) {
    importantObject = [[SomeObject alloc] init];
  }
  return importantObject;
}

@end

This will keep it around until the app exists. But if you want to invalidate or recreate it you could add a method like:

这将保持它,直到应用程序存在。但是,如果您想要使其无效或重新创建它,您可以添加如下方法:

+ (void)expireObject {
  [importantObject release];
  importantObject = nil;
}

Or even

甚至

+ (void)setObject:(SomeObject*)newObject {
  [importantObject release];
  importantObject = [newObject retain];
}

And you can now use importantObject in SomeObject's class and instance methods, or fetch it from other classes via SomeObject's class method getter.

现在,您可以在SomeObject的类和实例方法中使用importantObject,或者通过SomeObject的类方法getter从其他类中获取它。