递归函数中的Qvector崩溃随机

时间:2018-12-12 20:32:35

标签: c++ qt macos-mojave qvector

我在使用QT创建者时遇到了奇怪的问题。我正在使用最新版本的Qt Creator和Qvector函数(我也尝试过std :: vector和相同的问题)在for循环中,我正在调用递归函数,例如:

void add(bool in){
    global_var.push_back(in) //Qvector<bool>, global varible
    return;
}
void start(int x,int y,int z){
    for(int i=0;i<z;i++){
        if(i<z){
           add(true);
           start(x,y,z);
           start(x,y,z);
           return;
        }

    }
        add(false) 

    }

然后应用程序在某个时刻随机崩溃,我得到的唯一错误是:

    Process:               FIRST_program [24565]
Path:                  /Users/USER/Downloads/*/FIRST_program.app/Contents/MacOS/FIRST_program
Identifier:            feri.FIRST_program
Version:               0
Code Type:             X86-64 (Native)
Parent Process:        Qt Creator [23775]
Responsible:           FIRST_program [24565]
User ID:               501

Date/Time:             2018-12-12 21:24:41.838 +0100
OS Version:            Mac OS X 10.14.1 (18B75)
Report Version:        12
Bridge OS Version:     3.0 (14Y667)
Anonymous UUID:        F7D655B5-7798-C8EF-925D-0F2BF1B2ABD3

Sleep/Wake UUID:       6384DE06-A77B-4893-9D81-3BC040DA989F

Time Awake Since Boot: 52000 seconds
Time Since Wake:       11000 seconds

System Integrity Protection: enabled

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

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_PROTECTION_FAILURE at 0x00007ffee4a8bff8
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [24565]

VM Regions Near 0x7ffee4a8bff8:
    MALLOC_SMALL           00007f9884800000-00007f9885800000 [ 16.0M] rw-/rwx SM=PRV  
--> STACK GUARD            00007ffee128c000-00007ffee4a8c000 [ 56.0M] ---/rwx SM=NUL  stack guard for thread 0
    Stack                  00007ffee4a8c000-00007ffee528c000 [ 8192K] rw-/rwx SM=SHM  thread 0

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   macbook.FIRST_program               0x000000010a97d690 QBasicAtomicInteger<int>::load() const + 16 (qbasicatomic.h:103)
1   macbook.FIRST_program               0x000000010a97f155 QtPrivate::RefCount::isShared() const + 21 (qrefcount.h:101)
2   macbook.FIRST_program               0x000000010a980d38 QVector<bool>::isDetached() const + 24 (qvector.h:106)
3   macbook.FIRST_program               0x000000010a981920 QVector<bool>::append(bool const&) + 64 (qvector.h:679)
4   macbook.FIRST_program               0x000000010a97b64d QVector<bool>::push_back(bool const&) + 29 (qvector.h:260)
5   macbook.FIRST_program               0x000000010a97afc8 MainWindow::add(bool) + 40 (mainwindow.cpp:204)
6   macbook.FIRST_program               0x000000010a97ae22 MainWindow::start(int, int, int, int) + 242 (mainwindow.cpp:132)
7   macbook.FIRST_program               0x000000010a97ae9b MainWindow::start(int, int, int, int) + 363 (mainwindow.cpp:139)
8   macbook.FIRST_program               0x000000010a97ae9b MainWindow::start(int, int, int, int) + 363 (mainwindow.cpp:139)
9   macbook.FIRST_program               0x000000010a97ae9b MainWindow::start(int, int, int, int) + 363 (mainwindow.cpp:139)
10  macbook.FIRST_program               0x000000010a97ae9b MainWindow::start(int, int, int, int) + 363 (mainwindow.cpp:139)
11  macbook.FIRST_program               0x000000010a97ae9b MainWindow::start(int, int, int, int) + 363 (mainwindow.cpp:139)
12  macbook.FIRST_program               0x000000010a97ae9b MainWindow::start(int, int, int, int) + 363 (mainwindow.cpp:139)
13  macbook.FIRST_program               0x000000010a97ae9b MainWindow::start(int, int, int, int) + 363 (mainwindow.cpp:139)

1 个答案:

答案 0 :(得分:0)

我建议您在QVector中使用append在add元素中添加元素,它等于pus_back,因为QVector通常分配比必要的更多的内存。

void add(bool in){
    global_var.append(in) //Qvector<bool>, global varible
    return;
}

您的start方法将循环播放。如何调用启动方法? z的值在每次调用中都不会更改,如果z = 0,则表示条件不起作用。

那么,您真正要解决的问题是什么?