Serial problem , segmentation fault
-
Hi everyone!
I am new here , and i am working on my first Qt Project . I have some problem with my code. There is something with my write() function. When i start debugging , the compiler give an "Segmentation fault"msg. I can' imagine what can be the problem . I stared the code for hours , but still not found the answer... I know it is not a good method to copy all of my code, but i have no idea , where is the bug.Here is my Project : https://drive.google.com/folderview?id=0B7daYRiQ-gLoblVackg4WnFySjQ&usp=sharing
PLS , help me :)
-
Welcome to devnet
Did you familiarize yourself already with the debugger?
You should be able to get to the line step by step until your application is crashing. When you have located your problem, you can post mostly a few lines. That is probably a better way to get help.
-
Thanks for your reply .
I tried it several times ... And the lines , which are may cause the problem are from a tutorial. I have compiled the project of the tutorial , but it is ok , the tutorial's code works fine . And the same code is not working in my Project, but the logic is the same . So this is why i am here . -
Hi and welcome to devnet,
Then which line exactly does the debugger show ?
-
@domcsidd
Looks like the ardunio pointer has not been initialized.
In the right pane of the debugger window you have posted, you can click on the little triangle left of "this". You will see all variables of your instance listed. You will probably see also "ardunio" and the value "0x000".At least that is my expectation. Check it out.
Note. it depends where ardunio is declare and you may have to dig a bit further.
-
Thank you very much!
But i still didn't find the solution. The arduino pointer is declarated in the mainwindow.h header . Like this "QSerialPort *arduino;" . The arduino adress/value is "0x0". But all the other arduino func works... I would be happy , if you could offer an other way to write integer to the serial. -
@domcsidd
Sorry, at least you know that the arduino pointer is a null pointer. It is either never initialized (memory assigned) or it has been set unintensionally to zero.You need to check all assignments to arduino. With the debugger you can go step by step through your code and check where the problem may start.
If your code is still similar to what you have initially posted, you can step into the constructor of MainWindow there is the initial assignment of
arduino = new QSerialPort;
At this point there should be a value assigned. Best is to set a breakpoint before and after. Check whether the value is as expected.
-
@domcsidd said:
Hi everyone!
I am new here , and i am working on my first Qt Project . I have some problem with my code. There is something with my write() function. When i start debugging , the compiler give an "Segmentation fault"msg. I can' imagine what can be the problem . I stared the code for hours , but still not found the answer... I know it is not a good method to copy all of my code, but i have no idea , where is the bug.Here is my Project : https://drive.google.com/folderview?id=0B7daYRiQ-gLoblVackg4WnFySjQ&usp=sharing
PLS , help me :)
Hi! Often staring at the code for hours really doesn't help. You just see the same bug and keep thinking its just fine.
You give a hint that the arduino pointer address is 0x0. Thats not good. It should have a value so first question is:
Are you somewhere saying:
arduino = new QSerialPort(this);
If not then most likely that is your problem. The fact that some routines do work with a 0x0 pointer is just luck and alignment. Also some routines might be static and will work ok.
I have several pieces of advice:
-
Try to post more example code here. Try to limit what you post to what you know actually runs BEFORE the error. No one here can page through a lot of code to help you find your bug. So you need to show us "it runs this module just fine..." "then it calls this routine..." "next this one" and people will take a look and see if they can help.
-
Usually problems like this are related either to unallocated pointers or pointers that get tromped on by some other code. It is not 100% clear from your screen pict if the arduino pointer is the problem or not. But since the debugger was on that line I think it could be likely. So... put in break points throughout your program and follow arduino around. When you first create arduino make a note of the hex address in the debugger. Then follow it through your various places and see if it changes at all.
Last piece of device is that few programmers in this world can debug a 10,000 or more line program at once. We all need to chunk things into smaller pieces. Eliminate running code and focus on the problem area. Sometime to fine the problem area is more than 1/2 of the battle.
I use qDebug() a lot for this purpose. If I'm getting a crash I run around and deposit some qDebug() messages with unique output something simple like "A" "B" "B1" "B2" etc. Then if the program crashes and the output is: "A" "B" "B1" then I know to start looking in the area after B1 but before B2.
This does not guarantee the problem did not occur in B or even A but at least you can focus on a smaller portion of your code at first and work backward.
-