简体   繁体   English

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

[英]Unable to Run application via Terminal but application working fine in XCode

I am working on lilgp which is ac language based tool for Genetic Programming. 我正在研究lilgp ,它是基于交流语言的遗传编程工具。 The problem that i am facing is that I am using XCode for the project and it is working fine and shows correct output via terminal. 我面临的问题是我正在使用XCode进行项目,它工作正常并通过终端显示正确的输出。 But when i try to run the same application in DerivedData of my project in XCode i get segmentation fault (11) 但是当我尝试在XCode中的项目的DerivedData中运行相同的应用程序时,我得到了分段错误(11)

Then i have checked the console in Utilities for errors which shows error like this 然后我检查了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 

However, when i checked app.m:614 it has if (strcmp(c, "regress_asim") != 0) { where c is a chracter array and this array is working fine from last couple of months and in XCode too. 然而,当我检查app.m:614它有if (strcmp(c, "regress_asim") != 0) {其中c是一个chracter数组,这个数组在过去几个月和XCode中工作正常。 Can anybody tell me what am i missing? 谁能告诉我我错过了什么?

UPDATE: 更新:

1) The Resolved Project settings for both Debug and Release are same (except for the binary paths for sure) 1)Debug和Release的Resolved Project设置是相同的(确保二进制路径除外)

2) Code not only works in XCode but is also working in eclipse and creating a working binary. 2)代码不仅适用于XCode,而且还在eclipse中工作并创建工作二进制文件。

3) The part of the code that crashes in that the array c is: 3)代码崩溃的部分,数组c是:

Declared as 宣称为

char c[100];

Initillized as 初始化为

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

The code block where it crashes 崩溃的代码块

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

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. 在堆栈转储中,它显示您正在使用fscanf从文件中读取数据。 Later, strcmp crashes. 后来, strcmp崩溃了。

The obvious idea is that fscanf reads more characters than can fit in the char array, which leaves no space for the closing \\0 . 显而易见的想法是, fscanf读取的字符数多于char数组中可容纳的字符数,因此不会为关闭\\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. strcmp (或任何其他代码,并不重要)运行超过字符串的结尾,直到它击中一些合理的东西,然后它崩溃。

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. 设置一些巨大的东西,比如4096,来验证方法,然后找到真正的修复。

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. 任何这些都可能是char数组溢出的原因。

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. 第二个想法:转储行VM Regions Near 0x68显示它转储的内存区域,其内容看起来像ls结果或路径/文件名的一部分( /Users/USER/Library/Developer/Xcode/DerivedData/Theisis-gsze.... ),因此该路径的变量可能太短。 Make that one longer too and try. 让那个更长,然后尝试。

Edit: c[100] is too small, the path shown there is 115 chars . 编辑: c[100]太小,显示的路径有115个字符 Note that .\\Thesis will be replaced by \\Users\\... by the OS, which then is longer than 100 chars. 请注意.\\Thesis OS将替换为\\Users\\... ,然后超过100个字符。
Declare c[260] (or even larger) and you will be good. 声明c[260] (甚至更大),你会很好。

According to these lines in the debug output: 根据调试输出中的这些行:

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

What is causing the crash is the fscanf, not the strcmp. 导致崩溃的原因是fscanf,而不是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). 检查来自fopen的返回值,在测试用例中它必须为NULL(很可能是找不到正在读取的文件;从XCode运行程序时,工作目录可能与您预期的不同)。

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 return s; 如果没有,请使用一些return ;

You need to check return values in C 您需要在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. 从发布代码的其余部分看,看起来错误检查是一个问题(MALLOC?)所以复制文件路径(未发布?)可能有相同的问题并溢出“c”缓冲区,导致fopen返回NULL并导致fscanf失败。 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. 检查返回值...如果strncpy返回c [sizeof(c)-1]中没有'\\ 0'的字符串 - 则不能使用它来打开文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Xcode生成的应用程序运行良好,不会直接从.app运行 - Application runs fine when built from Xcode, won't run directly from the .app 如何在Qt应用程序中通过terminal命令运行分离的应用程序? - How to run a detached application by terminal command in Qt application? C ++调用getenv(“ LINES”)或getenv(“ COLUMNS”)在Xcode中运行良好,在终端中运行时出现段错误 - C++ calling getenv(“LINES”) or getenv(“COLUMNS”) runs fine in Xcode, segfaults when run in terminal 在终端上为通过C ++中的exec启动的应用程序处理用户输入 - Handing user input on terminal for application launched via exec in C++ 通过 Bazel 在 macOS 上构建并运行 Qt 应用程序 - Build and run a Qt application on macOS via Bazel 在Linux下,如何通过.html运行应用程序 - Under linux, how to run application via .html Linux C++,使应用程序打开终端并在其上运行 - Linux C++, make application open terminal and run on it 仅当通过finder启动Xcode 5控制台应用程序时,segfaults - Xcode 5 console application segfaults only when launched via finder 尝试在使用纯g ++或Xcode的Mac上编译可运行的irrlicht应用程序 - Trying to compile a working irrlicht application on a mac with plain g++ or Xcode 当终端中的一切正常时,为什么Xcode无法链接PCL? - Why does the Xcode fails to link PCL, when everything is fine in the terminal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM