S
S
syxme2020-05-12 08:26:04
C++ / C#
syxme, 2020-05-12 08:26:04

How to fix a memory leak?

class Layer
{
public:
  Layer();
  Layer(int size,int nextSize);
  int size;
  int nextSize;
  double* neurons;
  double* biases;
  double** weights;
};


void NeuralNetwork::backpropagation(double *targets)
{
    double* errors = new double[layers[layer_count - 1].size];
    for (int i = 0; i < layers[layer_count - 1].size; i++) {
        errors[i] = ( layers[layer_count - 1].neurons[i]- targets[i]);
    }
    for (int k = layer_count - 2; k >= 0; k--) {
        Layer* left= &layers[k];
        Layer* right = &layers[k + 1];
        double* errorsNext = new double[left->size];
        double *w_d = new double[right->size];
        double* w_err = new double[right->size];

        for (int i = 0; i < right->size; i++) {
            w_d[i] = errors[i] * derivative(right->neurons[i]);
            w_err[i] = w_d[i];
            w_d[i] *= learningRate;
        }
        double** deltas = new double*[right->size];
        for (int i = 0; i < right->size; i++) {
            deltas[i] = new double[left->size];
        }
        for (int i = 0; i < right->size; i++) {
            for (int j = 0; j < left->size; j++) {
                deltas[i][j] = w_d[i] * left->neurons[j];
            }
        }
        double **weightsNew = new double *[left->size];
        for (int i = 0; i < left->size; i++) {
            weightsNew[i] = new double[right->size];
        }

        for (int i = 0; i < right->size; i++) {
            for (int j = 0; j < left->size; j++) {
                weightsNew[j][i] = left->weights[j][i] - deltas[i][j];
            }
        }

        for (int i = 0; i < right->size; i++) {
            delete[] deltas[i];
        }
        for (int i = 0; i < layers[k].nextSize; i++) {
           delete [] layers[k].weights[i];
        }

        delete[]layers[k].weights;
        layers[k].weights = nullptr;
        delete[] deltas;
      
        layers[k].weights = weightsNew;
        int sizxx = sizeof(weightsNew);
        int sizxt = sizeof(weightsNew[0]);


        for (int i = 0; i < left->size; i++) {
            errorsNext[i] = 0;
            for (int j = 0; j < right->size; j++) {
                errorsNext[i] += weightsNew[i][j] * w_err[j];
            }
        }
       
        delete []errors;
        errors = new double[left->size];
        memcpy(errors, errorsNext, left->size * sizeof(double));
        for (int i = 0; i < right->size; i++) {
            right->biases[i] += w_d[i];
        }
        delete[]errorsNext;
        delete[]w_d;
        delete[]w_err;
          
    }
    delete[]errors;
}

Through heap profiling, I realized that the leak was in weightsNew.
In the code, I need to clear layers[k].weights and replace it with weightsNew.
As far as I understand, I clear the memory in layers[k].weights and write a pointer to weightsNew, but somehow it leaks.
Tell me how to deal with it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ananiev, 2020-05-12
@syxme

C++ recommends using std::vector for dynamic arrays. Then there will be no problems with memory leaks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question