Application unexpected crash while editing Text element (with C++)



  • Hello all,
    I need to get current language in mobile phone and I do it via C++ like this:

    main.cpp
    @
    #include <QtGui/QApplication>
    #include "qmlapplicationviewer.h"
    #include <QSystemInfo>
    #include <QDeclarativeContext>

    Q_DECL_EXPORT int main(int argc, char *argv[])
    {
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    QDeclarativeView view;
    QtMobility::QSystemInfo *sysinfo;
    QString lang = sysinfo->currentLanguage();
    
    view.setSource(QUrl::fromLocalFile&#40;"main.qml"&#41;);
    view.show();
    
    QDeclarativeContext *context = view.rootContext();
    
    context->findChild<QObject*>("language")->setProperty("text", lang);
    
    return app->exec&#40;&#41;;
    

    }
    @

    MainPage.qml
    @
    import QtQuick 1.1
    import com.nokia.symbian 1.1

    Page {
    id: mainPage

    Text {
        width: 360
        height: 640
        color: "#ffffff"
        font.pointSize: 23
        objectName: "language"
        visible: true
        onTextChanged: {
            console.log(text);
        }
    }
    

    }
    @

    Qt Creator does not report any error but simulator freezes (returned value -1073741819).



  • You dereference the sysinfo pointer without initialising it.



  • Also make sure that your createApplication(argc, argv) method takes the correct parameter decorations:

    @
    createApplication(int &argc, char *argv[]);
    @

    [[Doc:QCoreApplication]]'s constructors depend on being passed a reference to argc!



  • @Volker: Then template file of main function is not correctly written.



  • [quote author="Peppy" date="1338130869"]@Volker: Then template file of main function is not correctly written.[/quote]

    That's nonsense, of course. C/C++'s main function is way older than that of Qt and it's expected to work :)

    In fact, it's jut a matter of variable life times.

    main's argc variable exists as long as main is executed, thus a reference to it stays valid until main quits. The Q(Core)Application object exits before main exits. If not, you have a memory leak - which is another problem of its own.

    createApplication() eventually quits and returns a QApplication/QCoreApplication object. If createApplication has a copy of argc and not a reference, that copy is destroyed on returning, and the Q(Core)Application in turn has a reference to no longer existing int.



  • I am not tryin' to troll you or be smarter than you, but what what you get, when your main.cpp is generated? Just:
    @
    Q_DECL_EXPORT int main(int argc, char *argv[])
    {
    QScopedPointer<QApplication> app(createApplication(argc, argv)); // Yep, it's generated so. But I still have QtCreator 2.4.1, not 2.5 - maybe there is reason...

    QmlApplicationViewer viewer;
    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile&#40;QLatin1String("qml/MindBrowser/main.qml"&#41;);
    viewer.showExpanded();
    
    return app->exec&#40;&#41;;
    

    }
    @



  • Supposing that createApplication does not take a reference to argc, this is what happens:

    For simplicity, we break down the method to this simple version:

    @
    createApplication(int argc2, char *argv2[])
    {
    return new QApplication(argc2, argv2);
    }
    @

    You call createApplication(). The runtime system creates a copy of main's argc and passes that as argument to createApplication() which provides it as argc2 in its body.

    The [[Doc:QApplication]] takes argc2 as reference. It is vital for it that this reference stays alive during the complete life time of the application object - see the warning in the "QCoreApplication constructor docs":/doc/qt-4.8/qcoreapplication.html#QCoreApplication.

    createApplication() eventually returns the QApplication object and quits. That is the moment, when the runtime system destroys variable argc2.

    But the QApplication object still holds a reference to that variable - if the memory that was formerly used by argc2 is reused by some other object, and the QApplication tries to read or even write to its argc2 reference you access an invalid memory location - boom!

    This all does not apply to main(), as main's argc variable is destroyed after the QApplication object has been destroyed.

    WARNING:
    It may happen that your program works if using a copy of argc in createApplication, but that's by pure chance and it is not guaranteed!

    WARNING 2:
    The very same holds true if you subclass QCoreApplication or QApplication and do not use a reference to argc in your own constructors!


Log in to reply
 

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