Reputation: 340
I've started learning qt recently and was playing with it, also remembering a bit of c++ since I haven't used it in a while...
Anyway, I made this simple test class to see what would happen when you have a dangling pointer:
#include <QtCore/QCoreApplication>
#include <iostream>
class Test{
public:
QString str;
int i;
public:
Test(){
i=1337;
str="str";
std::cout<<"initialized\n";
}
~Test(){
std::cout<<"destructor called\n";
delete &str;
delete &i;
/*the program MAY run if the deletes aren't called;*/
}
};
Test* p;
void test_function(){
Test a;
p=&a; //this will give a segfault, since the destructor of a is called at the end of this function's scope, but p still points to a
//p=new Test(a); //this will call the copy constructor of p, copying the values from a to p
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
test_function();
std::cout<<p->str.toStdString() << p->i<<std::endl;
return a.exec();
}
which, sure enough, crashes (but it doesn't say why - probably segfault). However, if I comment the line where the dangling pointer is called (the cout in main()), the program now crashes giving me a "std::bad_alloc" exception. The following list of things tested independently causes the exception to not be thrown anymore:
What I tried and didn't have any effect was initializing str with QString::fromStdString("str") (instead of just "str").
I've also tried surrounding the initialization and destruction of str (while a QString) with a try/catch block, but to no avail. Also, try/catch for the test_function() - no result. I did however succeeded in catching the exception by surrounding the a.exec() in a try/catch block.
Why does it throw an exception when the cout is commented, but only crashes otherwise? Why does having both initializer and destructor for str throw the exception, but commenting one or both only results in a crash? (now that I'm writing this, I realise this may be the sole problem; however, why does it crash for QString but not for std::string?) Why does the a.exec() throw the exception?
Can someone who knows qt (or even c++) - better than I do - explain what's going on? Thank you
Upvotes: 2
Views: 1358
Reputation: 340208
I'm not exactly sure what kind of answer you're looking for, but even if you don't use the dangling pointer p
you'll have corrupted the heap (or some other undefined behavior) because in your dtor for Test
you have the following:
~Test(){
std::cout<<"destructor called\n";
delete &str;
delete &i;
/*the program MAY run if the deletes aren't called;*/
}
The two delete
statements are deleting things that aren't allocated via new
- don't do that.
Upvotes: 1
Reputation: 121971
This is one mistake:
delete &str;
delete &i;
trying to delete
variables that were not dynamically allocated, i.e. using new
. Remove those two lines. i
and str
will be destroyed automatically when the Test
instance is destructed.
Upvotes: 4