Unable to Run application via Terminal but application working fine in XCode Unable to Run application via Terminal but application working fine in XCode c c

Unable to Run application via Terminal but application working fine in XCode


Without seeing the code, this is like repairing a car in absolute darkness. Try it.

However, I'll give it a try.

In the stack dump, it shows that you are using fscanf to read data from a file. Later, strcmp crashes.

The obvious idea is that fscanf reads more characters than can fit in the char array, which leaves no space for the closing \0. strcmp (or any other code, doesn't really matter) the runs past the end of the string, until it hits something sensible, and then it crashes.

If this is the reason, making the buffer longer would fix it (temporarily). Set it something huge, like 4096, to verify the approach, and then find the real fix.

If you ask why the same program would work in one place, but not in the other - you are probably reading a different file, so the content of the file has different lengths; or you simple reading the file in a different directory, which has a longer name; any of those can be the reason for the char array overflow.

Second idea: The dump line VM Regions Near 0x68 shows the memory area where it dumped, and its content looks like a part of a ls result or a path/filename (/Users/USER/Library/Developer/Xcode/DerivedData/Theisis-gsze....), so potentially your variable for that path is too short. Make that one longer too and try.

Edit: c[100] is too small, the path shown there is 115 chars. Note that .\Thesis will be replaced by \Users\... by the OS, which then is longer than 100 chars.
Declare c[260] (or even larger) and you will be good.


According to these lines in the debug output:

 Thread 0 Crashed:: Dispatch queue: com.apple.main-thread0   libsystem_c.dylib               0x00007fff93a8b09e flockfile + 41   libsystem_c.dylib               0x00007fff93a8d463 fscanf + 156

What is causing the crash is the fscanf, not the strcmp.Check the return values from fopen, it must be NULL in your test case (most probably the file that is being read hasnt been found; when running the program from XCode, the working directory may not be the same as you were expecting).

In your case, this should suffice:

FILE *in_file = fopen(c, "r");if (in_file == NULL) {    perror( "The following IO error occurred" );    error( E_FATAL_ERROR, "IO error" );}int r = fscanf(in_file, "%d", &fitness_cases);if (r != 1) {    perror( "The following IO error occurred" );    fclose( in_file );    error( E_FATAL_ERROR, "Read Error" );}

Assuming that your error function jumps out of the function. If not, use some returns;


You need to check return values in C

    FILE *in_file = fopen(c, "r");    /* insert error checking code here */    fscanf(in_file, "%d", &fitness_cases);    /* otherwise it fails here when fscanf tries to read from NULL */    if (strcmp(c, "regress_asim") != 0) {  //Line 614

From the looks at the rest of the posted code, it looks like error checking is an issue (MALLOC?) so the copying of the file path (not posted?) probably has the same issues and overflows the "c" buffer, causing fopen to return NULL and causing fscanf to fail. CHECK RETURN VALUES... if strncpy returns a string that doesn't have '\0' in c[sizeof(c)-1] - you cannot use it to open a file.