The following code snippet has a memory leak that I spent too much time chasing down. The problem is that inside Foo(), the local variable x_ hides the member variable x_. It's quite annoying too, because the compiler could have warned me about it. Is there a flag in GCC for such a warning? (For the curious: I have arrived at the buggy code by first using a local variable, then changing it to a member variable, but forgetting to remove the type declaration.)
struct A {
A() x_(NULL) {}
~A() {
delete x_;
}
void Foo() {
HugeThingy* x_ = new HugeThingy();
x_->Bar("I. Need. Garbage. Collection. Now.");
}
HugeThingy* x_;
DISALLOW_COPY_AND_ASSIGN(A); // Macro to prevent copy/assign.
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…