c++ object pointer lost after calling a function of the object
<p>I defined my matrix class and I put here the simplifed version:</p>

<pre><code>// matrix of double type
typedef std::vector&lt; std::vector&lt; double &gt; &gt; dbmatrix; 

class MMatrix
{
private:
    // matrix with size of rowsNum times colsNum
    size_t rowsNum; // number of rows
    size_t colsNum; // number of columns

protected:
    dbmatrix mMat; // the matrix

public:
    // Constructor
    MMatrix(size_t m, size_t n, double initVal); 

...

    //  Display matrix in console (unsuitable for large matrix)
    void display(void);
};

// initialize a m-by-n matrix with an initial Value
MMatrix::MMatrix(size_t m, size_t n, double initVal)
{
    this-&gt;rowsNum = m;
    this-&gt;colsNum = n;

    this-&gt;mMat.reserve(this-&gt;rowsNum);

    for (uint i = 0; i &lt; this-&gt;rowsNum; i++)
    {
        dbvector oneRow(this-&gt;colsNum, initVal);
        this-&gt;mMat.push_back(oneRow);
    }
}

// Display matrix in console
void MMatrix::display()
{
    for (uint i = 0; i &lt; rowsNum; i++)
    {
        for (uint j = 0; j &lt; colsNum; j++)
        {
            std::cout &lt;&lt; mMat[i][j] &lt;&lt; " ";
        }
        std::cout &lt;&lt; std::endl;
    }
    std::cout &lt;&lt; std::endl;
}
</code></pre>

<p>Then I generated a 2 dimensional pointer to receive the result from the function "matrixDiff"</p>

<pre><code>MMatrix** divH = matrixDiff(*V, fwdKer, false);
</code></pre>

<p>The weird thing happened when I tried the following code after it:</p>

<pre><code>(*divH[0]).display();
(*divH[0]).display();
</code></pre>

<p>before I executed the first line, my object pointer looks normal (fig1)
<a href="http://i.stack.imgur.com/2Q94n.png" rel="nofollow">fig1.pointer to a 10-by-10 matrix looks normal</a>
The first line successfully give the wanted result. </p>

<p>However, after execute the first line, the second line (which is exactly the same) throw a Access violation. The debugger indicates the pointer lost its target after executing the first line (fig2)
<a href="http://i.stack.imgur.com/iWuRO.png" rel="nofollow">fig2. pointer lost its target</a>.</p>

<p>In additional test, I tried:</p>

<pre><code>divH[0]-&gt;display();
divH[0]-&gt;display();
</code></pre>

<p>Which is still not working. However, when I tried the following code:</p>

<pre><code>MMatrix* testMat = divH[0];
testMat-&gt;display();
testMat-&gt;display();
</code></pre>

<p>It works perfectly, which completely make me confused.</p>

<p>What's more, I also tried to display the matrix inside the function "matrixDiff" I create:</p>

<pre><code>MMatrix** matrixDiff(MMatrix depthMat, MMatrix ker, bool isDirect)
{
    MMatrix* retMat[3];

    retMat[0] = filter(depthMat, ker);
    retMat[1] = filter(depthMat, (~ker));


...

    retMat[0] -&gt; display();
    retMat[1] -&gt; display();

    retMat[2] = diffMag; // gradient magnitude

    return retMat;
}
</code></pre>

<p>With the same two-line commands, it works either! </p>

<p>So, I am super confused now about c++ pointers. It seems that passing result through object pointers will change something, though I originally thought it only pass the address of the object. Could you point out what the real problem is here? Thank you!</p>