当我第二次运行我的应用程序时,它会抛出“程序接收信号exc_bad_access”

时间:2011-11-01 06:48:00

标签: iphone ios

当我第二次重新运行该程序时,它崩溃了......

- (IBAction)onConnect: (id)sender
{
    recordResults = NO;
    NSString *soapMessage = [NSString stringWithFormat:
                             @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                             "<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"
                            "<GetHotItems xmlns=\"http://www.mysite.com/WSMobileData/\" />\n"
                            "</soap:Body>\n"
                            "</soap:Envelope>\n"
                             ];

    NSLog( @"soapMessage: %@", soapMessage);

    // URL connection
    NSURL *url = [NSURL URLWithString:@"http://www.mysite.com/WSMobileData/WSMobile.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://www.mysite.com/WSMobileData/GetHotItems" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    NSLog(@"theRequest: %@", theRequest);

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if( theConnection )
    {
        webData = [[NSMutableData data] retain];
    }
    else
    {
        NSLog(@"theConnection is NULL");
    }
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"ERROR with theConenction");
    [connection release];
    [webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"DONE. Received Bytes: %d", [webData length]);
    NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    NSLog(@"theXML: %@", theXML);
    [theXML release];

    if( xmlParser )
    {
        [xmlParser release];
    }

    xmlParser = [[[NSXMLParser alloc] initWithData: webData] autorelease];
    [xmlParser setDelegate: self];
    [xmlParser setShouldResolveExternalEntities: YES];
    [xmlParser parse];

    [connection release];
    [webData release];
}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
   attributes: (NSDictionary *)attributeDict
{
    if( [elementName isEqualToString:@"GetHotItemsResult"])
    {
        if(!soapResults)
        {
            soapResults = [[NSMutableString alloc] init];
        }
        recordResults = YES;
    }
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if( recordResults )
    {
        [soapResults appendString: string];
    }
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if( [elementName isEqualToString:@"GetHotItemsResult"])
    {
        recordResults = NO;
        NSLog(@"soapResults: %@", soapResults);
        [soapResults release];
        soapResults = nil;
    }
}

@end

1 个答案:

答案 0 :(得分:0)

我们至少需要一个崩溃日志来回答。请不要发布您的原始代码,我们不应该为您做调查工作。 exc_bad_access意味着你要重新定位一个对象太多次,检查你的autorelase并用调试器释放行。

你也可以使用NSZombie来追踪被释放的对象太多次了。

本教程可以帮助您:

http://www.touch-code-magazine.com/how-to-debug-exc_bad_access/

希望这有帮助, 文森特