无法识别的选择器发送到实例0x5d18d60 ...我会疯了! [英] unrecognized selector sent to instance 0x5d18d60... i'm going crazy!

查看:99
本文介绍了无法识别的选择器发送到实例0x5d18d60 ...我会疯了!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是应用程序的源代码: Smoking.zip



它只保存带有NSMutableArray的.dat文件。
现在,第一次你将启动应用程序,尝试点击香烟按钮有时:一切都应该工作正常。
确定,现在关闭应用程序,重新打开它,然后再次单击按钮。这次应用程序将崩溃与无法识别的选择器发送到实例0x5d18d60错误。
我确定问题是在保存数据,因为当我评论行[theData writeToFile:dataFilePath atomically:YES];在saveData方法中的错误消失了。
后来我发现如果我尝试从NSMutableArray读取数据,它会再次出现。



请花一点时间检查我的项目并帮助我,beacause我会疯了!

解决方案

你疯了,花了很多时间才找到这些线: p>

  Cig * oldCig = [mainDelegate.smokeArray lastObject]; 

...

[oldCig release];

为什么要这样做?您可以有效地将数组中最后一个对象的保留计数减少为0.保存时,它会被保存,保留计数为零。



反序列化,解码器将保留其解码的任何(子)元素,因此,最后一个对象的保持计数将短暂地为1.然后,在释放解码器时,它释放所有元素,并且poof到最后一个Cig目的。从数组中获取该对象时,会得到一个指向完全不同的指针的应用程序崩溃。



您应该阅读内存处理。 lastObject 只是返回一个指向数组中的对象的指针,而不保留它,所以你不必释放它。此外,对于

   - (NSArray *)quando 

尝试通过在返回之前调用autorelease来返回自动释放的数组:

  NSArray * newArray = [[[NSArray alloc] initWithObjects:year,...,nil] autorelease]; 

然后你的其他代码不必考虑释放它。并释放dateFormatter。任何您分配保留复制 $ c> new ,您必须发布 autorelease


I'm going crazy with this my little app... Please help me!!!

this is the source code of the app: Smoking.zip

It only saves a .dat file with an NSMutableArray. Now, the first time you will launch the app, try to click the cigarette button sometimes: Everything should working fine. Ok, now close the app, re-open it, and click again on the button. This time the app will crash with the "unrecognized selector sent to instance 0x5d18d60" error. I was sure the problem was in saving the data, because when i commented the line "[theData writeToFile:dataFilePath atomically:YES];" in the "saveData" method the error disappeared. Later i discovered that it appears again if i try to read the data from the NSMutableArray.

Please take a moment to check my project and help me, beacause i'm going crazy about that!!

解决方案

You crazy man, it took quite some time to find these lines:

Cig *oldCig = [mainDelegate.smokeArray lastObject];

...

[oldCig release];

Why are you doing that? You effectively reduce the retain count of the last object in the array to 0. When saving, it is happily saved, with its retain count of zero.

On de-serialization, the decoder will retain any (sub) element it decodes, so the retain count of this last object will, for a brief moment, be 1. Then, on release of the decoder, it releases all elements, and poof goes the last Cig object. When getting that object from the array, you get a pointer pointing to something totally different, and the app crashes.

You should read up on memory handling. lastObject just returns a pointer to an object in the array, without retaining it for you, so you don't have to release it. Furthermore, for functions like

- (NSArray *) quando

try to return an autoreleased array, by calling autorelease before returning:

NSArray *newArray = [[[NSArray alloc] initWithObjects:year,...,nil] autorelease];

Then your other code doesn't have to think about releasing it. And release the dateFormatter. Anything you alloc, retain, copy or new, you must release or autorelease!

这篇关于无法识别的选择器发送到实例0x5d18d60 ...我会疯了!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆