可以设置为 nil,但不能设置为不同的值
我想我还没有完全理解关于内存和那些东西的所有知识,但这是我的问题:
我在将被推送的视图上定义了 idActual 变量(在其标头中定义的 var ),我可以读取 (NSLog(idActual) )并将其设置为零,没有问题。但是当我更改它的值时,我收到一个 CFString 错误,这应该是由于内存管理不良造成的,我已经尝试过:
我可以这样做:nextView.idActual = nil;
我不能这样做:
a) nextView.idActual = @"1";
b) NSString *str = [NSString stringWithFormat:@"1"];
nextView.idActual = str;
c) NSString *str = [[NSString alloc] initWithFormat:@"1"];
nextView.idActual = str;
[str release];
a、b 和 c 总是给我 CFString 错误: *** -[CFString isEqualToString:]: 消息发送到已释放的实例
i think i don't finish to understand all about memory and that stuff but this is my problem:
I have a variable defined idActual on a view that will be pushed (var defined in its header), i can read (NSLog(idActual)) and set it to nil without problems. BUT when i change its value i get an CFString error, that its supposed to be due to bad memory management, i've tried this:
i can do this: nextView.idActual = nil;
i cant do this:
a) nextView.idActual = @"1";
b) NSString *str = [NSString stringWithFormat:@"1"];
nextView.idActual = str;
c) NSString *str = [[NSString alloc] initWithFormat:@"1"];
nextView.idActual = str;
[str release];
a, b and c always give me the CFString error:
*** -[CFString isEqualToString:]: message sent to deallocated instance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您去更改值时,
nextView.idActual
中包含的 CFString (NSString) 似乎已经被释放。如果您可以发布更多相关代码,那将会有所帮助。我猜测 idActual 被声明为 @property(nonatomic,retain) 。当您尝试向 idActual 设置新值时,将调用该属性的 setter 方法(该 setter 可能是自动生成的)。 setter 方法所做的第一件事是尝试比较旧值和新值 - 然后它崩溃了。
当该 setter 方法尝试将新值与旧值进行比较时,它会遇到麻烦,因为旧值已经被释放。
在分配这些新值之前,您是否正在调用
[nextView.idActual release]
?如果是,请注释掉该行,看看是否可以解决您的问题。自动生成的 setter 方法将为您释放旧值。It appears that the CFString (NSString) that is contained in
nextView.idActual
has already been released when you go to change the value. If you can post more of the related code, that would help.I'm guessing that
idActual
is declared as@property(nonatomic,retain)
. When you try to set a new value intoidActual
, the setter method for that property is called (It's possible that the setter was automatically generated). The first thing that the setter method is doing is trying to compare the old value and the new value - and then it crashes.When that setter method attempts to compare the new value to the old value, it runs into trouble because the old value is already deallocated.
Are you calling
[nextView.idActual release]
before you assign these new values ? If you are, comment out that line, and see if that fixes your problem. The auto-generated setter method will release the old value for you.