Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ - Seg Fault from Deleting Dynamic Array
    text
    copied!<p>Having some trouble deleting a dynamically allocated array, and I'm not 100% sure why. The only thing I do to this array is copy over some values from it individually (in a for loop), in another routine, which is verified to work correctly.</p> <p>Here is the declaration in class:</p> <pre><code>std::complex&lt;float&gt; * frameData; </code></pre> <p>instantiation in constructor:</p> <pre><code>this-&gt;frameData = new std::complex&lt;float&gt;[n]; srand( time(NULL) ); std::complex&lt;float&gt; randUnityRoot; for( int i = 0; i &lt; this-&gt;n; i++){ randUnityRoot = std::polar(1.0, 2*M_PI * (rand() % 1000000)/1e06); this-&gt;frameData[i] = randUnityRoot; } </code></pre> <p>deletion in destructor(this is the line 70 mentioned in the backtrace):</p> <pre><code>delete[] this-&gt;frameData; </code></pre> <p>gdb backtrace after segfault at program completion:</p> <pre><code>(gdb) f 4 #4 0x00007ffff7bc579c in Frame::~Frame (this=0x602940, __in_chrg=&lt;optimized out&gt;) at Frame.cpp:70 70 delete[] this-&gt;frameData; (gdb) f 3 #3 0x00007ffff7669b96 in ?? () from /lib/x86_64-linux-gnu/libc.so.6 (gdb) f 2 #2 0x00007ffff765f39e in ?? () from /lib/x86_64-linux-gnu/libc.so.6 (gdb) f 1 #1 0x00007ffff7624b8b in abort () from /lib/x86_64-linux-gnu/libc.so.6 (gdb) f 0 #0 0x00007ffff7621425 in raise () from /lib/x86_64-linux-gnu/libc.so.6 (gdb) </code></pre> <p>I've been staring at this for a while and am right out of ideas. I figured I would turn to the hive mind. Please let me know if you would like any more information. Thanks!</p> <p>EDIT: I updated everything to a vector and vector* based approach.</p> <p>No segfaults :).</p> <p>To generate some sort of "knowledge gained" from this however, in another class I had called something like:</p> <pre><code>std::complex&lt;float&gt; * frameGet; frameGet = this-&gt;Frame-&gt;getFrame(); // Do stuff with frameGet //THIS NEXT LINE IS THE BAD PART delete[] frameGet; </code></pre> <p>Half question, half assertion: delete[] frameGet calls delete on original array content? If frameGet needs to be deleted should do something like:</p> <pre><code>frameGet = NULL; delete frameGet; </code></pre>
 

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