I am working with c++ and been using valgrind to fix memory leaks. I am trying to optimize the following piece of code that valgrind characterizes as a leak:
void VOMC::sub_train(vector<Letter> tempLettersA, vector<Letter> tempLettersB) {
int stateA_id = state_exists(tempLettersA);
State *tempStateA;
if (stateA_id != -1) {
tempStateA = get_state_by_id(stateA_id);
} else {
tempStateA = new State(tempLettersA);// MEMORY LEAK - 1
vomc.push_back(tempStateA);
}
int stateB_id = state_exists(tempLettersB);
if (stateB_id != -1) {
tempStateA->inc_state(stateB_id);
} else {
State* tempStateB;
tempStateB = new State(tempLettersB);//MEMORY LEAK -2
vomc.push_back(tempStateB);
stateB_id = tempStateB->GetId();
tempStateA->inc_state(stateB_id);
}
}
For memory leak-1 I get the following message
==11289== 4,055 (64 direct, 3,991 indirect) bytes in 1 blocks are definitely lost in loss record 228 of 228
==11289== at 0x402B87E: operator new(unsigned int) (vg_replace_malloc.c:292)
==11289== by 0x807FA75: VOMC::sub_train(std::vector<Letter, std::allocator<Letter> >, std::vector<Letter, std::allocator<Letter> >) (VOMC.cpp:952)
For memory leak -2
==11289== at 0x402B87E: operator new(unsigned int) (vg_replace_malloc.c:292)
==11289== by 0x807FB16: VOMC::sub_train(std::vector<Letter, std::allocator<Letter> >, std::vector<Letter, std::allocator<Letter> >) (VOMC.cpp:968)
Can I delete these pointers? This would remove my leaks BUT as you can see the pointers are being pushed into a stack, the goal is to maintain them in the stack but remove the leak.
EDIT -1 : adding letter and state definitinons:
class Letter {
public:
Letter();
Letter(Helper *helper);
~Letter();
void add_note(RawNote r);
void evaluate_letter(double eigthNoteDuration);
void setNotePositionAccordingToLetter();
void empty_notes();
LetterPattern getPattern() const;
void setPattern(LetterPattern pattern);
bool isEmpty();
vector<RawNote> getRawNotes() const;
void setRawNotes(vector<RawNote> rawNotes);
bool has_note_no_velocity(RawNote* r1);
bool has_note_with_velocity(RawNote* r1);
private:
vector<LetterPattern> *allPossibleNotes;
vector<RawNote> rawNotes;
LetterPattern pattern;
};
class State {
public:
State(); //should not be used, it is only for testing
State(vector<Letter> letters);
virtual ~State();
int GetId() const;
void SetId(int id);
vector<Letter> GetLetters() const;
void SetLetters(vector<Letter> letters);
void AddLetters(vector<Letter> letters);
void inc_state(int state_id);
void print_state_letters();
bool has_state(int state_id);
void print_connected_states();
void print_sorted_states();
vector<string> get_rhythm_as_string();
map<int, double> GetConnected_states() const;
map<int, double> connected_states;
vector< pair <int, double > > vector_sorted_connected_states;
void bubblesort_vector_descending(vector< pair <int, double > > *v_sort);
int get_connected_state_stochastically();
static int id_generator;
CustomNumberDist *normal_dist;
int id_from_file; //only used on load of a file
private:
int id;
vector<Letter> letters;
};