如何处理函数返回的对象以避免内存泄漏?

发布于 2024-11-07 09:12:19 字数 551 浏览 0 评论 0原文

假设我有一个函数,

- (NSString *)fullNameCopy {
    return [[NSString alloc] initWithFormat:@"%@ %@", self.firstName, self.LastName];
}

有人可以告诉我如何调用这个函数,如何将其值分配给新对象,以及如何释放它,避免内存泄漏和错误访问。

会像

NSSting *abc = [object fullNameCopy];

// 使用它并释放

[abc release];

还是我也应该分配 abc 字符串?

更新:

这里的要点是,我可以从函数返回非自动释放对象,然后在调用函数中释放它们吗?根据 Obj-C 函数命名约定,包含 alloc 或 copy 的函数名称应返回对象,假设调用函数具有所有权。

与上面的情况一样,我的函数“fullNameCopy”返回一个非自动释放的对象,我想在调用函数中释放它们。

Suppose I have a function

- (NSString *)fullNameCopy {
    return [[NSString alloc] initWithFormat:@"%@ %@", self.firstName, self.LastName];
}

Can somebody tell me how to call this function, how to assign its value to a new object, and how then to release it, avoiding memory leaks, and bad access.

Would it be like

NSSting *abc = [object fullNameCopy];

// Use it and release

[abc release];

or I should alloc abc string too ?

Update:

The point here, Can I return non-autorelease objects from a function and then release them in the calling function. As per Obj-C function naming conventions, a function name containing alloc or copy should return object assuming that calling function has the ownership.

As in above case, my function "fullNameCopy" return a non-autoreleased abject, and I want to release them in the calling function.

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

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

发布评论

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

评论(4

狼性发作 2024-11-14 09:12:19

你是对的。由于方法名称包含单词“copy”,Cocoa 约定规定该方法返回一个由调用者拥有的对象。由于调用者拥有该对象,因此它负责释放它。例如:

- (void)someMethod {
    NSString *abc = [object fullNameCopy];

    // do something with abc

    [abc release];
}

或者,您可以使用 -autorelease 而不是 -release

- (void)someMethod {
    NSString *abc = [[object fullNameCopy] autorelease];

    // do something with abc
}

You are right. Since the method name contains the word ‘copy’, Cocoa convention dictates that the method returns an object that is owned by the caller. Since the caller owns that object, it is responsible for releasing it. For example:

- (void)someMethod {
    NSString *abc = [object fullNameCopy];

    // do something with abc

    [abc release];
}

Alternatively, you could use -autorelease instead of -release:

- (void)someMethod {
    NSString *abc = [[object fullNameCopy] autorelease];

    // do something with abc
}
强辩 2024-11-14 09:12:19

请参阅 更新后

- (NSString *)fullNameCopy {
    NSString *returnString  = [NSString stringWithFormat:@"%@ %@", self.firstName, self.LastName]; // Autorelease object.
    return returnString;
}

-(void) someFunction {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *fullName = [self fullNameCopy];

    [pool release]
}

Refer this post

UPDATE:

- (NSString *)fullNameCopy {
    NSString *returnString  = [NSString stringWithFormat:@"%@ %@", self.firstName, self.LastName]; // Autorelease object.
    return returnString;
}

-(void) someFunction {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *fullName = [self fullNameCopy];

    [pool release]
}
忆离笙 2024-11-14 09:12:19

像这样:

- (NSString *)fullName {

    NSString * retVal = [[NSString alloc] initWithFormat:@"%@ %@", self.firstName, self.LastName];

    return [retVal autoRelease];
}

然后

NSSting *abc = [object fullName];

Like This:

- (NSString *)fullName {

    NSString * retVal = [[NSString alloc] initWithFormat:@"%@ %@", self.firstName, self.LastName];

    return [retVal autoRelease];
}

Then

NSSting *abc = [object fullName];

谁人与我共长歌 2024-11-14 09:12:19

return [[[NSString alloc] initWithFormat:@"%@ %@", self.firstName, self.LastName]autorelease];

return [[[NSString alloc] initWithFormat:@"%@ %@", self.firstName, self.LastName]autorelease];

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