if operator breaks signal-slot
-
if(true)makes my tim never starts... Why? If I removeif(true)then my tim again counts...int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); if(true) //remove me to work MainClass mainClass(QCoreApplication::instance()); int ret= QCoreApplication::exec(); return ret; }My tim code
MainClass::MainClass(QObject *parent) : QObject(parent) { connect(&tim, &QTimer::timeout, &obj, &MyClass::mySlot); tim.start(1000); } void MyClass::mySlot(){ static uint i; qDebug() << "hi" << i++; } -
@SimonSchroeder said in if operator breaks signal-slot:
std::unique_ptr instead of a raw pointer
In my code I use parent
MainClass *mainClass(new MainClass(QCoreApplication::instance()));so why I should use
deleteorunique_ptr? Is it bad advice?@DungeonLords said in if operator breaks signal-slot:
so why I should use delete or unique_ptr? Is it bad advice?
Using a parent with Qt is perfectly fine and the preferred way of handling this.
-
if(true)makes my tim never starts... Why? If I removeif(true)then my tim again counts...int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); if(true) //remove me to work MainClass mainClass(QCoreApplication::instance()); int ret= QCoreApplication::exec(); return ret; }My tim code
MainClass::MainClass(QObject *parent) : QObject(parent) { connect(&tim, &QTimer::timeout, &obj, &MyClass::mySlot); tim.start(1000); } void MyClass::mySlot(){ static uint i; qDebug() << "hi" << i++; }@DungeonLords said in if operator breaks signal-slot:
if(true) //remove me to work MainClass mainClass(QCoreApplication::instance());Think about the lifetime of
mainClass.This, by the way, has nothing to do with the
ifitself, any Signals & Slots, the timer or anything with Qt.
Not even a C++ issue, because other OOP languages also have scopes in which objects are "living" and "valid" before they are destructed. -
@DungeonLords said in if operator breaks signal-slot:
if(true) //remove me to work MainClass mainClass(QCoreApplication::instance());Think about the lifetime of
mainClass.This, by the way, has nothing to do with the
ifitself, any Signals & Slots, the timer or anything with Qt.
Not even a C++ issue, because other OOP languages also have scopes in which objects are "living" and "valid" before they are destructed.@Pl45m4 so
if(true) MainClass *mainClass(new MainClass(QCoreApplication::instance()));is right solution for my case?
-
@Pl45m4 so
if(true) MainClass *mainClass(new MainClass(QCoreApplication::instance()));is right solution for my case?
@DungeonLords said in if operator breaks signal-slot:
MainClass *mainClass(new MainClass(QCoreApplication::instance()));Why two instances ofmainClassnow?!
[Edit: I think I've misread that. It should also work.
I've seen twonewin there. One inside the other as parent, instead of the pointer initialization]MainClass *mainClass = new MainClass(&a);should be good.
Also fine:
MainClass *mainClass; if ( /* condition */) mainClass = new MainClass; int ret = QCoreApplication::exec(); if (mainClass) delete mainClass; return ret;But why you want to put the creation inside the
ifscope? -
@DungeonLords said in if operator breaks signal-slot:
MainClass *mainClass(new MainClass(QCoreApplication::instance()));Why two instances ofmainClassnow?!
[Edit: I think I've misread that. It should also work.
I've seen twonewin there. One inside the other as parent, instead of the pointer initialization]MainClass *mainClass = new MainClass(&a);should be good.
Also fine:
MainClass *mainClass; if ( /* condition */) mainClass = new MainClass; int ret = QCoreApplication::exec(); if (mainClass) delete mainClass; return ret;But why you want to put the creation inside the
ifscope? -
@Pl45m4 said in if operator breaks signal-slot:
Also fine:
MainClass *mainClass; [...] if (mainClass) delete mainClass;Except for the uninitialized pointer access when /* condition */ is false...
-
-
@aha_1980
What's wrong?delete-ing a variable which has valuenullptr? That's fine (i.e. guaranteed to accept and ignorenullptr; quite different fromdelete-ing an uninitialized variable). Whether you choose to write that or test fornullptrand not calldeleteis up to you. -
@aha_1980 said in if operator breaks signal-slot:
@Pl45m4 Still wrong ;)
My code inits the
MainClassptr tonullptr.
When it's properly heap allocated withnew( =>mainClass != nullptr), it deletes it at the end of the code.And if that part
if ( /* condition */) mainClass = new MainClass;is skipped,
mainClassis still anullptrand you don't have to delete it?!Explain please, what's exactly wrong with that?!
-
@aha_1980 said in if operator breaks signal-slot:
@Pl45m4 Still wrong ;)
My code inits the
MainClassptr tonullptr.
When it's properly heap allocated withnew( =>mainClass != nullptr), it deletes it at the end of the code.And if that part
if ( /* condition */) mainClass = new MainClass;is skipped,
mainClassis still anullptrand you don't have to delete it?!Explain please, what's exactly wrong with that?!
@Pl45m4 There is nothing wrong - the check for != nullptr is just superfluous :)
-
@Pl45m4 There is nothing wrong - the check for != nullptr is just superfluous :)
@Christian-Ehrlicher said in if operator breaks signal-slot:
the check for != nullptr is just superfluous
Ah well... yes. Because calling
deleteon anullptrdoesn't do anything.
So one could calldelete mainClassregardless. -
This would be the perfect place for
std::unique_ptrinstead of a raw pointer (usingstd::make_unique<MainClass>to create the object). Then we wouldn't have all this discussion aboutdelete... 😉 -
This would be the perfect place for
std::unique_ptrinstead of a raw pointer (usingstd::make_unique<MainClass>to create the object). Then we wouldn't have all this discussion aboutdelete... 😉@SimonSchroeder said in if operator breaks signal-slot:
std::unique_ptr instead of a raw pointer
In my code I use parent
MainClass *mainClass(new MainClass(QCoreApplication::instance()));so why I should use
deleteorunique_ptr? Is it bad advice? -
@SimonSchroeder said in if operator breaks signal-slot:
std::unique_ptr instead of a raw pointer
In my code I use parent
MainClass *mainClass(new MainClass(QCoreApplication::instance()));so why I should use
deleteorunique_ptr? Is it bad advice?@DungeonLords said in if operator breaks signal-slot:
so why I should use delete or unique_ptr? Is it bad advice?
Using a parent with Qt is perfectly fine and the preferred way of handling this.
-
D DungeonLords has marked this topic as solved on