1

我知道还有其他人遇到此问题,并且我查看了大量有关此问题的帖子,但仍然无法弄清楚为什么我的应用程序不断崩溃。我只是不明白发生了什么='(我尝试清理所有内容,注释掉我的部分代码,并遵循此问题的其他答案但无济于事。这是我的 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

这是我的 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

崩溃后,我被带到了这个:

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

将此突出显示为断点:

0x10e60b2:  je     0x10e60d0                 ; objc_msgSend + 68

这作为输出:

(lldb)

我真的没有第一个线索为什么它停止工作,它似乎工作了一分钟然后它停止了,但我也尝试将其清理干净并重置模拟器,但没有这样做..任何帮助都会非常感谢!!!

4

1 回答 1

2

乍看上去。

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

应该

SieveArray = [[NSMutableArray alloc]init];
PrimesAndThreads = [[NSMutableArray alloc]init];
于 2013-03-26T23:18:22.487 回答