2016-11-25 90 views
0

所以,我终于发现这个奇怪的问题,我没有找到答案。qtimer在创建构造函数或静态时崩溃

我创建了一个小GUI,在一个单独的窗口中启动应用程序,然后使用qtimer轮询此应用程序的状态。

process_timer = new QTimer(this); 
    connect(process_timer, SIGNAL(timeout()), this, SLOT(checkFlashProcess())); 
    process_timer->start(100); 

所以这个工作。但我宁可不创建一个新的计时器每一次,所以我放置在GUI的构造process_timer的创建:

Flasher::Flasher(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::Flasher) 
{ 
    ui->setupUi(this); 
    process_timer = new QTimer(this); 
} 

现在,这导致崩溃和输出: 的QObject ::连接:不能连接(空)::超时()到闪蒸器:: checkFlashProcess()

同样为这样:

Flasher::Flasher(QWidget *parent) : 
QMainWindow(parent), 
ui(new Ui::Flasher), 
process_timer(new QTimer) 
{... 

QTimer * process_timer在应用报头定义。

我也试图定义process_timer非动态:

header.h: 
     QTimer process_timer; 
    code.cpp 
    void Flasher::on_flashButton_clicked() 
    { 
    (...) 
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, process_pid); 
    if(hProcess) 
    { 
     qDebug() << "Got handle for process!"; 
     connect(&process_timer, SIGNAL(timeout()), this, SLOT(checkFlashProcess())); 
     process_timer.start(30); 
    } 

这也导致崩溃。

回调:

void Flasher::checkFlashProcess() 
{ 
     qDebug() << "Got handle for process!"; 
} 

但是这是为什么呢?我猜计时器不会在构造函数中创建,但在构造函数中创建对象应该不是问题吧?而且为什么静态版本也会崩溃,这会是同一个问题吗?

+1

'connect'调用在哪里,什么时候调用相对于'Flasher'对象实例的构造? – alexisdm

+0

连接在gui上的按钮的槽中调用。 'void Flasher :: on_flashButton_clicked() { ... hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,FALSE,process_pid); if(hProcess) qDebug()<<“获得进程句柄!”; process_timer = new QTimer(this); connect(process_timer,SIGNAL(timeout()),this,SLOT(checkFlashProcess())); process_timer-> start(30); }' –

+0

你是否100%肯定'process_timer'在创建和调用'connect()'之间的某处没有设置为null?您是否在调试器中停止了connect()调用并调查了它? – Googie

回答

0

好吧,看起来这与qtimer无关,而是一个记忆问题。我正在四处寻找如何在windows中获取进程句柄,并且主要发现关于unix的源代码。然后在thesetwo我明显地把事情混淆了,最后使用了一个对于process_pid的DWORD,这似乎已经放在内存中的计时器之前。因此调用openprocess会导致腐败并导致崩溃。在启动应用程序后创建定时器,通过再次恢复定时器来“固定”这个定时器。

另一个经验教训,使用qPro64从qProcess到OpenProcess的pid工作正常。希望别人可以在寻找类似的东西时找到这个,并且可以避免这种麻烦,谢谢你让我创造出一个孤立的问题来解决问题。

+0

你是如何找出内存泄漏的原因的?什么是mcve? – ransh