Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy heap corruption when returning an object Mat?
    text
    copied!<p>I don't understand why I get a corrupted heap error with this program (I'm using OpenCV for the class <code>Mat</code>): </p> <pre><code>class A { private: Mat image; static UINT ThreadProc( LPVOID pParam ) { A* pThis= (ClientNetwork*)pParam; UINT nRet= pThis-&gt;DoThreadProc(); // get out of 'static mode' return( nRet ); } UINT ClientNetwork::DoThreadProc() { vector&lt;uchar&gt; vect; while(1) { /**** initialize vect and get the image data to decode ****/ decode(vect); } } public: void decode(const vector&lt;uchar&gt;&amp; vectorData){image=imdecode(vectorData, CV_LOAD_IMAGE_COLOR);} Mat get_image(){return image;} void start() {m_pcThread= AfxBeginThread(ThreadProc, this );} } int main() { A* a = new A(); a-&gt;start(); while(1) { Mat image = a-&gt;get_image(); } delete a; return 0; } </code></pre> <p>It seems that the error come from <code>Mat image = a-&gt;get_image();</code> because if I return a reference instead of a copy of the object, I don't have error anymore:</p> <pre><code>Mat* get_image(){return &amp;image;} </code></pre> <p>and </p> <pre><code>Mat* image = a-&gt;get_image(); </code></pre> <p>I read that returning a copy of an object is more elegant in C++ than a reference. So I would like to know what is wrong.</p> <p>EDIT: Visual studio breaks at <code>a-&gt;decode(vect)</code> but it happens only when I return an object and not a reference.</p> <p>EDIT 2: I edited the code to reflect the full program. I think the problem comes from the shared object <code>a</code> which is copy and modified at the same time. I will see if the problem still occur using a mutex.</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