【问题标题】:“EXC_BAD_ACCESS" signal received收到“EXC_BAD_ACCESS”信号
【发布时间】:2011-07-17 03:38:17
【问题描述】:

我是 iphone 编程的初学者,我正在做一个照片库应用程序,在其中滚动 UITableView 时,我在设备中收到此异常,因为程序接收到信号:“EXC_BAD_ACCESS”

谁能帮我解决这个问题...

enter cod[super viewDidLoad];
self.title=@"Back";

udf=[NSUserDefaults standardUserDefaults];
id_arr=[[NSMutableArray alloc]init];
descrip_arr=[[NSMutableArray alloc]init];
path_arr=[[NSMutableArray alloc]init];
count_arr=[[NSMutableArray alloc]init];



client = [[[ClientController alloc] init] autorelease];
r_m = [[[RequestMessage alloc]init] autorelease];
m_j = [[[Main_Json alloc]init] autorelease];

[r_m setServiceType:@"IMAGE"];
[r_m setParameter:@"sample string"];

NSString * json_string = [m_j returnJsonString:r_m:@"button"];
json_string=[json_string stringByAppendingFormat: @"\n"];
NSLog(@"Client is sending:%@", json_string);
//NSLog(json_string);

client.connect; 


NSLog(@"client connecting........");

if([client isConnected])
{
    NSString *flag = [client send:json_string];     
    if(![flag isEqualToString:@"success"])
    {

        NSLog(@"Before Alert_view");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:flag delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }

    NSString *Var=[client receiveResponse];




    //NSLog(@"TEST:%@",Var);



    NSString *main_str=[Var stringByReplacingOccurrencesOfString:@"!#{}&" withString:@""];
    //main_str=[Var stringByReplacingOccurrencesOfString:@"{\"serviceType\":\"ALLIMAGE\",\"parameters\":[[]]}" withString:@""];


    NSLog(@"Split:%@",main_str);





    js=[[JsontoObject alloc]sivajiTV_response:main_str];

    e1=[[Event alloc]init];


    NSLog(@"TEST1:%@",[udf objectForKey:@"id_value"]);
    NSLog(@"TEST2:%@",[udf objectForKey:@"des_value"]);
    NSLog(@"TEST3:%@",[udf objectForKey:@"path_value"]);
    NSLog(@"TEST4:%@",[udf objectForKey:@"count_value"]);


    id_arr=[[udf objectForKey:@"id_value"] componentsSeparatedByString:@","];
    NSArray *descrip_arr1 =[[udf objectForKey:@"des_value"] componentsSeparatedByString:@","];
    NSArray *path_arr1=[[udf objectForKey:@"path_value"] componentsSeparatedByString:@","];
    count_arr=[[udf objectForKey:@"count_value"] componentsSeparatedByString:@","];

    //NSLog(@"-------------");
    //NSLog(@"STRING_ID2:%@",descrip_arr1);
    //NSLog(@"STRING_ID3:%@",path_arr1);
    //NSLog(@"-------------");

    for (int i=0; i<[descrip_arr1 count]; i++) {
        NSString *temp=[NSString stringWithFormat:@"%@",[descrip_arr1 objectAtIndex:i]];
        //NSLog(@"STRING_test:%@",temp);
        temp=[temp stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        //NSLog(@"STRING_test1:%@",temp);
        [descrip_arr addObject:temp];

        NSString *temp1=[NSString stringWithFormat:@"%@",[path_arr1 objectAtIndex:i]];
        temp1=[temp1 stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        [path_arr addObject:temp1];
    }


    NSLog(@"------------------------------------------------------------------------------");
    NSLog(@"STRING_ID1:%@",id_arr);
    NSLog(@"STRING_ID2:%@",descrip_arr);
    NSLog(@"STRING_ID3:%@",path_arr);
    NSLog(@"STRING_ID4:%@",count_arr);
    NSLog(@"------------------------------------------------------------------------------");

    if([main_str isEqualToString: [NSNull null]])
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:main_str delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }   
}  
else
{
    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:nil message:@"Connection not Found" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert1 show];
    [alert1 release];

}




UILabel *lblTitle=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 40)];
lblTitle.backgroundColor=[UIColor clearColor];

self.navigationItem.titleView=lblTitle;


toolbar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 25)];
toolbar.barStyle=UIBarStyleBlackOpaque;
[self.view addSubview:toolbar];
UILabel *mylabel = [[UILabel alloc] initWithFrame:CGRectMake(250, 2, 60, 20)];
mylabel.backgroundColor=[UIColor clearColor];
mylabel.textColor = [UIColor whiteColor];
mylabel.text = @"Gallery";
[toolbar addSubview:mylabel];


myTableView.frame=CGRectMake(0,26, 320,430);
//  myTableView.backgroundView=nil;
[self.view addSubview:myTableView];
//self.myTableView.separatorColor = [UIColor clearColor];
//UITableViewController *myTableViewController = [[UITableViewController alloc] initWithTableStyle:UITableViewStyleGrouped];
//  UIToolBar *myToolBar = [[UIToolBar alloc]initWithFrame:CGRectMake(0, 15, 320, 10)];
//  CGRect *toolBarFrame;
//  toolBarFrame = CGMakeRect (0, 440, 320, 40);
//  [toolBarFrame setFrame:toolBarFrame];
//  [myTableViewController.view addSubView:toolBarFrame];
//viewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"movies.jpg"]];

//myTableView.rowHeight=120;
[self.view addSubview:myTableView];
//tempArray = [[NSArray alloc] initWithObjects:@"Sports",@"News",@"naturals1",@"live",nil];
//titleArray = [[NSArray alloc] initWithObjects:@"SA spinners made the difference: Sammy",@"Cabinet terminates ISRO’s deal",@"Hudson River Fish Evolve Toxic ",@"Hi Today Special News?",nil];
//SBJSON *json = [SBJSON new];
//  json.humanReadable = YES;

//self.tableDataList = tempArray;
//self.myTableView.backgroundColor = [UIColor clearColor];

//create new uiview with a background image
//UIImage *backgroundImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"movies" ofType:@"jpg"]];
//UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];

//adjust the frame for the case of navigation or tabbars
//backgroundView.frame = CGRectMake(0, 0, 320, 460);

//add background view and send it to the back
//[self.viewController.view addSubview:backgroundView];
//[self.viewController.view sendSubviewToBack:backgroundView];

//[backgroundView release];

//UIImage *img1= [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://122.183.217.134:8080/sivajitv/photos/20101216001017.jpg"]]];
//NSLog(@"ERROR1:%@",img1); 

//NSData *imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://122.183.217.134:8080/sivajitv/photos/20101216001017.jpg"]];
//NSLog(@"ERROR12:%@",imageData);
}

【问题讨论】:

  • 天哪...你真的需要阅读关于编写 iPhone 应用程序的优秀教程或书籍。特别要确保您阅读并记住memory management rules
  • 另外,通过静态分析器运行您的代码(选择“构建和分析”)。让我们知道这会带来什么。
  • 是的,我做到了...我得到了 14 个潜在泄漏的分析仪结果,并且从未读取过一些存储的值..
  • 你能告诉我如何解决dis ..
  • 要修复漏洞,您需要了解您拥有哪些对象以及不拥有哪些对象。您还需要知道您应该拥有哪些对象。如果您拥有一个对象,您需要在完成后放弃所有权,您可以通过发送releaseautorelease 来完成此操作。如果您拥有一个对象,那么您不需要发送releaseautorelease。使用retain 属性来帮助进行内存管理。

标签: iphone objective-c cocoa-touch ios memory-management


【解决方案1】:

你的代码有很多问题,很难知道从哪里开始:

  1. client.connect;[client connect] 的语法糖,但是由于connect 不是属性(至少我希望它不是属性),你应该不要使用点符号。

  2. isEqualToString: 的参数需要 NSString * 对象,而不是 NSNull *。如果您想查看变量是否指向字符串(而不是 nil),或者,如果您想确保字符串实际包含字符,请使用:

    if ([main_str length])
        // do something
    
  3. 您使用id_arr=[[NSMutableArray alloc]init]; 将您拥有的新可变数组分配给id_arr,但是您稍后会使用id_arr=[[udf objectForKey:@"id_value"] componentsSeparatedByString:@","]; 完全覆盖此引用。这意味着您创建(和拥有)的原始数组无法再被引用,并且被泄露。

  4. 你永远不会初始化你分配的对象:

    js=[[JsontoObject alloc]sivajiTV_response:main_str];
    
  5. 您创建了一个Event 对象,但从不使用或释放​​它(这是内存泄漏):

    e1=[[Event alloc]init];
    

【讨论】:

    猜你喜欢
    • 2010-09-24
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    相关资源
    最近更新 更多