[solved] memory deallocation and the delete keyword
-
And just for future read "this":http://doc.qt.nokia.com/latest/qscopedpointer.html and "this":http://doc.qt.nokia.com/latest/qsharedpointer.html. This will help you not to worry about manually deletion.
-
[quote author="rokemoon" date="1313089726"]And just for future read "this":http://doc.qt.nokia.com/latest/qscopedpointer.html and "this":http://doc.qt.nokia.com/latest/qsharedpointer.html. This will help you not to worry about manually deletion.[/quote]
Caveat: Smart pointers are great tools to help manage the lifespan of objects, but they still require a base knowledge of what pointers are all about at their lowest level. It's important to make sure that you have a grasp of the fundamentals before using the higher-level counterparts, though. That's just IMHO, though.
-
[quote author="mlong" date="1313090165"]
Caveat: Smart pointers are great tools to help manage the lifespan of objects, but they still require a base knowledge of what pointers are all about at their lowest level. It's important to make sure that you have a grasp of the fundamentals before using the higher-level counterparts, though. That's just IMHO, though.
[/quote]I cannot but second this!
So we have the outline for next year's "DevDays T-Shirts":http://developer.qt.nokia.com/forums/viewreply/50309/ :) :) :) -
[quote author="Lukas Geyer" date="1313089715"]
No, you never ever call delete on the object which is currently destructed.[/quote]if i have this
@class a {
private:
BClass* pointer;
};@then it is correct to do the following,right?
@~a::a(){
delete pointer;
}@the follow is true or false?
@delete pointer;
pointer=0;if (pointer) {
....}@ -
[quote author="Giorgos Tsiapaliwkas" date="1313091362"]
if i have this
@class a {
private:
BClass* pointer;
};@then it is correct to do the following,right?
@~a::a(){
delete pointer;
}@the follow is true or false?
@delete pointer;
pointer=0;if (pointer) {
....}@[/quote]
@~a::a(){
delete pointer;
}@should be this
@a::~a(){ // the ~ is not the class name it's parte of the function name
delete pointer;
}@the rest is ok.
EDIT:
Excurse: it's always- returntype classname::functionname(parameters)
and constructors and destructors have no return type :-)
-
Indeed.
I think the point that Lukas was making here:
bq. No, you never ever call delete on the object which is currently destructed.
Was that you'd never have the situation of:
@
a::~a() {
// BAD CODE:
delete this; // You would never delete an item from within its own destructor.
}
@(At least that's what I took it to mean.)
-
[quote author="Giorgos Tsiapaliwkas" date="1313091362"]
the follow is true or false?@delete pointer;
pointer=0;if (pointer) {
....}@[/quote]
Everything inside the if will not be executed (0 is false, everything else is true - just like with ints) -
thank you for your answers.
i will mark this thread as solved
-
[quote author="mlong" date="1313090165"]
Caveat: Smart pointers are great tools to help manage the lifespan of objects, but they still require a base knowledge of what pointers are all about at their lowest level. It's important to make sure that you have a grasp of the fundamentals before using the higher-level counterparts, though. That's just IMHO, though.
[/quote]
I completely agree with you mlong. I was a bit hasty with this. -
[quote author="Giorgos Tsiapaliwkas" date="1313091362"]
[quote author="Lukas Geyer" date="1313089715"]
No, you never ever call delete on the object which is currently destructed.[/quote]if i have this ...[/quote]
You are of course allowed to create and delete objects within a destructor - but you should never delete yourself (delete this). See mlong's post.