Application Crashes

Discussion in 'Mac Programming' started by Monaj, Sep 21, 2009.

  1. Monaj macrumors regular

    Joined:
    May 24, 2009
    #1
    Hi All

    this is code which am using for the fetching data fro DB through my API

    Some time application crashes in the directMySQL function after the log directmysql.. at end of directMySQLQuery ok

    Code:
    
    -(NSData*)executeQuery:(NSString*)query WithAction:(NSString*)doAction{
    	NSLog(@"within ExecuteQuery:WithAction: Query is: %@ and Action is: %@",query,doAction);
        NSString *returnResult;
    	@try {
    		
    		NSString *returnResult;
    		NSMutableURLRequest *postRequest;
    		NSError *error;
    		NSData *searchData;
    		NSHTTPURLResponse *response;
    		postRequest=[self directMySQLQuery:query WithAction:doAction];
    		NSLog(@"after directMYSQL in QueryByPost- performQuery... ErrorLogMsg");
    		searchData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
    		returnResult = [[NSString alloc] initWithData:searchData encoding:NSASCIIStringEncoding];
    		
    		NSString *resultToBeCompared=[returnResult stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    		NSLog(@"result obtained - %@/ resultToBeCompared - %@",returnResult,resultToBeCompared);
    		
    		
    		if(![resultToBeCompared isEqualToString:@""]){
    		}else {
    			sleep(10);
    			postRequest=[self directMySQLQuery:query WithAction:doAction];
    			searchData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
    			if(![resultToBeCompared isEqualToString:@""]){
    			}else {
    				sleep(10);
    				postRequest=[self directMySQLQuery:query WithAction:doAction];
    				searchData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
    				if(![resultToBeCompared isEqualToString:@""]){
    				}else {
    					sleep(10);
    					postRequest=[self directMySQLQuery:query WithAction:doAction];
    					searchData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
    					if(![resultToBeCompared isEqualToString:@""]){
    					}else {
    						sleep(10);
    						postRequest=[self directMySQLQuery:query WithAction:doAction];
    						searchData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
    						if(![resultToBeCompared isEqualToString:@""]){
    						}else {
    							return nil;	
    						}
    					}
    				}
    			}
    		}
    		
    		
    		
    		
    		returnResult = [[NSString alloc] initWithData:searchData encoding:NSASCIIStringEncoding];
    
    		NSLog(@"final result returned- %@",returnResult);
    		return searchData;
    	}
    	@catch (NSException * e) {
    		NSLog(@"within QueryByPost , execurteQuery:WithAction - %@",[e description]);
    		return nil;		
    	}
    }
    
    -(NSMutableURLRequest *)directMySQLQuery:(NSString*)query WithAction:(NSString*)doAction{	
    	@try{
    		NSLog(@"within directMySQLQuery:WithAction");
    		NSLog(@"Query is: %@ and Action is: %@",query,doAction);
    		NSString *stringBoundary,*contentType;
    		NSURL *cgiUrl ;
    		NSMutableURLRequest *postRequest;
    		NSMutableData *postBody;
    		NSString *ans=@"434";
    		
    		cgiUrl = [NSURL URLWithString:@"https://keysoftwareservices.com/API.php"];
    		postRequest = [NSMutableURLRequest requestWithURL:cgiUrl];
    		[postRequest setHTTPMethod:@"POST"];
    		
    		stringBoundary = [NSString stringWithString:@"0000RCSQueryxxxxxx"];
    		contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
    		[postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
    		
    		//setting up the body:
    		postBody = [NSMutableData data];
    		[postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    		[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"code\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    		[postBody appendData:[[NSString stringWithString:ans] dataUsingEncoding:NSUTF8StringEncoding]];
    		
    		[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    		[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"action\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    		[postBody appendData:[[NSString stringWithString:doAction] dataUsingEncoding:NSUTF8StringEncoding]];
    
    		[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    		[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"devmode\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    		[postBody appendData:[[NSString stringWithString:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"devmode"]] dataUsingEncoding:NSUTF8StringEncoding]];
    		NSLog(@"devmode...%@",[[[NSBundle mainBundle] infoDictionary] objectForKey:@"devmode"]);
    		[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    		[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"q\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    		[postBody appendData:[[NSString stringWithString:query] dataUsingEncoding:NSUTF8StringEncoding]];
    		
    		[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    		[postRequest setHTTPBody:postBody];
    		NSLog(@"directmysql.. at end of directMySQLQuery ok");
    // Application crashes after the upper log
    		return [postRequest mutableCopy];
    		
    	}@catch (NSException * e) {
    		NSLog(@"NSException %@",e);
    		NSRunAlertPanel(@"Error Panel", @"Within QueryByPost- directMySQLQuery...%@", @"OK", nil, nil,e);
    		return nil;
    	} 
    }
    
    
    

    is any thing wrong in code
    Please suggest me what should I do


    Thanks
    Monaj
     
  2. Spike099 macrumors regular

    Joined:
    Feb 18, 2007
    Location:
    Canada
    #2
    Not 100% sure what the issue is, but why are you returning a "copy" of the data? You could just return the original postRequest.

    You should learn how to set a breakpoint, find out exactly what line of code the app is crashing on. And maybe tell us the error you are getting when the app crashes. EXEC_BAD_ACCESS?
     
  3. sanPietro98 macrumors 6502a

    sanPietro98

    Joined:
    May 30, 2008
    Location:
    28.416834,-81.581214
    #3
    If it is crashing after your get the log message you mentioned, the error could potentially be outside of your method (i.e., AFTER your return statement). I agree with the previous post, we'd need more info to really be of help.
     
  4. sanPietro98 macrumors 6502a

    sanPietro98

    Joined:
    May 30, 2008
    Location:
    28.416834,-81.581214
    #4
    Also, after the App crashes, go to the XCode console window and type 'backtrace'. That should give you a better idea of where the error is occurring. Just make sure you do this while the App is still running.
     

Share This Page