从外部类使用 ASIHTTPRequest
这是另一个新手问题。
我想从外部类调用 ASIHTTPRequest (因为我已经在我的主类中使用它),并且我执行如下操作:
ASIHTTPNSFabExt *nRequest = [[ASIHTTPNSFabExt alloc]init];
nRequest.URL = @"http://something";
nRequest.var1 = [dictionaryRecord objectForKey:@"something"];
nRequest.var2 = [dictionaryRecord objectForKey:@"something"];
[nRequest saveComment];
saveComment 方法执行 ASIHTTPRequest 任务。
一切工作正常,除了当我尝试在主类中执行 [nRequest release] 时,这会在 ASIHTTPRequest 方法中生成错误。
在我释放之前保留计数为 1 并且自动释放也不起作用:我确信我缺少一些内存管理基础知识,但是你能帮助我寻找正确的方法来操纵它吗?
提前感
谢法布里奇奥
here it's another newbie question.
I want to call ASIHTTPRequest from an external class (because I already use it in my main class) and I do something like this:
ASIHTTPNSFabExt *nRequest = [[ASIHTTPNSFabExt alloc]init];
nRequest.URL = @"http://something";
nRequest.var1 = [dictionaryRecord objectForKey:@"something"];
nRequest.var2 = [dictionaryRecord objectForKey:@"something"];
[nRequest saveComment];
Where saveComment methods perform the ASIHTTPRequest tasks.
Everything is working ok except when I try to do [nRequest release] inside my main class that will generate a error inside ASIHTTPRequest methods.
Retain Count before my release is 1 and autorelease doesn't work too: I'm sure I'm missing some memory management fundamentals but can you help me to search for the correct way to manipulate it?
Thanx in advance
Fabrizio
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
到目前为止,您发布的代码没有任何明显的错误。问题可能出在您在 ASIHTTPNSFabExt 的 init/dealloc 方法中处理对 ASIHTTPRequest 对象的引用时。
将 ASIHTTPRequest 包装在另一个 Objective-C 类中没有什么特别的 - 只是标准确保您在正确的位置保留/释放该对象,并且在释放它后不使用它。
我假设你是个聪明人,所以你几乎肯定可以使用在 iOS 上调试内存问题的标准技术自己解决这个问题。
首先,在启用 NSZombies 的情况下运行;检查控制台/应用程序停止的位置,您几乎肯定会发现一个错误,提示“消息已发送到已释放的对象”,并且回溯可以准确显示哪一行代码尝试发送该消息。
然后您只需弄清楚为什么该行代码发送到已释放的对象。可能是缺少“保留”或“释放”太早,或者后面没有将 nil 分配给保存该对象的实例变量。
-- 编辑 --
另外值得一提的是,您应该确保在 dealloc 方法中从 ASIHTTPRequest 中删除委托,否则它可能会在释放后尝试调用您的类 -
request.delegate = nil; 或<代码>[请求clearDelegatesAndCancel];。
There's nothing obviously wrong with the code you've posted so far. The problem is possibly in your handling of the reference to the ASIHTTPRequest object within the init/dealloc methods in ASIHTTPNSFabExt.
There's nothing special you have to do with wrapping ASIHTTPRequest within another Objective-C class - just the standard make sure you're retaining/releasing the object at the right points, and not using it after you've released it.
I assume you're a clever guy, so you can almost certainly figure this out yourself using the standard techniques for debugging memory problems on iOS.
First, run with NSZombies enabled; check the console / where the app stops, you'll almost certainly find an error say "Message sent to deallocated object ", and a backtrace that shows you exactly which line of code tried to send that message.
Then you just have to figure out why that line of code is sending to a deallocated object. It'll probably be a missing "retain" or a "release" that is too early or isn't followed by assigning nil to the instance variable that was holding the object.
-- Edit --
Also worth mentioning is that you should make sure you remove the delegate from ASIHTTPRequest in your dealloc method, otherwise it might try to call your class after it has been deallocated -
request.delegate = nil;
or[request clearDelegatesAndCancel];
.