0

I have problem with qt console application, destructor of MyServer class is not being called. Here is my simplified code:

#include <QtCore/QCoreApplication>
#include "MyServer/myserver.h" 
int main(int argc, char *argv[])
{

    QCoreApplication a(argc, argv); 
    MyServer server;
    server.startServer();
    return a.exec();

}

myserver.h

class MyServer : public QTcpServer
    {
        Q_OBJECT

public:
    MyServer(QObject *parent = nullptr);
    ~MyServer();
    QFile* file;
}

myserver.cpp

MyServer::MyServer(QObject *parent)
    : QTcpServer(parent)
{
    file = new QFile("file.ini",this);

}


MyServer::~MyServer()
{
    QSettings settings(file->fileName(), QSettings::IniFormat, this);
    settings.beginGroup("testGroup");


    settings.setValue("testValue", "asdf");
        settings.endGroup();
}

Destructor should change the file.ini, but it doesnt.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
karollo
  • 573
  • 8
  • 22
  • 6
    How did you verify that `~MyServer()` is not called? – nwp Feb 08 '18 at 16:52
  • File editing procedure :) – karollo Feb 08 '18 at 16:56
  • It would not be called until `main()` returns. – drescherjm Feb 08 '18 at 16:56
  • 6
    Can you show that procedure or something similar that reproduces the problem? Try to make a [mcve], otherwise all we can do is guess. – nwp Feb 08 '18 at 16:58
  • 4
    You used `new` so you got screwed. Remove it and things will work properly. (it's not `~MyServer()` that didn't get called, it's `~QFile()`) – nwp Feb 08 '18 at 17:09
  • Your code is incomplete; in particular, it seems to be missing at least one `#include` line. Please [edit] your code so it's a [mcve] of your problem, then we can try to reproduce and solve it. You should also read [ask]. – Toby Speight Feb 08 '18 at 17:19
  • I added includes, but they are irrelevant – karollo Feb 08 '18 at 17:21
  • In case it wasn't clear, by "remove it" I meant make `file` a `QFile` instead of a `QFile *`. That way the destructor will automatically be called at the right time. – nwp Feb 08 '18 at 17:30
  • I understood, but I used parent/cild mechanism, so file will be destroyed. But the problem is with ~MyServer. Any reason, why it is not being called? – karollo Feb 08 '18 at 17:32
  • 1
    Put a breakpoint in the destructor or printed something. – nwp Feb 08 '18 at 17:33
  • Reasons why the destructor wouldn't be called is because someone does `exit(0);` or the program crashes before it manages to get around to clean up. Another reason might be that Qt's parent/child mechanism just fails here. – nwp Feb 08 '18 at 17:35
  • Maybe there is a dependency in `QSettings` that `QCoreApplication` still has an event loop. – drescherjm Feb 08 '18 at 17:39
  • Another thinking! could this be related to QTcpServer destructor? – Mohammad Kanan Feb 08 '18 at 18:36
  • Who gets destroyed first, the parent or the child? If it is the child, `file` will be destroyed before the parent (`MyServer`) destructor is called. Therefore the writes to the file could not happen. – jwernerny Feb 08 '18 at 20:13
  • Reading the answers to this question on [https://stackoverflow.com/a/2491811/447438] (Memory management in Qt?) should also help. – jwernerny Feb 08 '18 at 20:22
  • In the parent's ~QObject() the child is freed. This happens after ~MyServer() finishes. – drescherjm Feb 08 '18 at 22:32

1 Answers1

2

MyServer::file might be your problem. Are you opening that file for write access somewhere else in your code? Because QSettings will attempt to also open the same file, and your two concurrent accesses to the same file might be clobbering its contents.

If you don't need MyServer::file for anything, then get rid of it. QSettings does not need QFile. It only needs the filename.

Nikos C.
  • 50,738
  • 9
  • 71
  • 96