Note that there are some explanatory texts on larger screens.

plurals
  1. PO"this" pointer becomes null c++
    primarykey
    data
    text
    <p>I have a C++ Qt static library with two classes - <em>dataprocthread</em> and <em>calcvalue</em>. In the first one, when I call a method from an instance of <em>calcvalue</em>, pointer <code>this</code> (which references to <em>dataprocthread</em> class) suddenly becomes <code>null</code>.</p> <p>This is <em>dataprocthread.h</em>:</p> <pre><code>class DataProcThread : public QThread { Q_OBJECT public: DataProcThread(int sarray[9], int val); signals: workDone(int result); private: int _sarray[9]; int _val; void run(); }; </code></pre> <p><em>dataprocthread.cpp</em>:</p> <pre><code>DataProcThread::DataProcThread(int sarray[9], int val) { for (int x = 0; x &lt; 9; x++) { _sarray[x] = sarray[x]; } _val = val; } void DataProcThread::run() { CalcValue* cv = new CalcValue(); int myval = 0; for (int i = 0; i &lt; 100; i++) { myval = cv-&gt;processData(this-&gt;_val); if (this-&gt;_sarray[0] != myval) { //do something } } emit workDone(intValue); } </code></pre> <p><em>calcvalue.h</em>:</p> <pre><code>class CalcValue { public: CalcValue(); int processData(int someval); }; </code></pre> <p><em>calcvalue.cpp</em>:</p> <pre><code>CalcValue::CalcValue() { } int processData(int someval) { //do something and return int value } </code></pre> <p>When I run this code, it suddenly recieves signal "Segmentation fault". Using debugger, I found that the problem in <code>DataProcThread::run()</code> function: when I call <code>cv-&gt;processData</code> function, all works good. But on the next line (<code>if (this-&gt;_sarray[0] != myval)</code>), <code>this</code> pointer becomes <code>null</code> (I can see it in Locals window), therefore, I can't access <code>this-&gt;_sarray</code> variable.</p> <p>In case it is important, that's how I start the thread (from another class outside my library):</p> <pre><code>DataProcThread* thread = new DataProcThread(sarray, val); QObject::connect(thread, SIGNAL(workDone(int)), this, SLOT(collectWork(int))); thread-&gt;start(); </code></pre> <p>What I am doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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