iphone - iOS App在启动时崩溃,没有显示任何错误

标签 iphone memory crash

我了解到还有其他人遇到此问题,我看了很多关于此问题的帖子,但仍然不知道为什么我的应用程序持续崩溃。我只是不明白发生了什么='(我尝试清除所有内容,注释掉我的代码的一部分,并按照这个问题的其他答案,但无济于事。这是我的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)

我真的不是第一个知道它为什么停止工作的线索,好像一分钟就在工作,然后又停止了,但是我也尝试将其清理干净并重置模拟器,但是没有成功。任何帮助都是非常感谢!!!

最佳答案

乍看上去。

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

应该
SieveArray = [[NSMutableArray alloc]init];
PrimesAndThreads = [[NSMutableArray alloc]init];

关于iphone - iOS App在启动时崩溃,没有显示任何错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15650393/

相关文章:

iphone - 手势识别器不适用于 subview

C memcpy() 一个函数

c++ - vector<unique_ptr<T>> 在 ubuntu 中占用的内存是 vector<T> 的三倍以上

crash - Hyperledger Fabric - 如何在每次区 block 链网络测试后清除 DEV 环境?

iphone - iPhone应用程序在某些设备上崩溃-寻找建议

iphone - 如何通过indexPath获取uitableViewCell?

iphone - Monotouch - 从我的应用程序将事件添加到 iPhone 日历

ios - 是否可以阻止其他应用程序运行?

c++ - 什么时候/为什么要使用 heapmin?

android - Android处理权限已从系统设置中禁用