NSAutoreleasepool 和 NSLog 的奇怪问题
我有一个奇怪的问题,如果我在代码中添加 NSLog 语句,这个问题就会得到解决。 我有一个带有搜索栏的 UITableviewController 。我正在通过以下方式进行初始获取以填充表:
NSAutoreleasePool *pool2 = [[NSAutoreleasePool alloc]init];
self.listContent = [MainFunctions populateArrayFromModel]; //Get the array populated here from the model data
[pool2 release];
在 MainFunctions 的静态方法内,数组的填充方式如下:
NSMutableArray *resultArray = [[[NSMutableArray alloc] init] autorelease];
MModel *mainModel = [[MModel alloc] init];
//get all results from FetchedResultsController
[mainModel release];
return resultsArray;
上述代码仅在我遵循特定模式时才有效,即我需要先按相关选项卡。如果我在来到这里之前转到另一个选项卡,则此代码会因某种原因挂起。
但是,如果我在代码中放入一些 NSLog 语句,它就会始终有效。像下面这样;
NSMutableArray *resultArray = [[[NSMutableArray alloc] init] autorelease];
NSLog(@"1");
MModel *mainModel = [[MModel alloc] init];
//get all results from FetchedResultsController
NSLog(@"2");
//....code here
NSLog(@"3");
//....code here
NSLog(@"4");
[mainModel release];
return resultsArray;
我曾多次尝试将其取下并重新戴上,并且始终显示相同的行为。 NSLog 与此有什么关系,我很困惑。
感谢任何指点。
I have a strange issue which gets resolved if I put an NSLog statement in the code.
I have an UITableviewController with search bar. I am doing the initial fetch to populate the table in the following way:
NSAutoreleasePool *pool2 = [[NSAutoreleasePool alloc]init];
self.listContent = [MainFunctions populateArrayFromModel]; //Get the array populated here from the model data
[pool2 release];
Inside the static method of MainFunctions, the array is populated like this:
NSMutableArray *resultArray = [[[NSMutableArray alloc] init] autorelease];
MModel *mainModel = [[MModel alloc] init];
//get all results from FetchedResultsController
[mainModel release];
return resultsArray;
The above code works only if I follow a particular pattern, i.e I need to press the related tab first. If I go to another tab before I come here, this code hangs for some reason.
However, if I put some NSLog statements in the code, it consistently works. like below;
NSMutableArray *resultArray = [[[NSMutableArray alloc] init] autorelease];
NSLog(@"1");
MModel *mainModel = [[MModel alloc] init];
//get all results from FetchedResultsController
NSLog(@"2");
//....code here
NSLog(@"3");
//....code here
NSLog(@"4");
[mainModel release];
return resultsArray;
I have tried taking it off and putting it back on multiple times and it consistently shows the same behaviour. What has NSLog got to do with this, I am baffled.
Appreciate any pointers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每当出现挂起/死锁时,您应该转储每个线程的调用堆栈以找出阻塞的原因。
为此,请将应用程序置于挂起状态,然后按“暂停”按钮(或从菜单中选择“产品”>“调试”>“暂停”)。然后,您将在左侧看到线程列表及其调用堆栈。展开每个线程以查看其调用堆栈。您还可以在调试控制台中使用以下命令之一来显示调用堆栈的文本(取决于您是使用 gdb 还是 lldb 进行调试):
如果需要更多帮助诊断问题,使用其中一个命令收集文本回溯并将其粘贴到注释中,以便我们查看。
Whenever you have a hang/deadlock, you should dump the call stacks of each thread to figure out what's blocking.
To do this, put your app into the hung state and then press the Pause button (or from the menus, Product > Debug > Pause). You will then see the list of threads and their call stacks on the left. Expand each thread to view its call stack. You can also a text representation of the call stacks using one of the following commands in the debug console (depending on whether you're debugging with gdb or lldb):
If you need more help diagnosing the problem, collect a text backtrace using one of those commands and paste it into a comment so we can take a look.