Using QSettings on Mac crashes on launch. Is there a fix?



  • Hello everyone!

    First of all, I apologise if I've posted this in then wrong place.

    I've written a small application and it uses QSettings to save user preferences. This seams to work ok on Linux and Windows, but not on Mac.

    When I launch the application (on launch, the app tries to read any user preferences) the app crashes straight away giving an error related to QMacSettingsPrivate.

    If you would like to see the full error report, it can be found here.

    I saw that there were some bug reports and others having the same issues. There was a patch attached to this bug report. I tried to apply that patch but git said that those filed don't exist more. I found them located somewhere else (in qtbase). The git repo I was using was git://code.qt.io/qt/qt5.git.

    So I was wondering if there is any other known solution to this?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    There's a link to a gerrit submission that you can use to patch and build Qt. Since you can reproduce the crash from the bug report, please update the report with the information you have to reproduce it.



  • You are using QSettings in a global variable. Move the initialisation inside the main().

    Looks like you are using a statically linked Qt, right ? Your QSettings is a global variable, QSettings implementation itself rely on other global variables. The order of initialisation of global variables is undefined.
    What could work, from worst to best:

    • shuffle the link order around to get QSettings globals initialised first (that would be a very fragile fix)
    • link Qt as a shared library, shared lib would be loaded and initialised before your code.
    • Don't build a QSettings as a global, replace it with a QSettings * and build it in main().


  • @SGaist Hello. Thanks for your response. I'm just recompiling Qt now with the patch from the gerrit submission. If it does work, I will link a minimal example in the bug report that will reproduce the initial error. Is that the best way to do it?



  • @sandy.martel23 Thanks for your response. I'm going to see if the patch in the bug report fixes the error. If not this will be what I try next :) Just a quick question about it though (if that's ok) :)

    At the moment I've got globalsettings.cpp. In there is where I have 'QSettings settings' and then I refer to this object in some static functions which are in globalsettings.cpp. Example below:

    // globalsettings.cpp
    #include <QSettings>
    #include "globalsettings.h"
    
    // Initialise some variables from globalsettings.h.
    
    QSettings settings;
    
    void GlobalSettings::saveSetting(int day) // This function is defined statically in globalsettings.h.
    {
        settings.setValue("dayNumber", day);
    }
    

    In main.cpp I define the information needed for QCoreApplication which is why I haven't included app name etc as parameters in the QSettings object.

    So my question, is, where should I move the initialisation of the QSettings object, as unless I've misunderstood (possible because I'm a newbie), I can't have a main loop in globalsettings.cpp, and if I put it in the main loop in main.cpp, it won't be accessible. As I said, I may have totally misunderstood but would appreciate any clarification :)


  • Lifetime Qt Champion

    You don't need a global QSettings. Just create one when you need to either read or write a setting and be done with it.



  • @SGaist Thanks. I assume this means that the bug report was not related to my problem? Thanks for your help!


  • Lifetime Qt Champion

    The bug report was about a corrupted file which doesn't match your use case.

    All in all, since you have already built Qt with the patch applied, did you also test it for your use case ?



  • @SGaist Qt with the patch applied is still building ;( I tested the above with the original build I was using when I got the error and the program loaded with no problems. Thanks again for your help :)


  • Lifetime Qt Champion

    Still building ? What machine do you have ? What options did you pass to configure ?



  • @SGaist A 2011 Mac book pro. The reason it's still building because it failed about an hour in while I was out. So I'm building it again :)


  • Lifetime Qt Champion

    If you only need the core modules for your application, just build qtbase, that will save you lots of times



  • @SGaist Thanks - not sure if i did that or not, lol, but I'll do it next time as I'm only using the core modules :)



  • @Crossy said:

    QSettings settings;

    void GlobalSettings::saveSetting(int day) // This function is defined statically in globalsettings.h.
    {
    settings.setValue("dayNumber", day);
    }

    SGaist is right, the code should look like this:

    void GlobalSettings::saveSetting(int day) // This function is defined statically in globalsettings.h.
    {
    QSettings settings; // <-- NOT a global
    settings.setValue("dayNumber", day);
    }

    This is much better than your current implementation, since the destructor of QSettings will save/sync the preferences right away.
    And if the global is really what is causing the problem, that would fix it.



  • @sandy.martel23 Thanks for your help :) It was the global causing the problem - after changing it to how you and SGaist suggested, it has worked :) Thank you!


  • Lifetime Qt Champion

    Good !

    Since everything is working now, please mark the thread as solved using the "Topic Tool" button so that other forum users may know a solution has been found :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.