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

发布于 2024-08-21 01:30:16 字数 548 浏览 8 评论 0原文

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

以实例变量“importantData”为例:

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


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

在我的实际项目中,getObject 过程位于不同的类中,但我对其进行了简化,只是为了表达我的观点。 importantData 在更新调用之间必须保持有效。

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

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".:

-(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.

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

并安 2024-08-28 01:30:16

您只需要更新为如下所示:

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

基本上,只需记住在分配新值之前释放即可。

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.

笑叹一世浮沉 2024-08-28 01:30:16

您可以使用静态变量。

static SomeObject *importantObject = nil;

@implementation SomeObject

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

@end

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

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

甚至

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文