1

I understand that there are others that have this problem, and I have looked at a ton of posts with this problem but still can't figure out why my app keeps crashing. I just don't understand what is happening ='( I have tried cleaning everything out, commenting out parts of my code, and following the other answers to this problem but to no avail. Here is my viewControler.h:

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate>

//txt fields for max value and thread
@property (weak, nonatomic) IBOutlet UITextField *txtNumThreads;
@property (weak, nonatomic) IBOutlet UITextField *txtMaxValue;

//labels for time elapsed, primes found, and time
@property (weak, nonatomic) IBOutlet UILabel *lblTimeElapsed;
@property (weak, nonatomic) IBOutlet UILabel *lblPrimesFound;
@property (weak, nonatomic) IBOutlet UILabel *lblTime;

//actions for each of the buttons
- (IBAction)btnStart_click:(id)sender;
- (IBAction)btnClear_click:(id)sender;
- (IBAction)btnTime_click:(id)sender;
- (IBAction)btnShow_click:(id)sender;

//show button and text view to show results
@property (weak, nonatomic) IBOutlet UIButton *btnClear;
@property (weak, nonatomic) IBOutlet UIButton *btnShow;
@property (weak, nonatomic) IBOutlet UIButton *btnStart;
@property (weak, nonatomic) IBOutlet UITextView *txtViewResults;

//boolean to determine if the start button has been pressed once already
@property bool isComputing;

//for number of threads and max value
@property int NumThreads;
@property int MaxValue;
@property int sqRoot;

//array for primes and results
@property (strong, nonatomic) NSMutableArray *SieveArray;
@property (strong, nonatomic) NSMutableArray *PrimesAndThreads;

@end

And this is my ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

//symbolic constants for max number of threads and max value
static const int MAX_NUM_THREADS = 4;
static const int MAX_VALUE = 9999;

@synthesize isComputing;
@synthesize sqRoot;
@synthesize NumThreads;
@synthesize MaxValue;
@synthesize SieveArray;
@synthesize PrimesAndThreads;

- (void)viewDidLoad
{
    [super viewDidLoad];

    //to dismiss the keyboard using tap gesture
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(dismissKeyboard)];

    //add tap gesture
    [self.view addGestureRecognizer:tap];

    //start with isComputing being false
    isComputing = false;

    //allocate
    SieveArray = [NSMutableArray array];
    PrimesAndThreads = [NSMutableArray array];
}

//to dismiss the keyboard using resign first responder
-(void)dismissKeyboard {
    [self.txtMaxValue resignFirstResponder];
    [self.txtNumThreads resignFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//click event for the start button
- (IBAction)btnStart_click:(id)sender {
    //clear contents of boxes
    self.txtViewResults.text = @"";

    if(SieveArray)
        [SieveArray removeAllObjects];

    if(PrimesAndThreads)
        [PrimesAndThreads removeAllObjects];

    [self.btnShow setEnabled:FALSE];

    if([self txtNumThreads].text.length != 0 && [self txtMaxValue].text.length != 0)
    {
        //get the user entered data
        NumThreads = [self.txtNumThreads.text intValue];
        MaxValue = [self.txtMaxValue.text intValue];

        if(NumThreads <= MAX_NUM_THREADS && MaxValue <= MAX_VALUE && NumThreads > 0)
        {
            //square root for the max number of divisions
            sqRoot = sqrt(MaxValue);

            //array for the algorithm
            for(int i = 0; i < MaxValue+1; i++)
                [SieveArray addObject:[NSNumber numberWithInt:1]];


            switch (NumThreads) {
                case 1:
                {
                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
                        [self SieveAlgorithm:0 Right:MaxValue ThreadNum:1];
                    });
                    break;
                }
                case 2:
                {
                    int FirstHalf = MaxValue/2;

                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
                        [self SieveAlgorithm:0 Right:FirstHalf ThreadNum:1];
                    });

                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
                        [self SieveAlgorithm:FirstHalf+1 Right:MaxValue ThreadNum:2];
                    });
                    break;
                }
                case 3:
                {
                    int OneThird = MaxValue/3;

                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
                        [self SieveAlgorithm:0 Right:OneThird ThreadNum:1];
                    });

                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
                        [self SieveAlgorithm:OneThird+1 Right:OneThird*2 ThreadNum:2];
                    });

                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
                        [self SieveAlgorithm:(OneThird*2)+1 Right:MaxValue ThreadNum:3];
                    });
                    break;
                }
                case 4:
                {
                    int OneFourth = MaxValue/4;

                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
                        [self SieveAlgorithm:0 Right:OneFourth ThreadNum:1];
                    });

                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
                        [self SieveAlgorithm:OneFourth+1 Right:OneFourth*2 ThreadNum:2];
                    });

                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
                        [self SieveAlgorithm:(OneFourth*2)+1 Right:OneFourth*3 ThreadNum:3];
                    });

                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
                        [self SieveAlgorithm:(OneFourth*3)+1 Right:MaxValue ThreadNum:4];
                    });
                    break;
                }
                default:
                    break;
            }

        }
        else
        {
            UIAlertView *ExceedMaxError = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"Please enter between 1 and %d threads, and a max of %d for value.", MAX_NUM_THREADS, MAX_VALUE] delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
            [ExceedMaxError show];
        }
    }
    else
    {
        UIAlertView *blankTextsError = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please enter No Threads and Max Value." delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
        [blankTextsError show];
    }

    [self.btnShow setEnabled:TRUE];
}

//click event for the clear button
- (IBAction)btnClear_click:(id)sender {
    self.txtMaxValue.text = @"";
    self.txtNumThreads.text = @"";
    self.txtViewResults.text = @"";
}

//click event for the time button
- (IBAction)btnTime_click:(id)sender {
    NSDate *now = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterShortStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];

    self.lblTime.text = [NSString stringWithFormat:@"%@", [formatter stringFromDate:now]];
}

//click event for the show button
- (IBAction)btnShow_click:(id)sender {
    //clear the result text view
    self.txtViewResults.text = @"";

    for(id object in PrimesAndThreads)
    {
        self.txtViewResults.text = [self.txtViewResults.text stringByAppendingString:[NSString stringWithFormat:@"%@\n", object]];
    }

    if(PrimesAndThreads)
        [PrimesAndThreads removeAllObjects];
}

//Sieve Algorithm function
//Takes the left and right subscript of the range and performs the algorithm on SieveArray
-(void)SieveAlgorithm:(int)left Right:(int)right ThreadNum:(int)thread {

    //get rid of multiples of i to square root of MaxValue
    for(int i = 2; i <= sqRoot; i++)
    {
        for(int x = i+i; x <= right; x=x+i)
        {
            [SieveArray replaceObjectAtIndex:x withObject:[NSNumber numberWithInt:0]];
        }
    }

    //add the primes to a string array for printing later
    for(int l = left; l <= right; l++)
    {
        if([[SieveArray objectAtIndex:l] integerValue] == 1)
            [PrimesAndThreads addObject:[NSString stringWithFormat:@"ThNum:%d Prime:%d", thread, l]];
    }
}
@end

Upon crashing I get taken to this:

libobjc.A.dylib`objc_msgSend:
0x10e608c:  movl   8(%esp), %ecx
0x10e6090:  movl   4(%esp), %eax
0x10e6094:  testl  %eax, %eax
0x10e6096:  je     0x10e60e8                 ; objc_msgSend + 92
0x10e6098:  movl   (%eax), %edx
0x10e609a:  pushl  %edi
0x10e609b:  movl   8(%edx), %edi
0x10e609e:  pushl  %esi
0x10e609f:  movl   (%edi), %esi
0x10e60a1:  movl   %ecx, %edx
0x10e60a3:  shrl   $2, %edx
0x10e60a6:  andl   %esi, %edx
0x10e60a8:  movl   8(%edi,%edx,4), %eax
0x10e60ac:  testl  %eax, %eax
0x10e60ae:  je     0x10e60b9                 ; objc_msgSend + 45
0x10e60b0:  cmpl   (%eax), %ecx
0x10e60b2:  je     0x10e60d0                 ; objc_msgSend + 68
0x10e60b4:  addl   $1, %edx
0x10e60b7:  jmp    0x10e60a6                 ; objc_msgSend + 26
0x10e60b9:  popl   %esi
0x10e60ba:  popl   %edi
0x10e60bb:  movl   4(%esp), %edx
0x10e60bf:  movl   (%edx), %eax
0x10e60c1:  jmp    0x10e60d9                 ; objc_msgSend + 77
0x10e60c3:  nopw   %cs:(%eax,%eax)
0x10e60d0:  movl   8(%eax), %eax
0x10e60d3:  popl   %esi
0x10e60d4:  popl   %edi
0x10e60d5:  xorl   %edx, %edx
0x10e60d7:  jmpl   *%eax
0x10e60d9:  pushl  %eax
0x10e60da:  pushl  %ecx
0x10e60db:  pushl  %edx
0x10e60dc:  calll  0x10d3df3                 ; _class_lookupMethodAndLoadCache3
0x10e60e1:  addl   $12, %esp
0x10e60e4:  xorl   %edx, %edx
0x10e60e6:  jmpl   *%eax
0x10e60e8:  calll  0x10e60ed                 ; objc_msgSend + 97
0x10e60ed:  popl   %edx
0x10e60ee:  movl   1007235(%edx), %eax
0x10e60f4:  testl  %eax, %eax
0x10e60f6:  je     0x10e60fe                 ; objc_msgSend + 114
0x10e60f8:  movl   %eax, 4(%esp)
0x10e60fc:  jmp    0x10e6098                 ; objc_msgSend + 12
0x10e60fe:  movl   $0, %edx
0x10e6103:  ret

With this highlighted as the breakpoint:

0x10e60b2:  je     0x10e60d0                 ; objc_msgSend + 68

And this as the output:

(lldb)

I seriously haven't the first clue why it stopped working, it seemed like one minute it was working and then it stopped, but I also tried making it clean and reseting the simulator but that didn't do it..any help would be enormously appreciated!!!

11
  • Can you explain what happens when it crashes..?
    – rog
    Commented Mar 26, 2013 at 19:42
  • Yup, added it in an edit just now.
    – KateMak
    Commented Mar 26, 2013 at 19:48
  • 2
    the crash you are getting here is obj mes send. This only encounters when you are trying to use or utilise the object which is no more available in the memory.
    – Abhishek
    Commented Mar 26, 2013 at 19:53
  • 3
    try to enable nszombies for your project. It will give you exact problem for the crash why you are getting it.
    – Abhishek
    Commented Mar 26, 2013 at 19:56
  • @Abhishek that and whenever you simply send a message to an object that does not implement a selector for it. e.g. send addObject: to an instance of NSString.
    – Till
    Commented Mar 26, 2013 at 20:07

1 Answer 1

2

At a Glance.

SieveArray = [NSMutableArray array];
PrimesAndThreads = [NSMutableArray array];

Should be

SieveArray = [[NSMutableArray alloc]init];
PrimesAndThreads = [[NSMutableArray alloc]init];
2
  • 1
    This wont make a difference under ARC. Commented Mar 27, 2013 at 0:27
  • True however it's bad form. Commented Jul 18, 2013 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.