在Objective C和XCode中抛出的调试异常 [英] Debugging exception thrown in Objective C and XCode

查看:112
本文介绍了在Objective C和XCode中抛出的调试异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Microsoft开发人员,我是使用XCode进行iPhone开发的新手。所以,我正在读一本书,并通过示例尝试自学如何使用Objective-C编写iPhone应用程序。到目前为止,一切都很顺利,但偶尔我会在运行时遇到通用的 objc_exception_throw 消息。发生这种情况时,很难找到此异常的来源。经过一些反复试验,我找到了答案。其中一个参数拼写错误。

I am a long time Microsoft developer and I am new to iPhone development using XCode. So, I am reading a book and going through examples trying to teach myself how to write an iPhone application using Objective-C. All has been good so far, however, once in a while I run into the generic 'objc_exception_throw' message at runtime. When this happens, the source of this exception is very difficult to find. After some trial and error I found my answer. One of the parameters was misspelled.

正如你在下面看到的那样,我错过了'otherButtonTitles'参数,省略了第二个't'按钮。

As you can see below, I misspelled the 'otherButtonTitles' parameter by leaving out the second 't' in button.

UIAlertView *alert = [[UIAlertView alloc] 
                      initWithTitle:@"Date and Time Selected" 
                      message:message 
                      delegate:nil
                      cancelButtonTitle:@"Cancel"
                      otherButonTitles:nil];

我花时间寻找的原因是代码构建成功。这是Objective-C编译器的正常行为吗?当我像这样做一个常见的语法错误时,我习惯于在.NET编译器中使构建失败。是否有编译器设置我可以更改以在构建这些错误时使构建失败?

The reason this took me time to find is that the code built successfully. Is this normal behavior for the Objective-C compiler? I am used to having the build fail in the .NET compiler when I make a common syntax error like this. Is there a compiler setting I can change to make the built fail when I make these mistakes?

推荐答案

首先,打开〜/ .gdbinit (这是您的主目录中名为 .gdbinit 的文件 - 是的,以点开头)并将其放入其中:

First and foremost, open ~/.gdbinit (that's the file called .gdbinit in your home directory - yes, starts with a dot) and put this in it:

fb -[NSException raise]
fb objc_exception_throw
fb malloc_error_break

这将使用三个默认断点初始化GDB,当它们发生时,GDB将暂停您的应用程序并向您显示堆栈跟踪。这与Xcode很好地集成,因此只要在某处发生异常或malloc失败,您就可以通过单击堆栈跟踪元素来很好地遍历代码。

That'll initialize GDB with three default breakpoints, when they occur, GDB will halt your application and show you the stack trace. This is very well integrated with Xcode so you'll be able to nicely walk through your code by clicking stack trace elements as soon as an exception occurs somewhere or a malloc fails.

然后,在项目中打开获取信息面板(或选择您的项目(组和文件)并点击 cmd -i ),转到 Build 标签并设置项目的基本SDK 设备 - iPhone OS [someversion] 。一直滚动到底部,找到 GCC 4.0 - 警告部分。那里;打开尽可能多的警告,但请确保打开将警告视为错误(这相当于 GCC_TREAT_WARNINGS_AS_ERRORS )。我个人认为:

Then, open the Get Info panel on your project (or select your project (top item in the Groups & Files) and hit cmd-i), go to the Build tab and set your project's Base SDK to Device - iPhone OS [someversion]. Scroll all the way to the bottom and find the GCC 4.0 - Warnings section. There; turn on as many warnings as you feel comfortable with, but make sure to turn on Treat Warnings as Errors (this is the equivalent of GCC_TREAT_WARNINGS_AS_ERRORS). Personally, I have it set to this:

GCC警告构建设置http://lhunath.lyndir.com/stuff/gcc_warnings.png

您现在应该收到大多数事情的编译器警告可以在代码中做错,编译器不会让你运行代码,直到你修复它们。当事情确实越过编译器的鼻子时,你应该能够轻松找到问题,GDB在方便的位置破解。

You should now be getting compiler warnings for most things you can do wrong in code and the compiler won't let you run the code until you fix them. When things do get past the compiler's nose, you should be able to find the problem easily with GDB breaking at a convenient spot.

你还应该看看 NSZombie * 。这些环境变量非常便于早期破坏不良内存分配或访问情况。例如; wih NSZombieEnabled 什么都不会真正发布;在dealloc上它会被 _NSZombie 覆盖,如果你再次尝试访问这个解除分配的内存(取消引用一个解除分配的指针),你将在GDB中获得一些东西,而不是呼叫通过正常情况,只发布在随机数据上(当然,这不是你想要的)。有关详细信息,请参阅 http://www.cocoadev.com/index.pl?NSZombieEnabled

You should also look into NSZombie*. These are environment variables that are very handy for early breaking on bad memory allocation or access situations. For instance; wih NSZombieEnabled nothing will truly be released; on dealloc it'll get overwritten with _NSZombie and should you try to access this dealloced memory again (dereferencing a dealloced pointer) you'll get something to break on in GDB, instead of the call going through like normal, only being issued on random data (which, of course, isn't what you wanted). For more info on this, see http://www.cocoadev.com/index.pl?NSZombieEnabled.

这篇关于在Objective C和XCode中抛出的调试异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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