Problems keeping objects alive
<p>I've encountered a weird problem that doesn't make any sense to me.</p>

<p>I've a struct (that contains string) on an API defined as follows:</p>

<pre><code>typedef struct sNCharcb
{
    char * pData;
    int    iDataLen;
}
tsNCharcb;
</code></pre>

<p>I need to save a deep copy of this struct. I have created an utility function to make a copy of this struct:</p>

<pre><code>inline sNCharcb rapi_strcpy(const sNCharcb &amp;rapistr)
{
    sNCharcb res;

    res.pData = new char[rapistr.iDataLen];
    strcpy(res.pData, rapistr.pData);
    res.iDataLen = rapistr.iDataLen;

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

<p>I create copies of those "sNCharcb" structs using this utility method and save them to reference variables in a parent object:</p>

<pre><code>stored_sNCharcb = rapi_strcpy(sNCharcb_to_copy);
</code></pre>

<p>After a short while these stored values are magically changed to contain some random garbage. The parent object where these values are stored is inside the scope all the time and it's not destructed. What might be causing these values to get wiped prematurely?</p>
