尝试在xcode中创建类的对象时出现EXC_BAD_ACCESS错误

时间:2010-01-03 23:54:28

标签: iphone objective-c xcode

当我尝试创建方法实例时,我收到了EXC_BAD_ACCESS错误。

UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(40,38,240,203)];
DetailImageViewDataSource *detail=[[DetailImageViewDataSource alloc] init];//**error line**

@implementation DetailImageViewDataSource


@synthesize webdata,xmlParser,soapResults,newImage;
-(void) startParsing
{
    recordResults=FALSE;
    NSString *soapMessage=[NSString stringWithFormat:
                           @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                           "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                           "<soap:Body>\n"
                           "<GetImage xmlns=\"http://tempuri.org/\">\n"
                           "<imageID>f89df9ad-5a5d-4354-895f-356d8ce4ccfb</imageID>\n"
                           "</GetImage>\n"
                           "</soap:Body>\n"
                           "</soap:Envelope>\n"];


//http://192.168.2.7/ImageWebService/Service1.asmx
//http://192.168.2.7/ThesisWebServicesNew1/Service.asmx
NSLog(soapMessage);
NSURL *url=[NSURL URLWithString:@"http://192.168.2.7/ThesisWebServicesNew1/Service.asmx"];
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:url];
NSString *msgLength=[NSString stringWithFormat:@"%d",[soapMessage length]];

[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:@"http://tempuri.org/GetImage" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if(theConnection)
{
    webdata=[[NSMutableData data] retain];

    NSLog(@"got connected");
}

else
{
    NSLog(@"No Cnnection");
}

//[nameinput resignFirstResponder];

}

初始化代码

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {

        // Custom initialization
    }

    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(40,38,240,203)];

    [img setImage:[UIImage imageNamed:@"LARCbackground.png"]];

    UITableView *tableView1=[[UITableView alloc] initWithFrame:CGRectMake(20,266,280,136) style:UITableViewStylePlain];
    [tableView1 setDelegate:self];
    [tableView1 setDataSource:self];
    [tableView1 setAlpha:0.75];
    [tableView1 setBackgroundColor:[UIColor clearColor]];

    [[self view] addSubview:tableView1];
    [tableView1 dealloc];
    [[self view] addSubview:img];
    [img dealloc];
    //[imageView setImage:];


    //[[self view] addSubview:imageView];
    return self;
}

我在这里应用了一个断点,当我点击继续时,我得到了exe_bad_access错误。

我正在尝试创建一个xmlparser类的对象。

现在已经持续了几个小时......没有成功。请帮忙..

1 个答案:

答案 0 :(得分:16)

此代码存在许多问题,其中任何一个都可能导致崩溃。没有看到回溯就很难分辨出来。

  1. 您的初始化程序错误:

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
        if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
    
        // Custom initialization
        }
    
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(40,38,240,203)];
    
        ....
        return self;
    }
    

    if和return之间不应该有任何代码。如果对super的调用失败会怎么样?在您的代码中,无论如何都将继续初始化实例。那肯定会破裂。

  2. 您对NSLog()的调用很危险:

    NSLog(soapMessage);
    

    如果soapMessage碰巧包含%@%s,则可以保证崩溃。

  3. 您正在直接调用-dealloc。两次。将要释放的对象添加到视图层次结构后立即。

    [[self view] addSubview:tableView1];
    [tableView1 dealloc];
    [[self view] addSubview:img];
    [img dealloc];
    

    这肯定会导致崩溃,因为解除分配的对象现在位于视图层次结构中。一旦你的应用尝试渲染任何东西,*** BOOM ****。

    永远不会直接致电-dealloc。 (你的意思是-release,对吗?)

  4. 在评论中,您说崩溃就在DetailImageViewDataSource *detail=[[DetailImageViewDataSource alloc] init];行。该行未出现在粘贴的源中。同样,所有初始化逻辑都在-initWithNibName:bundle:

    您确定您的对象是否正确初始化了吗?

  5. 修复所有这些,然后查看您的崩溃是否仍然发生。如果是,请发布崩溃的堆栈跟踪。

相关问题