ページ

2009年9月12日土曜日

自動変数を初期化しなかったのが原因で EXC_BAD_ACCESS

このエントリーをブックマークに追加 このエントリーを含むはてなブックマーク

前回までのサンプルを別のMac(MacOS10.5/PowerPC)へ持っていき、ビルドして実行したところエラーを吐いて止まってしまった。

デバッガコンソール

[Session started at 2009-09-12 06:09:45 +0900.]
プログラムをデバッガに読み込み中...
GNU gdb 6.3.50-20050815 (Apple version gdb-962) (Sat Jul 26 08:17:57 UTC 2008)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "powerpc-apple-darwin".プログラムは読み込まれました。
sharedlibrary apply-load-rules all
Attaching to program: `/Users/hashi/Documents/Private/study/CookieStorage/build/Debug/CookieStorage.app/Contents/MacOS/CookieStorage', process 34578.


デバッガを使いトレースしてみると原因がわかった。
原因は単純で自動変数を初期化しなかったこと。

 NSError* error;

NSString* path = [NSString stringWithFormat:@"%@/%@",
[[NSBundle mainBundle] resourcePath], FILENAME];
NSString* contents = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:&error];
if (error) {
NSLog(@"%@", error);
_is_loaded_file = NO;
return;
}


エラーが無いにも関わらず 未初期化状態の error を NSLog( ) で表示しようとしてエラーとなった。

Current language:  auto; currently objective-c
プログラムはシグナルを受信しました:“EXC_BAD_ACCESS”。



対処は初期化コードを書くだけ。
 NSError* error = nil;



ちょっと恥ずかしいミスだった。別の MacOSX10.6 では問題なかったのでたまたま nil になっていたようだ(もしくはコンパイルにオプションがある?)。