I wrote code very similar to this (although not identical) for a game I'm making. This has the same issue, so I'll provide it.
#include <iostream>
using namespace std;
class test {
public:
int* arr;
test(int n) {
arr = new int[n];
for(int i=0; i<=9; i++) {
arr[i] = i;
}
}
~test() {
delete[] arr;
}
};
int main() {
test obj = test(10);
for(int i=0; i<=8; i++) {
cout << obj.arr[i] << endl;
}
cout << obj.arr[9];
obj.~test();
return 0;
}
I know that I could rework my code and use the vector class to avoid this problem, but I want to actually understand why it's crashing. Removing the deconstructor call fixes the error, but that also introduces a memory leak, so I would prefer to just fix the deconstructor.