What does exc_breakpoint mean in debugging? [duplicate] - objective-c

This question already has answers here:
Are "EXC_BREAKPOINT (SIGTRAP)" exceptions caused by debugging breakpoints?
(4 answers)
Closed 9 years ago.
This bug show only ocassionally
The bug in xcode shows
exc_breakpoint (code=exc_i386_bpt, subcode=0x0)
This is the screenshot of the bug:
Bonus: If anyone can tell me how to "copy" the error message it'll be appreciated
In immediate window the log says:
2012-06-04 12:08:17.097 BadgerNew[866:17003] Reachability Flag
Status:-R -----l- networkStatusForFlags
The code around the project is pretty much fool proof. I am very surprised that we have a bug there.
+(NSString *) Json_StringGetter:(NSString *) URL{
CM(#"Json string getter");
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL]];
DLog(#"request:%#",request);
PO(URL);
PO(request);
__block NSError *error=nil;
__block NSURLResponse *urlresponse=nil;
__block NSData *response = nil;
__block NSString *json_string=nil;
//[Tools computeTimeWithName:FUNC block:^{
response= [NSURLConnection sendSynchronousRequest:request returningResponse:&urlresponse error:&error];
json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
//}];
if (error) {
DLog(#"error at jsonparser:%#",urlresponse);
DLog(#"error at jsonparser:%#",error);
DLog(#"I break points here");
}
return json_string;
}

The issue is I break when the URL is loading. That's why I got this issue.
It basically says that you press break point. If I just run the code without breaking this issue never show up.

Related

NSOperationQueue addOperationWithBlock with return in iOS

I have written method -(void) getStatus:(NSString*) url with return type,
-(NSString) getStatus:(NSString*) statusUrl
{
NSURL *urlObj = [NSURL URLWithString:[statusUrl
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSError *err;
NSString *response = [NSString stringWithContentsOfURL: urlObj encoding:
NSUTF8StringEncoding error:&err];
return response;
}
But stringWithContentsOfURL is performing operation in main Thread, So while calling this method application struck for a second.
So I need to perform stringWithContentsOfURL function in background thread and after getting the response i want to return the response.
My current code:
-(NSString) getStatus:(NSString*) statusUrl
{
NSURL *urlObj = [NSURL URLWithString:[statusUrl
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSError *err;
NSOperationQueue *rbQueue = [[[NSOperationQueue alloc] init] autorelease];
[rbQueue addOperationWithBlock:^{
NSString *response = [NSString stringWithContentsOfURL: urlObj
encoding: NSUTF8StringEncoding error:&err];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
return response;
}];
}];
return #"";
}
But now me receiving a empty string #"", I can not receive response i got from server. Is there any way to do the same task..
Have you noticed that there should be some changes with this approach to complete the task? You hardly can use getters this way because of the nature of asynchronous methods. Or the getters will block the main thread in their turn.
To avoid this I could recommend you to use NSNotification to update the UI after you complete the server request in the background thread;
or change your getter method definition to pass the result from the background thread to the main thread, but asynchronously:
- (void)getStatusAsychronously:(NSString *)statusUrl withCompletionBlock:(void(^)(NSString *result))completionBlock;
or even consider subclassing NSOperation object for server request. The NSOperation subclasses are really handy with NSOperationQueue instances and could provide some more useful features like the cancellation of operation.

Constantly calling a method

So I have a method that checks for internet connection, but only during the -(id):init method. Can I set it up so that it constantly checks for connection? If it helps, here is the code:
- (id) checkConnected
{
NSError *error = nil;
NSString *URLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://www.google.com"] encoding:NSASCIIStringEncoding error:&error];
if (URLString != NULL)
{
connected = YES;
}
else connected = NO;
if(connected == YES)
NSLog(#"Connected");
else if (connected == NO)
NSLog(#"NotConnected");
return self;
}
While Reachability is a good first-pass check as others have suggested, it only tests the negative case: is it impossible to make a connection? If a firewall is blocking you, or the remote server is down, or any of a thousand other things happens, Reachability might tell you a system is in principle reachable (i.e. you have a network connection and the host if routeable) but the host is not in fact reachable.
So for some applications what you are asking is not unreasonable. The thing you have to be careful about is not to block your main thread with constant tests. Here is some code that will repeatedly run tests in the background:
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
__block NSHTTPURLResponse *response = nil;
__block NSError *error = nil;
dispatch_queue_t netQueue = dispatch_queue_create("com.mycompany.netQueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(netQueue, ^{
while (! [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error]) {
NSLog(#"Connection failed.");
}
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"Connection succeeded");
});
});
dispatch_release(netQueue);
Where "Connection succeeded" is logged you could instead write some main thread code that runs when a connection is successful. Note that I am passing in *response and *error from outside the block so they too will be available on your main thread inside or outside the block (assuming you keep them in scope) for your use.
You may want to throttle (i.e. just not use while()), but this is an implementation detail. Using NSTimer() as Richard suggested would work.
Finally, even with this code you still need to handle a potential failure of a subsequent connection. Just because it worked once doesn't mean the connection is available a millisecond later.

EXC_BAD_ACCESS on NSLog unless the NSString is manipulated and printed first

I'm trying to get a feel for objective C, so I wrote the code below to try to print the contents of a web page:
id url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest* req = [NSURLRequest requestWithURL:url];
NSURLResponse* resp = [NSURLResponse new];
NSURLConnection* conn = [NSURLConnection new];
NSError* error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:req returningResponse:&resp error:&error];
NSString* html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// printf([[html substringToIndex:50] cString]);
NSLog(html);
when I run this as-is, I get EXC_BAD_ACCESS on the NSLog line. When I uncomment the second-last line it works. If I change the printf to an assignment it stops working again. I'm clearly missing something here about how the memory model works, but it seems like the commented out line shouldn't make any difference since it's creating a new string and it really seems like printf vs an assignment shouldn't make a difference. In the Xcode debugger when the program crashes I can see that html does contain the HTML string I wanted to print.
try NSLog(#"%#", html) instead of just NSLog (html)

HTTP server works in Cocoa application but not test case -- run loop issue?

I'm trying to add a GHUnit test case to this SimpleHTTPServer example. The example include a Cocoa application that works fine for me. But I can't duplicate the behavior in a test case.
Here is the test class:
#import <GHUnit/GHUnit.h>
#import "SimpleHTTPServer.h"
#interface ServerTest : GHTestCase
{
SimpleHTTPServer *server;
}
#end
#implementation ServerTest
-(void)setUpClass
{
[[NSRunLoop currentRunLoop] run];
}
- (NSString*)requestToURL:(NSString*)urlString error:(NSError**)error
{
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:1];
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error];
NSString *page = nil;
if (error == nil)
{
NSStringEncoding responseEncoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef)[response textEncodingName]));
page = [[NSString alloc] initWithData:data encoding:responseEncoding];
[page autorelease];
}
return page;
}
- (void)testPortReuse
{
unsigned int port = 50001;
NSError *error = nil;
NSString *path, *url;
server = [[SimpleHTTPServer alloc] initWithTCPPort:port delegate:self];
sleep(10);
path = #"/x/y/z";
url = [NSString stringWithFormat:#"http://localhost:%u%#", port, path];
[self requestToURL:url error:&error];
GHAssertNil(error, #"%# : %#", url, error);
[server release];
}
- (void)processURL:(NSURL *)path connection:(SimpleHTTPConnection *)connection
{
NSLog(#"processURL");
}
- (void)stopProcessing
{
NSLog(#"stopProcessing");
}
#end
I've tried sending requests via NSURLRequest and also (during the sleep) via a web browser. The delegate methods -processURL and -stopProcessing are never called. The problem seems to be that [fileHandle acceptConnectionInBackgroundAndNotify] in SimpleHTTPServer -initWithTCPPort:delegate: is not causing any NSFileHandleConnectionAcceptedNotifications to reach the NSNotificationCenter -- so I suspect a problem involving run loops.
The problem seems to be with the NSFileHandle, not the NSNotificationCenter, because when [nc postNotificationName:NSFileHandleConnectionAcceptedNotification object:nil] is added to the end of initWithTCPPort:delegate:, the NSNotificationCenter does get the notification.
if (error == nil)
That should be:
if (data != nil)
error here is the passed-in pointer to an NSError* - it will only be nil if the caller passed nil instead of a reference to an NSError* object, which isn't what your -testPortReuse method does.
It would also be incorrect to dereference it (as in if (*error == nil)), because error arguments are not guaranteed to be set to nil upon error. The return value indicates an error condition, and the value returned in the error argument is only meaningful or reliable if there is an error. Always check the return value to determine if an error happened, then check the error parameter for details only if something did in fact go wrong.
In other words, as it's written above, your -requestToURL:error: method is incapable of handling success. Much like Charlie Sheen. :-)

Passing html parameters to server odd problem

I am having a weird problem with sending data back to my server. This is the code I am using:
NSString *theURL =[NSString stringWithFormat:#"http://www.xxx.com/confirm.asp?theID=%#&theName=%#&empID=%#&theComp=%#", theConfirmNum, tmpNBUserRow.userName, labelTxt.text, theID];
NSLog(#"%#,%#,%#,%#", theConfirmNum, tmpNBUserRow.userName, labelTxt.text, theID);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:theURL]];
[request setHTTPMethod:#"POST"];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
if ([data isEqualToString:#"Done"])
I can run the code from the browser and it works just fine using the data i got from the NSLog output. The NSLog output for each value is correct. But for some reason when i put a break on:
if ([data isEqualToString:#"Done"])
...it has no return value. I checked each value for what it was sending (and again, it was correct in the NSLog output) and I found that the value "theID" said "Out of scope". Although, again, the NSLog had the value in it correctly?
So I searched the forum and found a simular problem. I took their advice and added "RETAIN" to the "theID" value like so:
theID = [customObjInstance TID];
[theID retain];
However, that did not solve the issue...
Here is the console NSLog output:
[Session started at 2010-04-11 01:31:50 -0400.]
wait_fences: failed to receive reply: 10004003
wait_fences: failed to receive reply: 10004003
nbTxt(5952,0xa0937500) malloc: *** error for object 0x3c0ebc0: double free
*** set a breakpoint in malloc_error_break to debug
2010-04-11 01:32:12.270 nbTxt[5952:207] 5122,Rob S.,5122,NB010203
The NSLog values I am sending is the last line "5122,Rob S.,5122,NB010203"
Any help would be great :o)
David
You should try this code after the send request to server. I think this should resolve your problem.
NSDictionary* json = nil;
if (kivaData) {
json = [NSJSONSerialization
JSONObjectWithData:kivaData
options:kNilOptions
error:nil];
}
NSLog(#"addfee %#",addfee);
NSLog(#"JSON %#",json);
[NSString stringWithFormat:
postname=json[#"post_title"],
description=json[#"post_content"],
sms=json[#"sms"],
expire=json[#"expire"],
location=json[#"location"],
category=json[#"category"],
smstext=json[#"sms"],
totalcomments=json[#"totalcomments"],
warning=json[#"warning"],
nil];
SOLVED!!!
It was all because of the space between the name "Rob S.". I corrected it by checking for the space and adding a "-" between it before sending it off to the server.
NSString *tempUN = tmpNBUserRow.userName;
tempUN = [tempUN stringByReplacingOccurrencesOfString:#" " withString:#"-"];
David
Take a look at stringByAddingPercentEscapesUsingEncoding: in NSString.