3 Replies - 1903 Views - Last Post: 16 March 2011 - 05:15 PM

#1 tommynator128  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 127
  • Joined: 05-January 10

NSArray EXC_BAD_ACCESS

Posted 12 March 2011 - 05:04 AM

Hello,

i'm really new to Objective-C and so i got until now, trying to make a little FileDialog which starts after selecting some Files a new Thread, searching for words in the files and returning them to screen. But right now i'm stuck at the point where the thread says EXC_BAD_ACCESS when trying to get the count of items in the array. My code right now:
.h
@interface DoSthThread : NSObject {
@private
    NSArray *fileArray;
	NSProgressIndicator *pbar;
	BOOL keepRunning;
	
}

@property(readwrite) NSArray *fileArray;
@property(readwrite) NSProgressIndicator *pbar;
@property(readwrite) BOOL keepRunning;

- (void)doSth;
@end


@interface FileSelector : NSObject {
@private
    NSArray *fileArray;
	IBOutlet NSButton *selectBtn,*doSthBtn;
	IBOutlet NSProgressIndicator *pbar;
	DoSthThread *newThread;
}

- (IBAction)openFileDialog:(id)sender;
- (IBAction)doSthBtnClicked:(id)sender;

@end



.m
@implementation FileSelector

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
		newThread = [[doSthThread alloc] init];
		[newThread setPbar:pbar];
		
    }
    
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (IBAction)openFileDialog:(id)sender {
	int i; // Loop counter.
	
	// Create the File Open Dialog class.
	NSOpenPanel* openDlg = [NSOpenPanel openPanel];
	
	// Enable the selection of files in the dialog.
	[openDlg setCanChooseFiles:YES];
	[openDlg setAllowsMultipleSelection:YES];
	
	
	// Display the dialog.  If the OK button was pressed,
	// process the files.
	if ( [openDlg runModalForDirectory:nil file:nil] == NSOKButton )
	{
		// Get an array containing the full filenames of all
		// files and directories selected.
		NSArray* files = [openDlg filenames];
		fileArray = [files sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
		// Loop through all the files and process them.
		if([fileArray count] > 0) {
			[doSthBtn setEnabled:YES];
			[newThread setFileArray:files];
		}
	}
}

- (IBAction)doSthBtnClicked:(id)sender {
	[NSThread detachNewThreadSelector:@selector(doSth) toTarget:newThread withObject:nil];
}

@end

@implementation DoSthThread
@synthesize fileArray, pbar, keepRunning;

- (void)doSth {
	for( int i = 0; i < [fileArray count]; i++ )
	{
		//Do sth ;)/>
	}
}

@end



Is This A Good Question/Topic? 0
  • +

Replies To: NSArray EXC_BAD_ACCESS

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: NSArray EXC_BAD_ACCESS

Posted 14 March 2011 - 04:01 AM

Please give us some more details of your problem.
( a ) Does your code compile?
( b ) Any errors or warnings? If there are then share them with us.
Copy and paste the errors exactly as they are.
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect?
Give details and, ideally, examples.
If you provided inputs to the program tell us what they were.
( e ) What have you already tried to fix it?
Was This Post Helpful? 0
  • +
  • -

#3 tommynator128  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 127
  • Joined: 05-January 10

Re: NSArray EXC_BAD_ACCESS

Posted 16 March 2011 - 06:45 AM

( a ) Yes
( b )
...../FileSelector.h:24:1: warning: no 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed [2]
 @property(readwrite) NSArray *fileArray;
 ^
...../FileSelector.h:24:1: warning: default property attribute 'assign' not appropriate for non-gc object [2]
...../FileSelector.h:25:1: warning: no 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed [2]
 @property(readwrite) NSProgressIndicator *pbar;
 ^
...../FileSelector.h:25:1: warning: default property attribute 'assign' not appropriate for non-gc object [2]

( c ) No (beside of the debugger telling me EXC_BAD_ACCESS happened
( d ) Actually the code should access a NSArray* but it returns an EXC_BAD_ACCESS.
( e ) As i don't know much about Objective-C not more than using a NSArray rather than a NSArray*. Which didn't work.

This post has been edited by tommynator128: 16 March 2011 - 06:46 AM

Was This Post Helpful? 0
  • +
  • -

#4 GWatt  Icon User is offline

  • member icon

Reputation: 257
  • View blog
  • Posts: 3,035
  • Joined: 01-December 05

Re: NSArray EXC_BAD_ACCESS

Posted 16 March 2011 - 05:15 PM

It looks like something is wrong with the @synthesize statements. Instead of using the default:
@property(readwrite)
try:
@property(readwrite,retain)

In your openFileDialog selector get an array of selected files, and then give that to newThread. I think that once that function exits, the file array is destroyed, and since Objective C objects are just pointers, the fileArray in DoSthThread points to an invalid object.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1