Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Critical error while freeing memory [closed]

Tags:

c++

I have a structure "Indices" containing buffer for indices (DirectX, but I think it doesn't matter):

struct Indices {
    CComPtr<ID3D11Buffer> buffer;
    UINT indexCount;
};

and a method which initializes array with objects of class Indices:

mIndices = new Indices*[layers];
for( int i = 0; i < layers; ++i )
    mIndices[i] = new Indices[corrections];

//... initializing buffers

and method which frees memory:

for( int i = 0; i < layers; ++i )
    delete mIndices[i];                // here I am getting critical error

delete mIndices;

but when I try to release the memory I am getting "Critical error detected c0000374" (pointed out in the code above).

Could you help me, please? I hope the posted code will be enough to solve my problem.

Thanks

like image 564
tobi Avatar asked Feb 05 '26 23:02

tobi


2 Answers

When you create arrays with new T[n], you also have to use delete[] to release the memory:

for( int i = 0; i < layers; ++i )
    delete[] mIndices[i];

delete[] mIndices;

Manual memory management is a hazzle, leading easily to crashes and memory leaks. Have you considered std::vector? It can be used as a drop-in replacement for dynamic arrays:

// create and initialize the arrays
std::vector< std::vector<Indices> > indices(layers, std::vector<Indices>(corrections));

// will be automatically freed when lifetime ends
like image 60
Jens Avatar answered Feb 07 '26 12:02

Jens


Since you are allocating arrays, you should be deallocating arrays. Use delete[] instead of delete.

like image 36
imreal Avatar answered Feb 07 '26 12:02

imreal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!