Objective-C 自动释放池不释放对象
我对 Objective-C 很陌生,正在阅读内存管理。我试图用 NSAutoreleasePool 玩一下,但不知何故它不会释放我的对象。
我有一个带有 setter 和 getter 的类,它基本上设置一个 NSString *name。释放池后,我尝试 NSLog 该对象,它仍然有效,但我想它不应该?
@interface TestClass : NSObject
{
NSString *name;
}
- (void) setName: (NSString *) string;
- (NSString *) name;
@end
@implementation TestClass
- (void) setName: (NSString *) string
{
name = string;
}
- (NSString *) name
{
return name;
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
TestClass *var = [[TestClass alloc] init];
[var setName:@"Chris"];
[var autorelease];
[pool release];
// This should not be possible?
NSLog(@"%@",[var name]);
return 0;
}
I am very new to Objective-C and was reading through memory management. I was trying to play around a bit with the NSAutoreleasePool but somehow it wont release my object.
I have a class with a setter and getter which basically sets a NSString *name. After releasing the pool I tried to NSLog the object and it still works but I guess it should not?
@interface TestClass : NSObject
{
NSString *name;
}
- (void) setName: (NSString *) string;
- (NSString *) name;
@end
@implementation TestClass
- (void) setName: (NSString *) string
{
name = string;
}
- (NSString *) name
{
return name;
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
TestClass *var = [[TestClass alloc] init];
[var setName:@"Chris"];
[var autorelease];
[pool release];
// This should not be possible?
NSLog(@"%@",[var name]);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您释放指针 var 时,您是在告诉操作系统它指向的内存可以重新分配。指针仍然指向该内存,并且在重新分配它之前,它仍然包含对象的剩余部分。一旦它被重新分配,尝试调用 name 方法将不再起作用。
When you release the pointer var, you're telling the OS that the memory it pointed to is available to be reallocated. The pointer still points to that memory, and until it gets reallocated it still contains the remains of your object. Once it gets reallocated, trying to call the name method will no longer work.
您的代码有几个问题。首先,您既不
复制
也不保留
存储在name
实例变量中的字符串。因此,如果字符串被存储到属性中的人释放,那么您将留下一个悬空引用。您应该从一开始就使用或使用属性。
另外,如果将对象引用保留在实例变量中,则应该提供dealloc方法的正确定义:
最后,仅仅因为对象已被释放,并不意味着前一个实例的内存无效。您的原始程序很可能在悬空引用 (
var
) 上调用一个方法,这纯粹是运气好。 (特别是, to (auto
)release
不会自动将引用设置为nil
)。Your code has several problems. First, you do neither
copy
norretain
the string stored into thename
instance variable. So, if the string is released by whoever stored it into the property, you are left with a dangling reference. You should door use properties right from the start.
Also, if you keep object references in instance variables, you should provide a proper definition of the
dealloc
method:Finally, just because an object has been deallocated, does not mean, that the memory of the former instance is invalidated. Your original program is most likely calling a method on a dangling reference (
var
), which happens to work by sheer luck here. (In particular, to (auto
)release
does not automatically set the reference tonil
).