gpt4 book ai didi

objective-c - iOS objective-c 返回对象可用性/范围

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:10:34 27 4
gpt4 key购买 nike

我已阅读 apple 文档以断言我正确地进行了内存管理,但提出了一些问题。

问题一:

他们将这段代码展示为错误的:

in obj implementation:
- (NSString *)method1 {
NSString *string = [[NSString alloc] initWithFormat:@"%@ %@", firstName, lastName];
return string;
}

... later ...

NString* myString = [obj method1];
NSLog("%@", myString);
[myString release];

Apple Doc :

You own the string returned by alloc, but lose the reference to it before you get a chance to relinquish ownership. Following the memory management rules, this would result in a memory leak, since the caller has no indication that they own the returned object.

因为我负责释放之前分配的对象,所以没有内存泄漏,对吗? “失去对它的引用”是什么意思?

仅关于 apple 的内存管理建议是错误的(调用者没有指示他们拥有返回的对象)或者这在技术上也是错误的?

问题 2:

这是关于自动释放对象的可用性:

示例代码:

in obj1 implementation:
- (NSString *)methodA {
NSString *string = [[NSString alloc] initWithFormat:@"%@ %@", firstName, lastName];
return [string autorelease];
}

in obj2 implementation:
- (NSString *)methodB:(NSString *)inputString {
NSLog("%@",inputString)
//*other call of methods with arg inputString*//
}
... later ...

NString* myString = [obj1 methodA];

[obj2 method2:myString];

在我的函数调用之后,obj1 返回的自动释放对象将可用多远(或多深)。关于苹果的文档“自动释放对象将在其可变范围内可用”。我应该在某个时候保留它吗?

最佳答案

Q1: It is wrong only regarding apple's memory management recommendations (the caller has no indication that they own the returned object) or this is also technically wrong ?

从技术上讲这是正确的,因为您在使用后释放 myString。但是,如果您关注 Apples guidelines for method naming (强烈推荐),这显然是错误的:

You own any object you create. You “create” an object using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy).

method1 不包含 createallocnewcopy - 因此根据指南,调用者不拥有该对象,也不必释放它。

Q2: How far (or deep) following my functions calls will the autorelease object returned by obj1 will be available. Regarding apple's documentations "Autorelease objects will be available within their variable scope". Should I retain it at some point ?

自动释放的对象将一直存在,直到最近的自动释放池被耗尽,参见 Autorelease Pools :

An autorelease pool is an instance of NSAutoreleasePool that “contains” other objects that have received an autorelease message; when the autorelease pool is deallocated it sends a release message to each of those objects. An object can be put into an autorelease pool several times, and receives a release message for each time it was put into the pool. Thus, sending autorelease instead of release to an object extends the lifetime of that object at least until the pool itself is released (the object may survive longer if it is retained in the interim).
...
The Application Kit automatically creates a pool at the beginning of an event cycle (or event-loop iteration), such as a mouse down event, and releases it at the end, so your code normally does not have to worry about them. There are three cases, though, where you might use your own autorelease pools:
...

因此,如果您需要您的实例在相应的自动释放池被耗尽后保持事件状态,请通过保留它们来取得所有权。否则,您通常只是让它们由池处理。

关于objective-c - iOS objective-c 返回对象可用性/范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4873481/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com