【发布时间】:2012-10-11 09:54:36
【问题描述】:
我正在使用 c++ 并一直在使用 valgrind 来修复内存泄漏。 我正在尝试优化以下被 valgrind 描述为泄漏的代码:
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);
}
}
对于内存泄漏 1,我收到以下消息
==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)
对于内存泄漏-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)
我可以删除这些指针吗?这将消除我的泄漏,但是您可以看到指针被推入堆栈,目标是将它们保留在堆栈中但消除泄漏。
编辑 -1:添加字母和状态定义:
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;
};
【问题讨论】:
-
帮自己一个忙,将指针更改为智能指针。我在
<memory>中推荐shared_ptr。 -
有时你必须
delete[]他们,否则他们会被泄露。我们不知道你在程序的其余部分做了什么,或者为什么你必须在 vomc 中存储指针,所以很难给出建议。 -
@stefan 等等,你怎么知道所有权是共享的?我错过了什么?你有帖子上没有的信息吗? @Potney 帮自己一个忙...考虑先不要使用任何智能级别的指针,然后再使用
unique_ptr。 -
@PotneySwitters 不,这不是真的。当拥有它的指针被销毁时,该对象被销毁。除非您将其设为 const(因此不可转让),否则您可以将所有权转移到堆栈中,然后在堆栈销毁时将其销毁。
-
@PotneySwitters:使用
new的主要原因是为了完全控制对象的生命周期,从而超越“在声明处出生,在作用域结束处死亡”的生命周期堆栈分配的对象得到。如果堆栈分配足够好,那么不要使用new,你不会有内存泄漏;)我们可以看到Letter和State的定义吗?
标签: c++ memory-leaks valgrind