Originally Posted by
pkjag
I do understand C++ but you are being too vague. What variable is out of what function's scope??
If you understood C++ you wouldn't be asking that question.
int func() {
A a;
}
int func() {
A a;
}
To copy to clipboard, switch view to plain text mode
In the code above local variable "a" goes out of scope (its own scope, as in "it dies") when "func" function returns as the stack unwinds destroying all local variables.
You mentioned i have a memory leak problem but you haven't showed me how i fix
I did:
Originally Posted by
wysota
Delete the object causing the leak once you don't need it anymore or use some mechanism (like Qt's parent-child relationship or the signal-slot mechanism together with deleteLater() slot) that will do that for you.
that hence my question how i should use the allocated memory thats causing the leak.
Use it any way you want. The point is to seal the leak (aka delete the object) once you are done using it. That's really one of the most basic things in C++ and I'm sure that if you spent at least 10 hours learning that language, you should have been taught (or learnt yourself) to clean up memory once you are finished using it.
You previously mentioned sonething along the lines of slots and signals, i've about a months worth experience with Qt and i don't know how to use them in this situation. I don't know why its so difficult for you to help me with this memory leak situation.
You don't need any Qt experience for that. Here is an example:
Leak:
class A {
public:
A() { m_var = 0; }
void func() { m_var = new B; }
private:
B *m_var;
};
class A {
public:
A() { m_var = 0; }
void func() { m_var = new B; }
private:
B *m_var;
};
To copy to clipboard, switch view to plain text mode
No leak:
class A {
public:
A() { m_var = 0; }
~A() { delete m_var; }
void func() { m_var = new B; }
private:
B *m_var;
};
class A {
public:
A() { m_var = 0; }
~A() { delete m_var; }
void func() { m_var = new B; }
private:
B *m_var;
};
To copy to clipboard, switch view to plain text mode
Another example:
Leak:
class A {
public:
A() {}
void func() { B *var = new B; var->doSomething(); /* var not needed anymore */ }
};
class A {
public:
A() {}
void func() { B *var = new B; var->doSomething(); /* var not needed anymore */ }
};
To copy to clipboard, switch view to plain text mode
No leak:
class A {
public:
A() { }
void func() { B var; var.doSomething(); /* var not needed anymore */ }
};
class A {
public:
A() { }
void func() { B var; var.doSomething(); /* var not needed anymore */ }
};
To copy to clipboard, switch view to plain text mode
And another example, this time using Qt's facilities:
Leak:
public:
void func() { B *var = new B; }
};
class A : public QObject {
public:
A(QObject *parent =0) : QObject(parent) { }
void func() { B *var = new B; }
};
To copy to clipboard, switch view to plain text mode
No leak:
public:
void func() { B *var = new B(this); }
};
class A : public QObject {
public:
A(QObject *parent =0) : QObject(parent) { }
void func() { B *var = new B(this); }
};
To copy to clipboard, switch view to plain text mode
And one more:
Leak:
class A {
public:
A() { }
void func() { B *var = new B; }
};
class A {
public:
A() { }
void func() { B *var = new B; }
};
To copy to clipboard, switch view to plain text mode
No leak:
class A {
public:
A() { }
void func() { B *var = new B; var->deleteLater(); }
};
class A {
public:
A() { }
void func() { B *var = new B; var->deleteLater(); }
};
To copy to clipboard, switch view to plain text mode
And yet another one:
Leak:
public:
void func() { B *var = new B; }
};
class A : public QObject {
public:
A(QObject *parent =0) : QObject(parent) { }
void func() { B *var = new B; }
};
To copy to clipboard, switch view to plain text mode
No leak:
public:
void func() {
B *var = new B(this);
connect(var, SIGNAL(finishedIamNotUsedAnymore()), var, SLOT(deleteLater()));
}
};
class A : public QObject {
public:
A(QObject *parent =0) : QObject(parent) { }
void func() {
B *var = new B(this);
connect(var, SIGNAL(finishedIamNotUsedAnymore()), var, SLOT(deleteLater()));
}
};
To copy to clipboard, switch view to plain text mode
Let me spell it out again for you: fixing memory leaks means freeing up heap memory before you lose the last handle to that memory (using whatever mechanism you have at hand -- the delete operator, smart pointers or some kind of deferred deletion). This is basic C++ stuff and there are zillons or webpages mentioning "memory leaks" and instructions how to seal them, if in your prior programming education you have not encountered that term.
Up till now I was assuming you had programming skills, that's why I was telling you "fix the memory leak" and not "please delete the variable you are not using anymore and remember to always free up memory you are not using anymore". Apparently that was my mistake... I guess we have both learned something here.
Bookmarks