Unsolved Bug QPainter crashes
-
@Sproza said in Bug QPainter crashes:
snake[i].x, snake[i].y
Have you checked the values of x,y when it dont draw?
Maybe its just outside widget area.Also, what is snake ?
It looks like one class but in paint you seems to use
as a array ??
snake.size();
snake[i].x
but you create it like
Snake* snake = new Snake(board);But in any case, its very hard to see the bug from code.
You should use the debugger and single step the code until you see what line it crashes
in or some value that is looking strange. -
@Sproza said in Bug QPainter crashes:
MainWindow::MainWindow(QWidget* parent) :
QMainWindow(parent)
{
Board* board = new Board(this);
Snake* snake = newYou are creating some local pointer variables like
Board* board = new Board(this);
Snake* snake = new Snake(board);Inside the paint event of snake you r trying to pass the board to Qpainter argument. At the same to board parent to snake object. Surely all this object relation ship must causing some issue. It is better to break the problem into smaller set & see if the issue exist.
I prefer you try just a Snake Class. Try to create object and see if this works. Then move the the board.
-
@dheerendra
Good catch. \o/ -
Snake is a class and snake is an member vector. In this case snake is also an instance. I made the whole code available at: https://github.com/Sproza/Snake. Board itself works fine. NOTE: drawLine() in paintEvent of both Snake and Board class was only for debugging purposes forgot to delete it sorry.
-
Board class alone works fine. Still no clue about Snake class though :/
-
On a side note, modifying your
pen
andbrush
objects after setting them onpainter
won't modify what painter draws. You should first configure them and once that done set them on painter. -
thanks @SGaist! it should be obvious, a silly mistake on my part. Any ideas about the problem though??
-
What's the size of your
snake
variable ? Are you sure the coordinates are valid ? -
Yes, coords are valid (250x and 250y whereas border is 500x500). Size of snake vector is 2.
Whole code is available at: https://github.com/Sproza/Snake.git
Just to make it clear because many find it confusing: Snake is a class with snake member vector and I named an instance of Snake "snake" in mainwindow constructor. -
Please? Anyone?
-
Can you provide a more complete code sample ? We can only do guesses here since there are a lot of unknowns from your side.
-
Hi
The code on git do not really compile out of the box.
Anyway, i saw thisfor(int i = snake.size(); i >= 0; i--)
if snake.size() is 0 , wont that crash in snake[0].x ?
as you say >= ? -
-
Yes, in that case it will crash, but snake.size() will never be 0 unless board -> STARTING_LENGTH will be 0. In Snake constructor program pushes additional dot to vector STARTING_LENGTH times.
But in reality there wont be way to change STARTING_LENGTH to 0 via menu.
So I do no think that's the reason. Just to be sure I tried snake.size() - 1 (although i have no idea why it would work) and it is not the solution. -
Ok, since you can run it then why not just find the crash with the debugger ?
When it crash the call stack will be shown and you can easy see where the crash happened.