• 締切済み

IBAction内でのEXC_BAD_ACCES

iPhone開発関係で質問です。 IBAction内でNSMutableDictionaryを参照しようとするとEXC_BAD_ACCESSとなってしまい、困っています。メモリ管理が甘いのでしょうか?? プログラムの概要は以下のとおりです。 問題をご存知の方、ご教授下さい。よろしくお願いします。 ---------- FirstViewController.h @interface FirstViewController : UIViewController { NSMutableDictionary *testDic; } @property (nonatomic , retain) NSMutableDictionary *testDic; - (IBAction) TestBtnTapped; @end ---------- FirstViewController.m #import "FirstViewController.h" @implementation FirstViewController @synthesize testDic; /* testDicの受け渡しがうまくいかない TestBtnTappedでEXC_BAD_ACCESS発生 IBActionのせい? */ - (IBAction) TestBtnTapped{ NSlog(@"%@" , testDic); } - (void)viewDidLoad { [super viewDidLoad]; testDic = あるNSMutableDictionaryデータ NSlog(@"%@" , testDic ); //ここでは表示される } - (void)dealloc { [super dealloc]; }

みんなの回答

  • harawo
  • ベストアンサー率58% (3742/6450)
回答No.1

> - (void)viewDidLoad { > [super viewDidLoad]; > testDic = あるNSMutableDictionaryデータ > NSlog(@"%@" , testDic ); //ここでは表示される > } 「//ここでは表示される」では、なにが表示されますか? testDicは、文字列定数でも、NSStringインスタンスでもないので、なにかが表示されるのはおかしく、エラーになるのが正常です。 おまけ: - (void)dealloc { [testDic release]; // 初期化メソッド(viewDidLoad)で、生成したインスタンスは、かならずどこかで開放してください。 [super dealloc]; }

関連するQ&A

  • iPhoneアプリのプログラミングに関する質問です

    iOSでiPhoneアプリの開発をしています。 今までに5本のアプリをアップストアにアップしているのですが、プログラムに関してアマチュアなので、サンプルを参考にしながら、稚拙なプログラム(配列などは使っていません。)を作っています。 6本目として作ったアプリを動かしてみたところ、1回目は普通に動くのですが、シミュレーターの下にある□ボタンで終了させた後に、再度起動させて動かすと、UIImageを呼び出すところで、フリーズしたり落ちたりします。 アップルのサンプルコードやチュートリアルなども見ているのですが、残念ながら私の力では、それらのチュートリアルで今回の問題を解決することは出来ませんでした。 下にあるのが、コードの一部ですが、[hana setImage:hana1]; のところで、Thread 1:Program received signal: "EXC_BAD_ACCESS".というエラーが出ます。メモリへの二重のアクセスが原因なのかもしれないと考えて、メモリを解放するために @property (nonatomic, retain) UIImage * hana0; @property (nonatomic, retain) UIImage * hana1; と @synthesize hana0; @synthesize hana1; と [hana0 release]; [hana1 release]; を追加しましたが、効果はありませんでした。 自分自身で何が問題か分かっていないために説明が間違っている可能性もあるので、この質問だけで解決していただくことは難しいと思いますが、どのような方向で考えるとよいのかだけでも教えていただくと助かります。 よろしくお願いします。 hのファイルです。 @interface MondaiViewController : UIViewController { UIImage * hana0; UIImage * hana1; IBOutlet UIImageView *hana; } @property (nonatomic, retain) UIImage * hana0; @property (nonatomic, retain) UIImage * hana1; -(IBAction) kaisi:(id)sender; -(IBAction) marutuke:(id)sender; @end mファイルです。 @implementation MondaiViewController @synthesize hana0; @synthesize hana1; -(IBAction) marutuke:(id)sender{ if (jidai == 0 && syoukou == 0 && tugi == 0) { int miss = 0; int kai = [[kaitou text] intValue]; if (kai == 345) { [kaitou setTextColor:[UIColor blackColor]]; [hana setImage:hana1]; //Thread 1:Program received signal: "EXC_BAD_ACCESS". } else { [kaitou setTextColor:[UIColor redColor]]; [hana setImage:hana0]; tugi = tugi - 1; miss = 1; } } - (void)dealloc { [hana0 release]; [hana1 release]; [super dealloc]; }

  • Obj-Cで生成元のViewへ文字列を表示させたい

    iOS SDK(Objective-C)を勉強中なのですが、あるView(TestViewController.view)から別のView(ButtonViewController.view)を表示し、表示したViewにあるボタンを押した時に、生成元のView(TestViewController.view)のラベル(test_label)に文字列を表示したいのですが、色々やってみたものの上手く行かず、どの用に実装するのが正しいのか行き詰まってしまいました。どなたか手助けよろしくお願いします。 //*** TestViewController.h #import <UIKit/UIKit.h> #import "ButtonViewController.h" @interface TestViewController : UIViewController { IBOutlet UILabel *test_label; ButtonViewController *buttonViewController; } - (IBAction) pushButton:(id)sender; @end //*** TestViewController.m #import "TestViewController.h" @implementation TestViewController - (void)viewDidLoad { [super viewDidLoad]; test_label.text = @"Push Button"; } //ButtonViewを表示 - (IBAction) pushButton:(id)sender { buttonViewController = [[ButtonViewController alloc] initWithNibName:@"ButtonView" bundle:nil]; [self.view addSubview:buttonViewController.view]; } - (void)dealloc { [buttonViewController release]; [super dealloc]; } @end //*** ButtonViewController.h #import <UIKit/UIKit.h> @interface ButtonViewController : UIViewController { } - (IBAction) pushButtonSub:(id)sender; @end //*** ButtonViewController.m #import "ButtonViewController.h" @implementation ButtonViewController - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction) pushButtonSub:(id)sender { // ??? ← このボタンを押したときにtest_labelへ表示させたい } - (void)dealloc { [super dealloc]; } @end

  • Objective-C,viewが動作しない

    IBを使わないカメラアプリを作っています。 AppDelegate.m内のapplication didFinishLaunchingメソッド内で生成したwindowとviewはコンパイルして表示確認できたのですが、viewControllerで書いたUIImagePickerViewControllerのviewが出てきません。 そもそもviewController.mのloadViewメソッドやviewDidLoadメソッドが動いていません。(メソッド内にNSLogを書いてコンパイルしてもログに出ない) viewController.h #import <UIKit/UIKit.h> @interface CameraViewController : UIViewController <UINavigationControllerDelegate,UIImagePickerControllerDelegate> { @private UIImagePickerController* _imagePicker; } @property(nonatomic,retain) UIImagePickerController *imagePicker; @end viewController.m #import "CameraViewController.h" @implementation CameraViewController @synthesize imagePicker=_imagePicker; - (void)dealloc { [super dealloc]; } #pragma mark - View lifecycle - (void)loadView { [super loadView]; //UIImagePickerControllerの作成 if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){ return; } UIImagePickerController* imagePicker; imagePicker = [[UIImagePickerController alloc] init]; [imagePicker autorelease]; imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; imagePicker.allowsEditing=NO; imagePicker.delegate = self; //imagePickerのviewを最上部に [self presentModalViewController:imagePicker animated:YES]; //シャッターを切る [self.imagePicker takePicture]; } @end viewControllerが機能しない原因としてそういった可能性が考えられるのでしょうか? あれこれ調べてみましたが煮詰まっています。よろしくお願いします

  • Objective-Cのメモリ管理

    はじめまして。 iphoneアプリを作っていてメモリ管理について不明な点があるので教えて下さい。 memoryViewController.h ============================================== #import <UIKit/UIKit.h> @interface memoryViewController : UIViewController { id obj; } @property(retain)id obj; @end =============================================== memoryViewController.mm(一部) =============================================== @implementation memoryViewController @synthesize obj; - (void)viewDidLoad { [super viewDidLoad]; self.obj = [[NSObject alloc] init]; NSLog(@"count = %d", [obj retainCount]); } =============================================== 下記の部分についてです。 self.obj = [[NSObject alloc] init]; 「self.」を付けた場合retainCountは2で、付けなかった場合retaionCountは1になります。 何故違ってくるのかが理解できません。 教えて下さい。

  • objectiv-cで困っています。教えてください

    objectiv-cの勉強を始めたばかりです。ネットで調べながら進めているのですが、プロジェクト内のテキストファイルではなく、サーバにあるテキストファイルの内容を読み込み、NSLogに出力しようとしているのですが、うまくいきません。xxxxxxxxxxxの部分にテキストファイルのあるURLを入れています。 -----------------textloadViewController.h------------------------------ #import <UIKit/UIKit.h> @interface textloadViewController : UIViewController @end -----------------textloadViewController.m------------------------------ #import "textloadViewController.h" @interface textloadViewController () @end @implementation textloadViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. NSError *error = nil; NSString *filePath = [[NSBundle mainBundle] pathForResource:@"xxxxxxxxxxx" ofType:@"txt"]; NSString *string = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error]; NSLog(@"%@", string); } @end 外部のテキストファイルを読み込む方法を教えてください。よろしくお願いします。

  • XcodeのObjective-Cについて

    大変初歩的な質問でお恥ずかしいのですが、最近Xcodeを初めて本で少しずつObjective-Cを勉強中の初心者です。 MainStoryboardでLabelをViewに配置した後、 「画面を表示する準備ができたときにコンソール画面に"こんにちは"と表示させるプログラムを追加」というものが載っているのですが、指示通り打ち込んでもエラーでうまくいきません。 本には「m.」ファイル上の「@implementation」の次の行に 01 - (void)viewDidLoad 02 { 03   [super viewDidLoad]; 04   NSLog(@"こんにちは"); 05 } とあり、その通りに打ち込んだのですが(以下Xcodeよりコピペ) @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"こんにちは"); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } となり、 NSLog(@"こんにちは"); } の次の行の - (void)viewDidLoad に赤い!マークのエラーが表示されてしまいます。 初心者のため訂正方法もわからずネット検索しても解決しなかったので、 大変申し訳ないのですが教えていただきたく思います。

  • iPhone開発ViewControllerのメモリ開放について

    iPhone開発ViewControllerのメモリ開放について iPhone開発ViewControllerの動きについて質問させて下さい。 XIBで指定したコントロールに対して、hファイルでアウトレットを宣言した変数はviewDidUnloadでリリースすべきか、deallocでリリースべきかを教えて頂けないでしょうか。 ただ、これには前提の条件が必要と思いますので、条件を記述いたします。 以下の条件下でどちらが良いのか教えて頂けないでしょうか。理由も合わせてお願いします。 viewDidUnloadに記述すべきと記述している資料が多いような気がしますが、deallocに記述している資料も多いのです。 deallocはおまじないで記述しているだけなのでしょうか? 前提条件 1、Xcodeバージョン3.2.3 2.XCODEの新規のファイルのテンプレートの選択でUIViewController subclassで   Viewとビューコントローラを作成します。 3.アウトレットの宣言   @interface TestView : UIViewController {    UIButton *btn1;   }   @property(nonatomic, retain) IBOutlet UIButton *btn1;   @end      @implementation TestView   @synthesize btn1;   ・・・・   処理   ・・・・   @end 4.TestViewはモーダル表示で、任意の画面から表示します。   TestView *tv = [[TestView alloc] init];   tv.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;   [self presentModalViewController:tv animated:YES];   [tv release]; 5.TestView画面の終了はTestView上のボタン押下で画面が閉じます。   [self.parentViewController dismissModalViewControllerAnimated:YES]; 6.処理中にはメモリ不足によるdidRecieveMemoriWarningは発生しない前提です。 上記の場合、NSLogでイベントの発生を見ると以下のようになります。 2010-08-16 23:42:44.454 MemoryTest[1606:207] TestView - viewDidLoad 2010-08-16 23:42:44.458 MemoryTest[1606:207] TestView - viewWillAppear 2010-08-16 23:42:46.117 MemoryTest[1606:207] TestView - viewWillDisappear 2010-08-16 23:42:46.839 MemoryTest[1606:207] TestView - dealloc didRecieveMemoriWarningが発生しないので、ViewDidUnLoadも発生しません。 ここまでは理解できますが、ViewDidUnLoadのみに[btn1 Release];を記述した場合、btn1は開放されるのでしょうか。 ビューコントローラのdeallocは発生しているので、OKということでしょうか? dealloc内の[super dealloc];XIBに関連するものは全て開放してくれるのでしょうか?それなら記述しなくて良い? それともViewDidUnLoadとbtn1の両方に記述すべきなのでしょうか? ビューコントローラのdeallocが発生しているということは、btn1も開放されているということですか? ビューコントローラのdeallocが発生しているけど、btn1も開放されないこともあるのでしょうか? すみません。次から次への疑問がわきます。多分、この辺の仕組みを理解していないのだと思いますが、いろいろ調べたのですが、納得する答えが見つかりませんでした。 どうかよろしくお願いします。

  • インスタンス変数の参照について

    おせわになります。 iOS xcodeのビギナーです。 ひとつのクラスで定義したインスタンス変数を、別のクラスで参照しようとしても、 内容がnilとなってしまいます。 参考書やインターネットでいろいろ調べたのですが、混乱してしまっております。 よろしくお願い致します。 RootViewController.h RootViewController.m OAutoView.h OAutoView.m RootViewContrller で取得した文字データを、OAutoView上のtextViewで表示したいのですが、 ロジックの問題?で中身にnilが入っているようです。 ------------------------------------------ RootViewController.h @property (retain) NSString *stringValue; ------------------------------------------ RootViewController.m @synthesize stringValue; - (NSString *)stringValue{ return stringValue; } - (void)setstringValue:(NSString *)value{ stringValue = value; } 下記でsDataをstringValueへセットしています。 sDataの中には、文字データが入っております。 - (void)tableView....... { stringValue = sData; } ------------------------------------------- OAutoView.h 下記でstringValueを定義したRootViewControllerクラスのインスタントを作成しています。 @class RootViewController; @interface OAutoView : UIViewController { UITextView *textView; RootViewController *rootViewController; } @property (nonatomic, retain) IBOutlet UITextView *textView; @property (retain) RootViewController *rootViewController; @end -------------------------------------------- OAutoView.m 下記で一旦 tDataに、RootViewControllerクラスで作成したstringValueを セットしていますが、既にこの時点で内容はnilのようです。 - (void)viewDidLoad { NSString *tData = [rootViewController stringValue]; rootViewController.stringValue = tData;       //[rootViewController setstring:tData] <--なぜかコンバイルエラーとなってしまいます。 textView.text = tData; [super viewDidLoad]; }

  • Xcode4のエラー(IPhoneアプリ)

    はじめまして。 IPhoneのアプリ作成に挑戦しています。 Xcode4のバージョンは 4.5.1です。 【はじめてのXcode4プログラミング】という参考書でIPhoneのカメラアプリを作る章があり、写真ライブラリから選択した画像を表示できるとのことです。 しかし、XcodeでRun(実行)すると、ViewController.mの最後2行【UIImage *originalImage =[info objectForkey:UIImagePickerControllerOriginalImage];】が赤くなり、【’dismissModalViewControllerAnimated’is deprecated: first deprecated in iOS 6.0】というエラー表示がでます。 全体のコードは下記の通りです。 ネットで調べてみましたが、解決方法がわかりません。 先に進めず困っています。 どうか解決方法を教えてください。 よろしくお願い致します。 ●ViewController.h// // ViewController.h // CameraApp // #import @interface ViewController : UIViewController - (IBAction)pressCameraButton:(id)sender; @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end ●ViewController.m // // ViewController.m // CameraApp // #import “ViewController.h” @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)pressCameraButton:(id)sender { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentModalViewController:picker animated:YES]; } - (void)imagePickerController:(UIImagePickerController*)Picker didFinishPickingMediaWithInfo:(NSDictionary*)info { [self dismissModalViewControllerAnimated:YES]; UIImage *originalImage = [info objectForkey:UIImagePickerControllerOriginalImage]; self.imageView.image = originalImage; } @end

  • Xcodeのクラスについてお聞きします。

    何らかのクラスをつくり、ViewControllerからクラスを生成して そのクラス(以下TestClass)のメソッドにアクセスします。 TestClassのメソッドから呼び出し元のViewControllerの変数若しくはメソッドに アクセスすることはできるのでしょうか? 例) @interface ViewController : UIViewController{ UIlabel *label; //TestClassから書き込みたい } @end @implementation ViewController -(void)viewDidLoad{ TestClass *class = [[TestClass alloc] init]; [class labelWrite];//クラス呼び出し } - (void)hoge{ //TestClassからここにアクセスしたい //処理 } @end @interface TestClass : NSObject @end @implementation TestClass - (void)labelWrite{ //ここからViewControllerのlabelに書き込みたい //もしくはViewControllerのhogeメソッドにアクセスしたい } @end