What would cause my program to pause on Xcode? I have no breakpoints set and when I execute my code, the gdb prompt appears on the command line. Does anybody have a quick advise for this. The program itself does not crash and returns the right values. It just won't stop execution.
To tell you a bit of what I am working on. I am going through some exercises from the Stephen Kochan Programming in Objective-C 2.0 book. The exercise where this happened is 8.6. The exercise asks to create a simple method that will create a rectangle object with the intersecting data between two other rectangles.
My main looks like:
#import "Rectangle.h"
#import "XYPoint.h"
#import <stdio.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Rectangle *myRectangle = [[Rectangle alloc] init];
XYPoint *myPoint = [[XYPoint alloc] init];
Rectangle *secondRectangle = [[Rectangle alloc] init];
XYPoint *secondPoint = [[XYPoint alloc] init];
Rectangle *intersectRectangle;
[myRectangle setWidth:100 andHeight:180];
[myPoint setX:400 andY:300];
[myRectangle setOrigin:myPoint];
[secondRectangle setWidth:250 andHeight:75];
[secondPoint setX:200 andY:420];
[secondRectangle setOrigin:secondPoint];
intersectRectangle = [myRectangle intersect:secondRectangle];
NSLog(@"Width: %i, Height: %i", intersectRectangle.width, intersectRectangle.height);
NSLog(@"With translated origin (%i, %i)", intersectRectangle.origin.x, intersectRectangle.origin.y);
[myRectangle release];
[myPoint release];
[secondRectangle release];
[secondPoint release];
[intersectRectangle release];
[pool drain];
return 0;
}
And the method for the class like the following:
-(Rectangle *)intersect:(Rectangle *)rect{
if (intersectingRect) {
[intersectingRect release];
}
intersectingRect = [[Rectangle alloc] init];
XYPoint *intersectPt = [[XYPoint alloc] init];
int intersectWidth = 0;
int intersectHeight = 0;
int intersectX = 0;
int intersectY = 0;
if(origin.x < rect.origin.x) {
if ((origin.x + width) > rect.origin.x) {
if ((origin.x + width) > (rect.origin.x+rect.height)) {
if (origin.y < rect.origin.y) {
if ((origin.y+height) > rect.origin.y) {
if ((origin.y + height) > (rect.origin.y + rect.height)) {
intersectWidth = rect.width;
intersectHeight = rect.height;
intersectX = rect.origin.x;
intersectY = rect.origin.y;
} else {
intersectWidth = rect.width;
intersectHeight = origin.y + height - rect.origin.y;
intersectX = rect.origin.x;
intersectY = rect.origin.y;
}
} else {
intersectWidth = 0;
intersectHeight = 0;
intersectX = 0;
intersectY = 0;
}
} else if ((rect.origin.y + rect.height) > origin.y) {
if ((rect.origin.y + rect.height) > (origin.y + height)) {
intersectWidth = rect.width;
intersectHeight = height;
intersectX = rect.origin.x;
intersectY = origin.y;
} else {
intersectWidth = rect.width;
intersectHeight = rect.origin.y + rect.height - origin.y;
intersectX = rect.origin.x;
intersectY = origin.y;
}
} else {
intersectWidth = 0;
intersectHeight = 0;
intersectX = 0;
intersectY = 0;
}
} else if (origin.y < rect.origin.y) {
if ((origin.y + height) > rect.origin.y) {
if ((origin.y + height) > (rect.origin.y + rect.height)) {
intersectWidth = origin.x + width - rect.origin.x;
intersectHeight = rect.height;
intersectX = rect.origin.x;
intersectY = rect.origin.y;
} else {
intersectWidth = origin.x + width - rect.origin.x;
intersectHeight = origin.y + height - rect.origin.y;
intersectX = rect.origin.x;
intersectY = rect.origin.y;
}
} else {
intersectWidth = 0;
intersectHeight = 0;
intersectX = 0;
intersectY = 0;
}
} else if ((rect.origin.y + rect.height) > origin.y) {
if ((rect.origin.y + rect.height) < (origin.y + height)) {
intersectWidth = origin.x + width - rect.origin.x;
intersectHeight = rect.origin.y + rect.height - origin.y;
intersectX = rect.origin.x;
intersectY = origin.y;
} else {
intersectWidth = origin.x + width - rect.origin.x;
intersectHeight = height;
intersectX = rect.origin.x;
intersectY = origin.y;
}
} else {
intersectWidth = 0;
intersectHeight = 0;
intersectX = 0;
intersectY = 0;
}
} else {
intersectWidth = 0;
intersectHeight =0;
intersectX = 0;
intersectY = 0;
}
} else if (origin.x < (rect.origin.x + rect.width)) {
if ((origin.x + width) > (rect.origin.x + rect.width)) {
if (origin.y < rect.origin.y) {
if ((origin.y+height) > rect.origin.y) {
if ((origin.y + height) > (rect.origin.y + rect.height)) {
intersectWidth = rect.origin.x + rect.width - origin.x;
intersectHeight = rect.height;
intersectX = origin.x;
intersectY = rect.origin.y;
} else {
intersectWidth = rect.origin.x + rect.width - origin.x;
intersectHeight = origin.y + height - rect.origin.y;
intersectX = origin.x;
intersectY = rect.origin.y;
}
} else {
intersectWidth = 0;
intersectHeight = 0;
intersectX = 0;
intersectY = 0;
}
} else if (origin.y < (rect.origin.y + rect.height)) {
if ((origin.y + height) > (rect.origin.y + rect.height)) {
intersectWidth = rect.origin.x + rect.width - origin.x;
intersectHeight = rect.origin.y + rect.height - origin.y;
intersectX = origin.x;
inte开发者_开发百科rsectY = origin.y;
} else {
intersectWidth = rect.origin.x + rect.width - origin.x;
intersectHeight = height;
intersectX = origin.x;
intersectY = origin.y;
}
} else {
intersectWidth = 0;
intersectHeight = 0;
intersectX = 0;
intersectY = 0;
}
} else if (origin.y < rect.origin.y) {
if ((origin.y + height) > rect.origin.y) {
if ((origin.y + height) > (rect.origin.y + rect.height)) {
intersectWidth = width;
intersectHeight = rect.height;
intersectX = origin.x;
intersectY = rect.origin.y;
} else {
intersectWidth = width;
intersectHeight = origin.y + height - rect.origin.y;
intersectX = origin.x;
intersectY = rect.origin.y;
}
} else {
intersectWidth = 0;
intersectHeight = 0;
intersectX = 0;
intersectY = 0;
}
} else if (origin.y < (rect.origin.y + rect.height)) {
if ((origin.y + height) > (rect.origin.y + rect.height)) {
intersectWidth = width;
intersectHeight = rect.origin.y + rect.height - origin.y;
intersectX = origin.x;
intersectY = origin.y;
} else {
intersectWidth = width;
intersectHeight = height;
intersectX = origin.x;
intersectY = origin.y;
}
} else {
intersectWidth = 0;
intersectHeight = 0;
intersectX = 0;
intersectY = 0;
}
} else {
intersectWidth = 0;
intersectHeight = 0;
intersectX = 0;
intersectY = 0;
}
[intersectingRect setWidth:intersectWidth andHeight:intersectHeight];
[intersectPt setX:intersectX andY:intersectY];
[intersectingRect setOrigin:intersectPt];
return intersectingRect;
}
The thing is that I don't want to disable breakpoints altogether. I just don't understand why the execution is being paused in this case since I am not setting any breakpoints. I don't want to disable breakpoints because eventually I would like to use them. It is just this specific case that is unexpectedly pausing.
The program was for some reason trying to send a message to an object that was already released. Fixing the overload of the dealloc method solved the pause problem. The object that was being created inside the intersecting method was then being released by the overloaded dealloc method. Hence, when the main tried sending the message to release it, it was already gone.
Edit: There was no code in your question when I answered, so this doesn't seem to be the problem. I'll leave it here in case someone has the same pausing issue I had.
Does it actually pause, by which I mean is the Continue button available and green?
If not but execution seems to have halted -- your code dosnt seem to be running but it's not really paused -- then it's possible that the app is sitting in the run loop, waiting for input, but you're not actually ready for that and it seems like it's hung.
If that's the case, it may be similar to what's happened to me when I called something that had a delegate protocol but I neglected to set self as the delegate so the program flow never returned to the expected delegate method.
Worth taking a look, anyway.
精彩评论