jeudi 9 juillet 2015

Program stops working with too many files in array

I have written this search program for searching through an array of objects which contains a file name and a pathway to the file on my computer. When I wrote the program, I had two test files which worked great with my code. I then started adding the actual files and it worked fine with 10-20 files, so I decided to add a few more (about 45 which made a grand total of around 60 files). This is were it goes wrong; The program runs, but I can't search for any file without getting the "Thread 1: signal SIGABRT" error, on the line were my search function is. I also get an error message in the console but I'm afraid that I can't understand it. It doesn't matter if the file is searchable when I only have 10 files, it stops working when I use all of them. I include my code below:

SMADoc.h (custom class for creating the objects that are PDF's)

#import <Foundation/Foundation.h>

@interface SMADoc : NSObject

@property (nonatomic) NSNumber *docNumber
@property (nonatomic) NSString *urlToDoc;

@end

main.m (with 5 of the files)

#import <Foundation/Foundation.h>
#import "SMADoc.h"
#include <readline/readline.h>
@import AppKit;

void *documentSearch() {
SMADoc *one = [[SMADoc alloc] init];
[one setdocNumber:@(17800)];
[one setUrlToDoc:@"/Users/Docs/docPath1.pdf"];

SMADoc *two = [[SMADoc alloc] init];
[two setdocNumber:@(11632)];
[two setUrlToDoc:@"/Users/Docs/docPath2.pdf"];

SMADoc *three = [[SMADoc alloc] init];
[three setdocNumber:@(17583)];
[three setUrlToDoc:@"/Users/Docs/docPath3.pdf"];

SMADoc *four = [[SMADoc alloc] init];
[four setdocNumber:@(14351)];
[four setUrlToDoc:@"/Users/Docs/docPath4.pdf"];

SMADoc *five = [[SMADoc alloc] init];
[five setdocNumber:@(11628)];
[five setUrlToDoc:@"/Users/Docs/docPath5.pdf"];

NSMutableArray *docs = [[NSMutableArray alloc] initWithObjects:one, two, three, four, five, nil];

int i = 0;

NSLog(@"Enter what you want to search for: ");
const char *searchC = readline(NULL);
int number = atoi(searchC);
NSNumber *sNumber = [NSNumber numberWithInteger:number];

for (SMADoc *nSearch in docs) {
    if ([sNumber isEqualToNumber:[nSearch docNumber]]) {
        NSLog(@"Opening document...");
        [[NSWorkspace sharedWorkspace] openFile:[nSearch urlToDoc]];
    }
    if (![sNumber isEqualToNumber:[nSearch docNumber]]) {
        i++;
    }
}
if (i == [docs count]) {
    NSLog(@"A match could not be found, please check your spelling");
}
free(documentSearch());
documentSearch();

return 0;
}

int main(int argc, const char * argv[]) {
@autoreleasepool {
    NSLog(@"message");
    documentSearch();
}
return 0;
}

And it is this line below that gets the SIGABRT-error:

 if ([sNumber isEqualToNumber:[nSearch docNumber]]) {

And this is the output in the console:

2015-07-09 14:28:25.047 LIX4[3537:108937] message
2015-07-09 14:28:25.049 LIX4[3537:108937] Enter what you want to search   for: 
1177880000

2015-07-09 14:28:27.505 LIX4[3537:108937] Opening document...
2015-07-09 14:28:27.633 LIX4[3537:108937] An uncaught exception was raised
2015-07-09 14:28:27.633 LIX4[3537:108937] -[__NSCFNumber compare:]: nil argument
2015-07-09 14:28:27.633 LIX4[3537:108937] (
    0   CoreFoundation                      0x00007fff8b8ae03c     __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff8ef9076e     objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff8b8adeed +    [NSException raise:format:] + 205
    3   CoreFoundation                      0x00007fff8b7ab031 -    [__NSCFNumber compare:] + 81
    4   CoreFoundation                      0x00007fff8b7aafc8 -    [__NSCFNumber isEqualToNumber:] + 24
    5   LIX4                                0x0000000100004a3a     documentSearch + 14490
    6   LIX4                                0x0000000100005355 main + 53   
    7   libdyld.dylib                       0x00007fff8fb6f5c9 start + 1
)
2015-07-09 14:28:27.634 LIX4[3537:108937] *** Terminating app due to   uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber     compare:]: nil argument'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff8b8ae03c     __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff8ef9076e     objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff8b8adeed +[NSException raise:format:] + 205
    3   CoreFoundation                      0x00007fff8b7ab031 -[__NSCFNumber compare:] + 81
    4   CoreFoundation                      0x00007fff8b7aafc8 -[__NSCFNumber isEqualToNumber:] + 24
    5   LIX4                                0x0000000100004a3a     documentSearch + 14490
    6   LIX4                                0x0000000100005355 main + 53
    7   libdyld.dylib                       0x00007fff8fb6f5c9 start + 1
)
    libc++abi.dylib: terminating with uncaught exception of type     NSException
    (lldb) 

If anyone could help me figure this out I would be very grateful!

Aucun commentaire:

Enregistrer un commentaire