Note that there are some explanatory texts on larger screens.

plurals
  1. POCall object instance from C++ function called via script
    text
    copied!<p>I have a class named Jellyfish who use the singleton design pattern :</p> <p>jellyfish.h</p> <pre><code>#ifndef JELLYFISH_H #define JELLYFISH_H #include &lt;QHash&gt; #include &lt;QScriptEngine&gt; class QScriptValue; class Jellyfish : public QObject { public: static Jellyfish * getInstance(); static Jellyfish * instance; private: Jellyfish(); }; #ifndef </code></pre> <p>jellyfish.cpp</p> <pre><code>Jellyfish * Jellyfish::instance = NULL; Jellyfish * Jellyfish::getInstance() { if ( !Jellyfish::instance ) { Jellyfish::instance = new Jellyfish(); } return Jellyfish::instance; } </code></pre> <p>When I am in main.cpp and make tests, I have no errors:</p> <p>main.cpp</p> <pre><code>#include &lt;QApplication&gt; #include "jellyfish.h" class Jellyfish; int main( int argc, char *argv[] ) { QApplication app( argc, argv ); Jellyfish *toto = Jellyfish::getInstance(); Jellyfish *toto2 = Jellyfish::getInstance(); Jellyfish *toto3 = Jellyfish::getInstance(); return app.exec(); } </code></pre> <p>But I want to use some static methods in <code>Jellyfish</code> in external QScript files :</p> <p>jellyfish.h</p> <pre><code>private: static QScriptValue set( QScriptContext *context, QScriptEngine *engine ); QScriptEngine *script_engine; </code></pre> <p>jellyfish.cpp</p> <pre><code>Jellyfish::Jellyfish() { script_engine = new QScriptEngine; /* ... */ initScriptEngine(); } void Jellyfish::initScriptEngine() { QScriptValue object = script_engine-&gt;newQObject( this ); object.setProperty( "set", script_engine-&gt;newFunction( set ) ); script_engine-&gt;globalObject().setProperty( "jellyfish", object ); } QScriptValue Jellyfish::set( QScriptContext *context, QScriptEngine *engine ) { // I have to load instance because I am in a static method. // But this is where the application loop endlessly. Jellyfish *jellyfish = Jellyfish::getInstance(); return true; } </code></pre> <p>And finaly the parsed script:</p> <pre><code>jellyfish.set( "line_numbers", true ); </code></pre> <p><strong>Problem</strong></p> <p>When I run the application, <code>getInstance()</code> always creates a new Jellyfish instance. But the real problem is that on the output with debug (<code>qDebug() &lt;&lt; "test";</code>) I can see that the application loop on <code>Jellyfish::getInstance();</code> until I get a <code>sgmentation fault</code>.</p> <p>Could someone help me to understand this ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload