obj-c NSString 和分配/保留/释放
这可能是一个更多关于对象分配/保留/释放的问题,但我将使用 NSString 作为示例。我知道我可以这样做:
NSString* myString = [[NSString alloc] initWithString:@"Test"];
本质上分配并初始化我的变量 myString 引用的字符串,稍后我应该调用 [myString release] 。但是,如果在执行此操作之后,我将其设置为其他字符串,例如:
myString = someOtherString;
这是否会本质上造成内存泄漏,因为我已将指针重新分配给另一个对象并丢失了对我分配的原始对象的所有引用?
如果我只是想亲自分配和释放一个字符串,然后在不同的时间更改它的值,我应该使用“=”以外的不同语法,还是重载该语法以正确更改最初由 myString 表示的对象的内容当我使用 =.
This is probably a question that is more about object alloc/retain/release, but I'll use NSString as an example. I'm aware that I can do:
NSString* myString = [[NSString alloc] initWithString:@"Test"];
to essentially allocate and initialize a string referenced by my variable myString which I should later call [myString release] upon. However, if after I do this, I set it to some other string such as:
myString = someOtherString;
does that essentially create a memory leak because I've reassigned my pointer to another object and lost all reference to the original one I allocated?
If I simply want to personally allocate and release a string and then change its value at various times, should I be using a different syntax other than '=' or is that overloaded to properly change the contents of the object that is originally represented by myString when I use =.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,在你的例子中你正在泄漏内存。
你应该这样做:
我听过的整个保留/释放事情描述的最好方法是想象你正在为某人遛狗。你的“对象”是一只狗,“保留”给狗拴上皮带,“释放”则松开皮带。您可以为狗拴上任意多条皮带,但您希望至少为狗保留一根皮带,这样它就不会自由奔跑(泄漏),并且一旦您将狗带回来,您希望将其上的所有皮带都取下给所有者(您想摆脱该对象)。在您的示例中,您可以将其视为在行走中松开狗的皮带并拿起另一只狗的皮带。
Yes, in your example you are leaking the memory.
You this should be done like so:
The best way I have heard the whole retain/release thing described is imagine you are walking a dog for someone. Your "object" is a dog and "retain" puts a leash on the dog and "release" takes a leash off. You can have as many leashes on the dog as you want but you want to keep at least one leash on the dog so he doesn't run free (leak) and you want to take all the leashes off the dog once you get it back to the owner (you want to get rid of the object). In your example, you can think of it as letting go of the leash on the dog mid-walk and picking up the leash of a different dog.
如果您在将另一个指针值分配给它之前正确地放弃了所有权,则不会发生泄漏:
当您通过调用release来放弃对象的所有权时,您应该考虑它有效地释放 - 这可能是最后一次参考它。指针仍然具有相同的值只是 Objective-C 类类型实现和使用方式的副作用。
根据内存限制和代码的用途,您还可以使用
autorelease
d 实例:请注意,如果引用不是局部变量而是 ivar,您可能需要使用 声明的属性。
If you relinquished ownership properly before assigning another pointer value to it, there is no leak:
The moment you relinquish ownership of an object by calling
release
, you should consider it effectively deallocated - this might have been the last reference to it. That the pointer still has the same value is just a side-effect of how Objective-C class types are implemented and used.Depending on memory constraints and what your code does, you could also use
autorelease
d instances:Note that if the reference isn't a local variable but an ivar, you'll probably want to use declared properties instead.
您可以执行以下操作:
返回的字符串是自动释放的,因此您不必释放它。您也不“拥有”它。如果这是一个问题,请按照其他人的建议进行操作,并在重新分配其值之前向
myString
发送一条release
消息。You could do the following:
The returned string is autoreleased so you don't have to release it. You also don't "own" it. If that's an issue do as other's have suggested and send a
release
message tomyString
before you reassign it's value.