Crash-Segmentation fault
-
If you create a new instance of NamePage, you are changing the new instance, not the one inside MainWindow. You have to pass the pointer to the existing NamePage to ProjectPage or use signal/slot. Otherwise the two pages do not communicate .
-
Additionally, you do not pass any pointer of a ProjectPage to the NamePage. The ProjectPage *project pointer is uninitialized, accessing it will always cause a crash!
-
Not always cause a crash Volker, the behavior is undefined, or how we say here: you could create an earthquake :-) If you are lucky, it crashes, otherwise no one knows what happens....
-
You're very lucky it crashed. No, really.
We've had an incident some time ago: one of our products (installed in about 1000 offices all over the country) started to get mad and do a strange things after some months of stable working. The problem was in uninitialized pointer of course. But it really worked fine for a while and passed through QA department, some automated stress-test and a months in a production before we had this bug in a one -happy- day.
-
That's why I said, you are happy if it crashes, because you find it...
If it doesn't crash it gets complicated, like you described... -
Thanks for all the help! I am pretty new at C++ and Qt programming and I am reading and trying all I can to better understand. I am still a bit confused... Gerolf, you stated that if I create a new instance of NamePage, I am changing the new instance. I don't quite understand. I created a new instance for a new class. I guess this is not the correct way? And then you say that I need to pass the pointer to the existing NamePage to ProjectPage. I am not sure how to do that. Could you give me an example of how to do that?
Volker, You stated that I did not pass a pointer of ProjectPage to the NamePage and that the ProjectPage *project pointer is uninitialized. I thought that by delaring a ProjectPage class in the header of NamePage and a pointer "ProjectPage *project" in the NamePage header took care of this.
I am going to rewrite this to use signals and slots, I kinda understand how to do this between classes, so I will give it a try.
But my question now is (and this is just for knowlege in case I run accross a similiar situation) How do you go about passing pointers and would it work if I instantiate a pointer to each class in MainWindow and pass the pointer around? And what is the method to do that.
Again, Thanks for your advice and I will post back after I change the code for signals and slots and see if I can get it to work! -
Hi poporacer,
passing the pointer would solve your problem (which is a C++, not a Qt problem) or using signals / slots.
What I meant with
bq. you stated that if I create a new instance of NamePage, I am changing the new instance.
If you have
@
1 class A
2 {
3 A()
4 {
5 m_p2 = new B;
m_bNotified = false;
6 }
7 foo1()
8 {
9 m_p2->foo();
10 }
11 notify(){m_bNotified = true};
11a notified(){return m_bNotified};
11b B* m_p2;
12 }
13
14 class B
15 {
16 foo()
17 {
18 m_p3 = new A;
19 m_p3->notify();
20 }
20a A* m_p3;
21 }
22
23 main(...)
24 {
25 A* m_p1 = new A;
26 m_p1->foo1();
27 m_p1->notified(); // will return false !!!
28 }
@If you call on line 26 the instance m_p1->foo, on line 18 you create a new instance of A and the notify (line 19)will not arrive at object m_p1 but m_p3 as you have 2 instances. You do the same in your code. m_p1 != m_p3 so if you go back to main and call notified on m_p1, it will return false, as m_p3 is notified, not m_p1.
[EDIT: fixed blockquote tag, Volker]
[EDIT: added Variable types, Gerolf] -
Gerolf, m_p1 is of type, there is no foo in that class...?
-
Ok, ok, I let the types out:-) I will add them in the original post
And it should be foo1(), it was pseudo code, not tested in compiler. It would have found it... -
Regarding your pointer problems, see "my answer":http://developer.qt.nokia.com/forums/viewreply/16349/ in your signals/slots thread.