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)

なぜそれが機能しなくなったのか、最初の手がかりは真剣にわ​​かりません.1分間は機能しているように見えましたが、停止しました.大変感謝しています!!!

4

1 に答える 1

2

一目で。

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

する必要があります

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