I modified this down since i don't have nibs/xibs. The while(1) is to just keep the main thread alive while the second one does its thing. This can be saved and compiled as a single source file. You'll need to create a file in your MapPathString path. I called mine S32W4t5.hgt, based on what the code was doing. The OP didn't say anything about the data files. This code does memory management totally wrong, but whatever. That's not the problem, it looks mostly like over-retaining. This does not crash for me.
I certainly don't know why it's crashing for you, but I'd try something similar. Hard-code your paths instead of getting from the GUI and see if you get a crash. If you still do, one less thing to worry about. Trim out the unnecessary parts until you have a very small example that still crashes. If it is getting things from the GUI... well, at least you know that's where things are going awry.
-Lee
I certainly don't know why it's crashing for you, but I'd try something similar. Hard-code your paths instead of getting from the GUI and see if you get a crash. If you still do, one less thing to worry about. Trim out the unnecessary parts until you have a very small example that still crashes. If it is getting things from the GUI... well, at least you know that's where things are going awry.
Code:
#import <Foundation/Foundation.h>
#import <dirent.h>
#import <Cocoa/Cocoa.h>
@interface GUI : NSObject {
NSString *MapPathString;
NSString *OutputPathString;
float totalFiles;
int currentFile;
}
- (void) StartMapGen;
- (void) processThread;
- (void) process3ArcSecondMapFile:(NSString*)inFile lat: (int)lat lon:(int)lon outPath:(NSString*) outPath;
@end
@implementation GUI
- (void) StartMapGen
{
MapPathString = @"/";
OutputPathString = @"/Users/";
currentFile = 0;
totalFiles = 0;
[MapPathString retain];
[OutputPathString retain];
DIR *pdir;
struct dirent *entry;
NSLog(@"Map path: %@",MapPathString);
pdir = opendir([MapPathString UTF8String]);
if (pdir) {
entry = readdir(pdir);
NSLog(@"Processing entry %p!",entry);
while (entry) {
NSLog(@"The name: %s",entry->d_name);
if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
NSLog(@"Incrementing!");
totalFiles++;
}
entry = readdir(pdir);
}
closedir(pdir);
if (totalFiles > currentFile) {
NSLog(@"Calling detach!");
[NSThread detachNewThreadSelector:@selector(processThread)
toTarget:self
withObject:nil];
while (1) {
}
}
}
}
- (void) processThread{
NSLog(@"In processThread!");
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
DIR *pdir;
struct dirent *entry;
NSString *outPath = [[NSString alloc] initWithString:OutputPathString];
[outPath retain];
pdir = opendir([MapPathString UTF8String]);
NSLog(@"About to read: %@",MapPathString);
if (pdir) {
entry = readdir(pdir);
while (entry) {
if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
if (!strncmp(entry->d_name + (strlen(entry->d_name) - 4),
".hgt", 4)) {
int lat, lon;
char tmp[4];
tmp[0] = entry->d_name[1]; tmp[1] = entry->d_name[2]; tmp[2] = '\0';
lat = ((entry->d_name[0] == 'S') ? -1: 1) * atoi(tmp);
tmp[0] = entry->d_name[4]; tmp[1] = entry->d_name[4];
tmp[2] = entry->d_name[6]; tmp[3] = '\0';
lon = ((entry->d_name[3] == 'W') ? -1: 1) * atoi(tmp);
lon = (360 + lon) % 360;
lat = (90 - lat) % 180;
NSString *inFilePath = [[NSString alloc] initWithFormat:@"%@/%s",
MapPathString, entry->d_name];
[inFilePath retain];
NSLog(@"About to call!");
//At this point all NSString values & ints are what I expect them to be.
[self process3ArcSecondMapFile: inFilePath lat:lat lon:lon outPath:outPath];
[inFilePath release];
NSLog(@"And we're back!");
}
}
currentFile++;
entry = readdir(pdir);
}
closedir(pdir);
}
[MapPathString release];
[OutputPathString release];
[outPath release];
[pool drain];
}
- (void) process3ArcSecondMapFile:(NSString*)inFile lat: (int)lat lon:(int)lon outPath:(NSString*) outPath
{ // Crashes at this LINE with EXC_BAD_ACCESS, inFile and outPath invalid.
NSLog(@"The in: %@, the lat: %d, the lon: %d, the out: %@",inFile,lat,lon,outPath);
int a = 0;
a++;
}
@end
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
GUI *myGUI = [[GUI alloc] init];
[myGUI StartMapGen];
[pool drain];
}
-Lee