Can you explain me this code?
-
wrote on 16 Oct 2018, 07:54 last edited by thippu
1)I have code like this
void MainWindow::openInifile() { QSettings settings(new QSettings("filename path and filename",QSettings::IniFormat)); setSettingsObject(settings);//I think it is passing its content that is a reference, am I right? }
- In the function, they are creating the
QSettings
object on the stack memory but passing the heap memory of the same type reference. will that stack object get deleted when it goes out of scope? and let me know the reason and what is the advantage of it.
- In the function, they are creating the
-
1)I have code like this
void MainWindow::openInifile() { QSettings settings(new QSettings("filename path and filename",QSettings::IniFormat)); setSettingsObject(settings);//I think it is passing its content that is a reference, am I right? }
- In the function, they are creating the
QSettings
object on the stack memory but passing the heap memory of the same type reference. will that stack object get deleted when it goes out of scope? and let me know the reason and what is the advantage of it.
@thippu said in Can you explain me this code?:
setSettingsObject
Can you show its signature?
"I think it is passing its content that is a reference, am I right?" - can't you simply check its declaration to answer this question?
- In the function, they are creating the
-
@thippu said in Can you explain me this code?:
setSettingsObject
Can you show its signature?
"I think it is passing its content that is a reference, am I right?" - can't you simply check its declaration to answer this question?
wrote on 16 Oct 2018, 08:01 last edited by@jsulm said in Can you explain me this code?:
Can you show its signature?
void setSettingsObject(const QSettings &settings);
-
@jsulm said in Can you explain me this code?:
Can you show its signature?
void setSettingsObject(const QSettings &settings);
@thippu In this case you need to check what setSettingsObject does to know whether it is a bug or not. Can you show its content?
Actually, as long as setSettingsObject does not store a pointer to settings there will not be any error. Most probably they just assign it to an instance variable, in which case it is copied (assignment/copy operator). -
1)I have code like this
void MainWindow::openInifile() { QSettings settings(new QSettings("filename path and filename",QSettings::IniFormat)); setSettingsObject(settings);//I think it is passing its content that is a reference, am I right? }
- In the function, they are creating the
QSettings
object on the stack memory but passing the heap memory of the same type reference. will that stack object get deleted when it goes out of scope? and let me know the reason and what is the advantage of it.
wrote on 16 Oct 2018, 08:04 last edited by@thippu said in Can you explain me this code?:
QSettings settings(new QSettings("filename path and filename",QSettings::IniFormat));
yeah, this is really poor. Since
QSettings
is aQObject
it compiles as it uses this constructor but the object allocated in the heap is unused and leaked.
Whoever wrote that code should undergo dire punishment - In the function, they are creating the
-
@thippu said in Can you explain me this code?:
QSettings settings(new QSettings("filename path and filename",QSettings::IniFormat));
yeah, this is really poor. Since
QSettings
is aQObject
it compiles as it uses this constructor but the object allocated in the heap is unused and leaked.
Whoever wrote that code should undergo dire punishment@VRonin Oh, somehow I missed that "new" :-)
-
@thippu In this case you need to check what setSettingsObject does to know whether it is a bug or not. Can you show its content?
Actually, as long as setSettingsObject does not store a pointer to settings there will not be any error. Most probably they just assign it to an instance variable, in which case it is copied (assignment/copy operator).wrote on 16 Oct 2018, 08:11 last edited by@jsulm said in Can you explain me this code?:
In this case you need to check what setSettingsObject does to know whether it is a bug or not. Can you show its content?
It was from the example "Settings editor", I'm studying the mechanism of it.
-
@jsulm said in Can you explain me this code?:
In this case you need to check what setSettingsObject does to know whether it is a bug or not. Can you show its content?
It was from the example "Settings editor", I'm studying the mechanism of it.
-
@jsulm said in Can you explain me this code?:
In this case you need to check what setSettingsObject does to know whether it is a bug or not. Can you show its content?
It was from the example "Settings editor", I'm studying the mechanism of it.
@thippu said in Can you explain me this code?:
example "Settings editor"
Oh, if this is from official Qt example than you should file a bug as this is a bad example then.
-
wrote on 16 Oct 2018, 08:13 last edited by
@jsulm said in Can you explain me this code?:
Oh, somehow I missed that "new" :-)
Really, you are the author?. :-)
-
@jsulm said in Can you explain me this code?:
Oh, somehow I missed that "new" :-)
Really, you are the author?. :-)
-
@thippu said in Can you explain me this code?:
QSettings settings(new QSettings("filename path and filename",QSettings::IniFormat));
yeah, this is really poor. Since
QSettings
is aQObject
it compiles as it uses this constructor but the object allocated in the heap is unused and leaked.
Whoever wrote that code should undergo dire punishment@VRonin said in Can you explain me this code?:
Whoever wrote that code should undergo dire punishment
Well, should be possible. It's been written here
http://doc.qt.io/qt-5/qtwidgets-tools-settingseditor-mainwindow-cpp.html
should be in the docu change log right :)
-
-
@jsulm said in Can you explain me this code?:
In this case you need to check what setSettingsObject does to know whether it is a bug or not. Can you show its content?
It was from the example "Settings editor", I'm studying the mechanism of it.
wrote on 16 Oct 2018, 09:17 last edited by VRonin@thippu said in Can you explain me this code?:
It was from the example "Settings editor", I'm studying the mechanism of it.
The example is correct:
typedef QSharedPointer<QSettings> SettingsPtr;
and thenSettingsPtr settings(new QSettings(locationDialog->format(), locationDialog->scope(), locationDialog->organization(), locationDialog->application()));
or
SettingsPtr settings(new QSettings(fileName, QSettings::IniFormat));
The problem is you took the liberty of replacing a shared pointer with a stack object
-
@thippu said in Can you explain me this code?:
It was from the example "Settings editor", I'm studying the mechanism of it.
The example is correct:
typedef QSharedPointer<QSettings> SettingsPtr;
and thenSettingsPtr settings(new QSettings(locationDialog->format(), locationDialog->scope(), locationDialog->organization(), locationDialog->application()));
or
SettingsPtr settings(new QSettings(fileName, QSettings::IniFormat));
The problem is you took the liberty of replacing a shared pointer with a stack object
6/15