0

このエラー「EXC_BAD_ACCESS」に何時間も悩まされており、何が問題なのかがわかりません。うまくいけば、あなたの何人かはそれを見つけるでしょう。

json-data を含むファイルをダウンロードし、カスタム NSObject として NSMutableArray に保存してから、UITableView に表示します。問題が発生したのは、UITableView で表示するときです。

ViewController.h

@property (nonatomic, retain) NSMutableArray *menuItems;

ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    menuItems = [[NSMutableArray alloc] init];
}

- (void)viewDidAppear:(BOOL)animated
{
    [self downloadMenu];

}

-(void)downloadMenu
{
    dispatch_queue_t fetchfromServerQueue = dispatch_queue_create("fetchfromServer", NULL);

    [self createProgressionAlertWithMessage:@"Fetching menu"];

    dispatch_async(fetchfromServerQueue, ^{

        NSURL *menuURL = [NSURL URLWithString:@"anURL"];

        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:menuURL];

        NSError *error = nil;
        NSURLResponse *response;
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

        responseArray = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

        menuItems = [[NSMutableArray alloc] initWithCapacity:[responseArray count]];

        for (int i = 0; i<[responseArray count]; i++) {
            NSDictionary *menuObject = [responseArray objectAtIndex:i];

            NSString *x = [NSString stringWithFormat:@"%@", [menuObject objectForKey:@"x"]];
            NSString *y = [NSString stringWithFormat:@"%@", [menuObject objectForKey:@"y"]];
            NSString *z = [NSString stringWithFormat:@"%@", [menuObject objectForKey:@"z"]];

            MenuItem *menuItem = [[MenuItem alloc] initWithX:x andY:y andZ:z];

            [menuItems addObject:menuItem];

            [menuItem release];
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            [progressAlert dismissWithClickedButtonIndex:0 animated:YES];
            [menuTable reloadData];
        });
    });
    dispatch_release(fetchfromServerQueue);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [menuItems count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    MenuItem *menuItem = (MenuItem *)[menuItems objectAtIndex:indexPath.row];

    cell.textLabel.text = [NSString stringWithFormat:@"%@", [menuItem x]];

    return cell;
}

「cell.textLabel.text = [NSString stringWithFormat:@"%@", [menuItem x]];」について 「EXC_BAD_ACCESS (code=1,address=0xXXXXXXXX)」と出ます。

私は ARC を使用しておらず、このプロジェクトで近い将来 ARC に変換することはできません。メモリ管理に関する知識が不足しているため、これが問題になります。

何が問題なのですか?

どうも!

#### #### #### #### #### #### #### #### ####

MenuItem は次のようになります。

#import <Foundation/Foundation.h>

@interface MenuItem : NSObject
{
    NSString *x;
    NSString *y;
    NSString *z;
}

-(id)initWithX:(NSString *)x_ andY:(NSString *)y_ andZ:(NSString *)z_;
-(NSString *)x;
-(NSString *)y;
-(NSString *)z;

@property(retain, nonatomic) NSString *x;
@property(retain, nonatomic) NSString *y;
@property(retain, nonatomic) NSString *z;

@end

#import "MenuItem.h"

@implementation MenuItem

@synthesize x;
@synthesize y;
@synthesize z;

-(id)initWithX:(NSString *)x_ andY:(NSString *)y_ andZ:(NSString *)z_
{
    self = [super init];
    if(self)
    {
        x = x_;
        y = y_;
        z = z_;
    }

    return self;
}

-(void)dealloc
{
    [x release];
    [y release];
    [z release];
    [super dealloc];
}

-(NSString *)x
{
return x;
}

-(NSString *)y
{
    return y;
}

-(NSString *)z
{
    return z;
}

@end
4

2 に答える 2

1

明らかなことは何も見えません。おそらく何かがmenuItem問題を引き起こしているのでしょうか? 何をし[menuItem x]ますか?

通常は、ゾンビ インストゥルメント (cmd-i) を使用してアプリを実行し、[ゾンビ] を選択します。これにより、EXC_BAD_ACCESS をスローしているオブジェクトの割り当て/解放のトレースが得られます。

幸運を。

于 2013-05-06T23:06:45.250 に答える
0

これは少し奇妙です:

-(void)downloadMenu
{
    dispatch_queue_t fetchfromServerQueue = dispatch_queue_create("fetchfromServer", NULL);
    dispatch_async(fetchfromServerQueue, ^{
        ...
    });
    dispatch_release(fetchfromServerQueue);
}

キューを作成し、非同期タスクを実行させます。そのタスクが何らかの作業を行うためにオフになっている間、すぐにキューを解放します。解放する前に非同期タスクが終了しない場合、これによりいくつかの問題が発生する可能性があります。

非同期呼び出しでの作業が完了するまで、リリースを保留することをお勧めします。

于 2013-05-06T23:39:44.050 に答える