为什么assert只是终止为iPhone编译的程序? [英] Why does assert simply terminate a program compiled for iPhone?

查看:121
本文介绍了为什么assert只是终止为iPhone编译的程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调试一个大量的assert()的iPhone应用程序(Xcode,Objective C ++,设备模拟器)。在某些情况下,断言失败只会终止应用程序,而不是按照我的预期进入调试器。



我通过实现自己的kinda-声明:

  #define AssertLite(b)if(!(b)){asm {int 3}} 

(绒毛省略),但我不知道有没有人遇到过这个。我不能确定一个模式,它什么时候中断,什么时候终止。代码没有线程,所有这些都是在事件处理程序中完成的。



编辑:um,问题是:为什么会发生这种情况,我如何使香草断言()表现得像一个条件断点应该是?

解决方案

首先,由于你正在使用iPhone应用程序,可能使用NSAssert()而不是香草BSD断言功能。



eg NSAssert(the_object,@NIL object meets);



NSAssert宏将抛出一个Objective-C异常( NSInternalInconsistencyException )如果断言失败。



由于您的目标是打破异常,下一步是使Xcode调试器在Objective-C异常中断。



在断点窗口(Run-> Show-> Breakpoints菜单项)中,单击双击符号输入符号 - [NSException raise]



最后要注意的是NSAsserts 在发布版本中编译。这意味着您必须准备好处理应用程序中的异常,或者您需要创建自己的在编译版本中编译的宏。



这是宏我用来编译运行时代码中的断言(请注意,然后我在代码中使用HMAssert而不是NSAssert):

  ifdef DEBUG 
#define HMAssert(A,B)NSAssert(A,B)
#else
#define HMAssert(A,B)
#endif

这需要定义一个DEBUG预处理器宏。以下是如何设置:


  1. 在Xcode中右键单击您的项目。这将是您的项目文件列出的左侧面板中的顶级项目

  2. 从弹出的上下文菜单中选择获取信息。

  3. 转到构建选项卡。

  4. 确保配置设置为调试。

  5. 在下一个字段中键入DEBUG到GCC 4.2 - 预处理下的预处理宏。


I'm debugging a heavily assert()'ed iPhone app (Xcode, Objective C++, device simulator). In some cases, the assert failure would just terminate the app, instead of breaking into the debugger as I'd expect.

I made a workaround by implementing my own kinda-assert to the effect of:

#define AssertLite(b) if(!(b)) {asm {int 3}}

(fluff omitted), but I wonder if anyone ever encountered this. I could not determine a pattern as to when does it break and when does it terminate. The code is not threaded, all it does is done in event handlers.

EDIT: um, and the question is: why does this happen and how do I make vanilla assert() behave like a conditional breakpoint it should be?

解决方案

First off, since you are working on an iPhone app, you should probably use NSAssert() instead of the vanilla BSD assert function.

e.g. NSAssert(the_object, @"NIL object encountered");

The NSAssert macro will throw an Objective-C exception (NSInternalInconsistencyException) if the assertion fails.

Since your goal is to break on the exception, the next step is to make the Xcode debugger break on Objective-C exceptions. This is probably a good thing to do anyway.

In the Breakpoints window (Run->Show->Breakpoints menu item), click where it says "Double-Click for Symbol" to enter the symbol -[NSException raise]

The last thing to be careful off is that NSAsserts do not compile out in a release build. That means that you have to either be prepared to handle the exception in your application, or you need to create your own macro that does compile out in release builds.

Here's the macro I use to compile out assertions in runtime code (note that I then use HMAssert in my code instead of NSAssert):

#ifdef DEBUG
#   define HMAssert(A,B) NSAssert(A,B)
#else
#   define HMAssert(A,B)
#endif

This requires a DEBUG preprocessor macro to be defined. Here's how to set that up:

  1. Right-click on your project in Xcode. That will be the top item in the left panel where your projects files are listed
  2. Select "Get Info" from the context menu that pops up.
  3. Go to the "Build" tab.
  4. Make sure the "Configuration" is set to "Debug".
  5. Type DEBUG into the field next to "Preprocessor Macros" under "GCC 4.2 - Preprocessing".

这篇关于为什么assert只是终止为iPhone编译的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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