views:

500

answers:

2

Hey Folks,

I am getting an EXC_BAD_ACCESS error when i am trying to create an instance of a method.

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/\"&gt;\n"
                           "<soap:Body>\n"
                           "<GetImage xmlns=\"http://tempuri.org/\"&gt;\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];

}

Initialiser code

- (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;
}

I applied a break point over here and when i hit continue i get the exe_bad_access error.

I am trying o create an object of an xmlparser class.

been on this for hours now.. no success. Please help..

A: 

See this question on EXC_BAD_ACCESS

Tobias Cohen
+13  A: 

There are a bunch of problems with this code, any of which might cause a crash. Hard to tell which without seeing a backtrace.

  1. your initializer is wrong:

    - (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;
    
    }

    You shouldn't have any code between the if and the return. What happens if the call to super fails? In your code, that'll go on to initialize the instance anyway. That'll break, for sure.

  2. Your call to NSLog() is dangerous:

    NSLog(soapMessage);
    

    If soapMessage were to happen to contain a %@ or %s, that is guaranteed to crash.

  3. You are invoking -dealloc directly. Twice. Immediately after adding the object to be deallocated to the view hierarchy.

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

    This will definitely cause a crash as the deallocated objects are now sitting in the view hierarchy. As soon as your app tries to render anything, BOOM*.

    You should never call -dealloc directly. (You meant -release there, right?)

  4. In a comment, you say the crash is on the line DetailImageViewDataSource *detail=[[DetailImageViewDataSource alloc] init];. That line doesn't appear in the pasted source. As well, all of your initialization logic is in -initWithNibName:bundle:.

    Are you sure your objects are being initialized correctly?

Fix all of that and then see if your crash still happens. If it does, post the stacktrace of the crash.

bbum