无法通过终端运行应用程序,但应用程序在XCode中正常运行

时间:2016-09-10 20:18:02

标签: c++ c xcode c++11

我正在研究lilgp这是一种基于c语言的遗传编程工具。我面临的问题是我正在使用XCode进行项目,它工作正常并通过终端显示正确的输出。但是当我尝试在XCode的项目的DerivedData中运行相同的应用程序时,我得到了分段错误(11)

然后我检查了Utilities中的控制台是否有错误,显示错误,如

    Process:               Theisis [9325]
Path:                  /Users/USER/Library/Developer/Xcode/DerivedData/Theisis-gszeehddtmnlkqdbicpeffygvkcw/Build/Products/Release/Theisis
Identifier:            Theisis
Version:               0
Code Type:             X86-64 (Native)
Parent Process:        bash [8987]
Responsible:           Terminal [299]
User ID:               501

Date/Time:             2016-09-11 01:05:25.158 +0500
OS Version:            Mac OS X 10.11.6 (15G31)
Report Version:        11
Anonymous UUID:        4063B9C3-F525-D9BD-EF5E-358810571673

Sleep/Wake UUID:       CA5341A7-C252-4C76-B694-7F2DAE196F79

Time Awake Since Boot: 57000 seconds
Time Since Wake:       1600 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000068

VM Regions Near 0x68:
--> 
    __TEXT                 0000000100c4e000-0000000100c6a000 [  112K] r-x/rwx SM=COW  /Users/USER/Library/Developer/Xcode/DerivedData/Theisis-gszeehddtmnlkqdbicpeffygvkcw/Build/Products/Release/Theisis

 Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libsystem_c.dylib               0x00007fff93a8b09e flockfile + 4
1   libsystem_c.dylib               0x00007fff93a8d463 fscanf + 156
2   Theisis                         0x0000000100c57853 app_initialize + 195 (app.m:614)
3   Theisis                         0x0000000100c4f245 main + 453 (main.m:205)
4   libdyld.dylib                   0x00007fff8e0575ad start + 1

Thread 0 crashed with X86 Thread State (64-bit): 
  rax: 0x00007fff5efb1970  rbx: 0x0000000000000000  rcx: 0x00000b0000000000  rdx: 0x0000000100c6aa8c
  rdi: 0x0000000000000000  rsi: 0x0000000100c675d4  rbp: 0x00007fff5efb1860  rsp: 0x00007fff5efb1860
   r8: 0x00000000fffffffc   r9: 0x00007fff740b1c10  r10: 0x00007fff97709e01  r11: 0x00007fff93a8d3c7
  r12: 0x450022575a4d98d4  r13: 0x0000000000000000  r14: 0x0000000100c675d4  r15: 0x0000000000000000
  rip: 0x00007fff93a8b09e  rfl: 0x0000000000010246  cr2: 0x0000000000000068

Logical CPU:     4
Error Code:      0x00000004
Trap Number:     14

VM Region Summary:
ReadOnly portion of Libraries: Total=176.1M resident=0K(0%) swapped_out_or_unallocated=176.1M(100%)
Writable regions: Total=19.6M written=0K(0%) resident=0K(0%) swapped_out=0K(0%) unallocated=19.6M(100%)

                                VIRTUAL   REGION 
REGION TYPE                        SIZE    COUNT (non-coalesced) 
===========                     =======  ======= 
Activity Tracing                  2048K        2 
Kernel Alloc Once                    4K        2 
MALLOC                            9604K       17 
Stack                             64.0M        3 
VM_ALLOCATE                          4K        2 
__DATA                            8300K      141 
__LINKEDIT                        91.4M        4 
__TEXT                            84.7M      146 
__UNICODE                          552K        2 
shared memory                        8K        3 
===========                     =======  ======= 
TOTAL                            260.2M      312 

但是,当我检查app.m:614时它有if (strcmp(c, "regress_asim") != 0) { ,其中c是一个chracter数组,这个数组在过去几个月和XCode中工作正常。谁能告诉我我错过了什么?

更新

1)Debug和Release的Resolved Project设置相同(当然,二进制路径除外)

2)代码不仅适用于XCode,而且还可以在eclipse中工作并创建工作二进制文件。

3)代码中崩溃的部分是数组c

声明为

char c[100];

初始化为

 strncpy(c, "Equation_Default_data.csv", sizeof(c));

崩溃的代码块

 if (!startfromcheckpoint) {
        oprintf( OUT_PRG, 50, "not starting from checkpoint file.\n");

        param = get_parameter("app.fitness_cases");
        if (param == NULL)
            fitness_cases = 200;
        else {
            fitness_cases = atoi(param);
            if (fitness_cases < 0)
                error( E_FATAL_ERROR,
                      "invalid value for \"app.fitness_cases\".");
        }
        FILE *in_file = fopen(c, "r");
        fscanf(in_file, "%d", &fitness_cases);
        if (strcmp(c, "regress_asim") != 0) {  //Line 614
            app_y_desired = (double *) MALLOC(fitness_cases * sizeof(double));
            app_fitness_cases[0] = (double *) MALLOC(
                                                     fitness_cases * sizeof(double));
            app_fitness_cases[1] = (double *) MALLOC(
                                                     fitness_cases * sizeof(double));
            app_fitness_cases[2] = (double *) MALLOC(
                                                     fitness_cases * sizeof(double));
            app_fitness_cases[3] = (double *) MALLOC(
                                                     fitness_cases * sizeof(double));

            memset(app_fitness_cases[2], 0, fitness_cases * sizeof(double));
            memset(app_fitness_cases[3], 0, fitness_cases * sizeof(double));
            memset(app_y_desired, 0, fitness_cases * sizeof(double));

        }
        app_fitness_importance = (int *) MALLOC(fitness_cases * sizeof(int));
        //Asim Code
        double x, y;
        for (i = 0; i < fitness_cases; ++i) {
            fscanf(in_file, "%lf", &x);
            fscanf(in_file, "%lf", &y);
            app_fitness_cases[0][i] = x;
            app_fitness_cases[1][i] = y;
            if (strcmp(c, "regress_asim") != 0) {
                app_y_desired[i] = y;
            }
            app_fitness_importance[i] = checkImportance(x);
        }
        fclose(in_file);
        datapointsPerImportance = (int*) MALLOC((max_datapoint_importance+1)*sizeof(int));
        memset(datapointsPerImportance, 0, (max_datapoint_importance+1)*sizeof(int));
        for (i = 0; i < fitness_cases; ++i) {
          // printf("%d : %d\n",i,checkImportance(app_fitness_cases[0][i]));
            datapointsPerImportance[checkImportance(app_fitness_cases[0][i])]=datapointsPerImportance[checkImportance(app_fitness_cases[0][i])]+1;

        }
        for(int i=0;i<=max_datapoint_importance;i++)
        {
            printf("Importance %d =%d\n",i,datapointsPerImportance[i]);
        }
        /*oprintf( OUT_PRG, 50, "%d fitness cases:\n", fitness_cases);
         for (i = 0; i < fitness_cases; ++i) {
         x = (random_double() * 2.0) - 1.0;

         // change this line to modify the goal function.
         y = x * x * x * x + x * x * x + x * x + x;

         app_fitness_cases[0][i] = x;
         app_fitness_cases[1][i] = y;

         // oprintf( OUT_PRG, 50, "    x = %12.5lf, y = %12.5lf\n", x, y);
         }*/
    } else {
        oprintf( OUT_PRG, 50, "started from checkpoint file.\n");
    }

3 个答案:

答案 0 :(得分:5)

没有看到代码,这就像在绝对黑暗中修理汽车一样。试试吧。

但是,我试一试。

在堆栈转储中,它显示您正在使用fscanf从文件中读取数据。之后,strcmp崩溃了。

显而易见的想法是fscanf读取的字符多于char数组中可容纳的字符数,因此不会为结束\0留下空间。 strcmp(或任何其他代码,并不重要)超过字符串结尾的运行,直到它击中一些合理的东西,然后它崩溃。

如果这是原因,那么延长缓冲区会暂时解决(暂时)。设置一些巨大的东西,比如4096,来验证方法,然后找到真正的修复。

如果你问为什么同一个程序可以在一个地方工作,而不是在另一个地方 - 你可能正在读一个不同的文件,所以文件的内容有不同的长度;或者您只是在不同的目录中读取文件,该目录的名称较长;其中任何一个都可能是char数组溢出的原因。

第二个想法:转储行VM Regions Near 0x68显示它转储的内存区域,其内容看起来像ls结果或路径/文件名(/Users/USER/Library/Developer/Xcode/DerivedData/Theisis-gsze....)的一部分,所以可能是你的变量因为这条路太短了。让它更长,然后尝试。

修改: c[100]太小,显示的路径是 115个字符。请注意,操作系统会将.\Thesis替换为\Users\...,然后超过100个字符。
声明c[260](甚至更大),你会很好。

答案 1 :(得分:4)

根据调试输出中的这些行:

 Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libsystem_c.dylib               0x00007fff93a8b09e flockfile + 4
1   libsystem_c.dylib               0x00007fff93a8d463 fscanf + 156

导致崩溃的原因是fscanf,而不是strcmp。 检查来自fopen的返回值,在测试用例中它必须为NULL(很可能是找不到正在读取的文件;从XCode运行程序时,工作目录可能与您期望的不同)。 / p>

在你的情况下,这应该足够了:

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" );
}

假设你的错误函数跳出了函数。如果没有,请使用一些return s;

答案 2 :(得分:3)

您需要检查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

从发布代码的其余部分看,看起来错误检查是一个问题(MALLOC?)所以复制文件路径(未发布?)可能会出现同样的问题并溢出&#34; C&#34;缓冲区,导致fopen返回NULL并导致fscanf失败。检查返回值...如果strncpy返回的字符串没有&#39; \ 0&#39;在c [sizeof(c)-1]中 - 你不能用它来打开文件。