# HG changeset patch # User ronw@google.com # Date 1370986913 0 # Node ID 76f749d29b48801d6967962b4d4d062129674039 # Parent bcb0c53a2fc50f6c900442abd1afc6f7fbd389d1 Fix memory leak in CARFAC. Also get rid of most uses of auto, which tend to hurt readability unless the type name is particularly long, especially when it masks pointers. diff -r bcb0c53a2fc5 -r 76f749d29b48 trunk/carfac/carfac.cc --- a/trunk/carfac/carfac.cc Tue Jun 11 21:32:50 2013 +0000 +++ b/trunk/carfac/carfac.cc Tue Jun 11 21:41:53 2013 +0000 @@ -36,6 +36,12 @@ Reset(num_ears, sample_rate, car_params, ihc_params, agc_params); } +CARFAC::~CARFAC() { + for (Ear* ear : ears_) { + delete ear; + } +} + void CARFAC::Reset(const int num_ears, const FPType sample_rate, const CARParams& car_params, const IHCParams& ihc_params, const AGCParams& agc_params) { @@ -118,12 +124,12 @@ if (mix_coeff > 0) { ArrayX stage_state; ArrayX this_stage_values = ArrayX::Zero(num_channels_); - for (const auto& ear : ears_) { + for (Ear* ear : ears_) { stage_state = ear->agc_memory(stage); this_stage_values += stage_state; } this_stage_values /= num_ears_; - for (const auto& ear : ears_) { + for (Ear* ear : ears_) { stage_state = ear->agc_memory(stage); ear->set_agc_memory(stage, stage_state + mix_coeff * (this_stage_values - stage_state)); @@ -134,7 +140,7 @@ } void CARFAC::CloseAGCLoop() { - for (auto& ear : ears_) { + for (Ear* ear : ears_) { ArrayX undamping = 1 - ear->agc_memory(0); // This updates the target stage gain for the new damping. ear->set_dzb_memory((ear->zr_coeffs() * undamping - ear->zb_memory()) / diff -r bcb0c53a2fc5 -r 76f749d29b48 trunk/carfac/carfac.h --- a/trunk/carfac/carfac.h Tue Jun 11 21:32:50 2013 +0000 +++ b/trunk/carfac/carfac.h Tue Jun 11 21:41:53 2013 +0000 @@ -47,6 +47,7 @@ CARFAC(const int num_ears, const FPType sample_rate, const CARParams& car_params, const IHCParams& ihc_params, const AGCParams& agc_params); + ~CARFAC(); void Reset(const int num_ears, const FPType sample_rate, const CARParams& car_params, const IHCParams& ihc_params, diff -r bcb0c53a2fc5 -r 76f749d29b48 trunk/carfac/carfac_output.cc --- a/trunk/carfac/carfac_output.cc Tue Jun 11 21:32:50 2013 +0000 +++ b/trunk/carfac/carfac_output.cc Tue Jun 11 21:41:53 2013 +0000 @@ -36,25 +36,25 @@ void CARFACOutput::AppendOutput(const vector& ears) { if (store_nap_) { nap_.push_back(vector()); - for (const auto& ear : ears) { + for (Ear* ear : ears) { nap_.back().push_back(ear->ihc_out()); } } if (store_ohc_) { ohc_.push_back(vector()); - for (const auto& ear : ears) { + for (Ear* ear : ears) { ohc_.back().push_back(ear->za_memory()); } } if (store_agc_) { agc_.push_back(vector()); - for (const auto& ear : ears) { + for (Ear* ear : ears) { agc_.back().push_back(ear->zb_memory()); } } if (store_bm_) { bm_.push_back(vector()); - for (const auto& ear : ears) { + for (Ear* ear : ears) { bm_.back().push_back(ear->zy_memory()); } } diff -r bcb0c53a2fc5 -r 76f749d29b48 trunk/carfac/carfac_test.cc --- a/trunk/carfac/carfac_test.cc Tue Jun 11 21:32:50 2013 +0000 +++ b/trunk/carfac/carfac_test.cc Tue Jun 11 21:41:53 2013 +0000 @@ -86,13 +86,13 @@ // This loads a vector of ArrayXs from multi-column text files. vector Load2dTestData(const string filename, const int rows, - const int columns) { + const int columns) { string fullfile = kTestSourceDir + filename; ifstream file(fullfile.c_str()); FPType myarray[rows][columns]; vector output; output.resize(rows); - for (auto& timepoint : output) { + for (ArrayX& timepoint : output) { timepoint.resize(columns); } if (file.is_open()) { diff -r bcb0c53a2fc5 -r 76f749d29b48 trunk/carfac/ear.cc --- a/trunk/carfac/ear.cc Tue Jun 11 21:32:50 2013 +0000 +++ b/trunk/carfac/ear.cc Tue Jun 11 21:41:53 2013 +0000 @@ -73,7 +73,7 @@ void Ear::ResetAGCState() { int n_agc_stages = agc_coeffs_.size(); agc_state_.resize(n_agc_stages); - for (auto& stage_state : agc_state_) { + for (AGCState& stage_state : agc_state_) { stage_state.decim_phase = 0; stage_state.agc_memory.setZero(num_channels_); stage_state.input_accum.setZero(num_channels_); @@ -175,8 +175,8 @@ bool Ear::AGCRecurse(const int stage, ArrayX agc_in) { bool updated = true; - const auto& agc_coeffs = agc_coeffs_[stage]; - auto& agc_state = agc_state_[stage]; + const AGCCoeffs& agc_coeffs = agc_coeffs_[stage]; + AGCState& agc_state = agc_state_[stage]; // This is the decim factor for this stage, relative to input or prev. stage: int decim = agc_coeffs.decimation; // This is the decim phase of this stage (do work on phase 0 only):