I have a member variable called neuronLayers
which is of type std::vector<Eigen::RowVectorXf*>
and another called weights
of type std::vector<Eigen::MatrixXf*>
.
As part of a forward propagation function, I need to multiply values from these two vectors and keep a pointer to that value. I've already made sure the sizes of the dimensions of values I'm multiplying match, so there's no concern there.
I came up with this:
for (unsigned i = 1; i < this->topology.size(); i++) {
delete this->neuronLayers[i];
this->neuronLayers[i] = new Eigen::RowVectorXf((*this->neuronLayers[i - 1]) * (*this->weights[i - 1]));
}
Unfortunately, memory management is a weak point of mine, so I might be missing something blindingly obvious. I'm just conscious that carelessly creating new
objects can lead to memory leaks.
When I ask if this is safe, what I want to know is if this could cause to memory leak, or somehow lead to some other memory error? If it could, how could I modify that code snippet to fix this issue?
Edit:
neuronLayers
is used elsewhere with other code that expects it to be a vector of Eigen::RowVectorXf*
pointers rather than a vector of Eigen::RowVectorXf
objects. The snippet shown doesn't show this.