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.
Here is the declaration in class:
std::complex<float> * frameData;
instantiation in constructor:
this->frameData = new std::complex<float>[n];
srand( time(NULL) );
std::complex<float> randUnityRoot;
for( int i = 0; i < this->n; i++){
randUnityRoot = std::polar(1.0, 2*M_PI * (rand() % 1000000)/1e06);
this->frameData[i] = randUnityRoot;
}
deletion in destructor(this is the line 70 mentioned in the backtrace):
delete[] this->frameData;
gdb backtrace after segfault at program completion:
(gdb) f 4
#4 0x00007ffff7bc579c in Frame::~Frame (this=0x602940,
__in_chrg=<optimized out>) at Frame.cpp:70
70 delete[] this->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)
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!
EDIT: I updated everything to a vector and vector* based approach.
No segfaults :).
To generate some sort of "knowledge gained" from this however, in another class I had called something like:
std::complex<float> * frameGet;
frameGet = this->Frame->getFrame();
// Do stuff with frameGet
//THIS NEXT LINE IS THE BAD PART
delete[] frameGet;
Half question, half assertion: delete[] frameGet calls delete on original array content? If frameGet needs to be deleted should do something like:
frameGet = NULL;
delete frameGet;
You most probably do not have either copy-ctor nor assignment operator explicitly defined in your class. So describe them in private section of your class (make then inaccessible):
class Frame {
//...
private:
Frame( const Frame & );
Frame &operator=( const Frame & );
// ...
};
you do not even have to implement them and compiler will show exact locations where your problem is by compilation errors.
About second half of your question. For some reason you think if you assign one raw pointer to another you copy that memory and need to delete them separately. That not the case, raw pointer is just a number - address in memory. So:
int *pi = new int[N]; // now pi has address of memory reserved by new, lets say 0x1234
int *api = pi; // now api has the same address 0x1234 as pi
delete[] api; // you tell environment that you do not need memory at address 0x1234 anymore
delete[] pi; // you tell environment that you do not need memory at address 0x1234 again, which is error
The fact you just assign one pointer to another directly or do that through function result or parameter pass does not change anything.