Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QGriddlayout works for grid 80*80 but not for 100*100 or more (SIGSEGV error)



  • Sorry in advance I am very new to QT and c++. I am trying to make a grid filled with colored clickable labels. And the whole program works perfectly for grids the size smaller than 80*80 but for bigger gridds I get a SIGSEGV segmentation fault error in the debugger, it doesn't exactly tell my why. This is the code where the error most likely originates:

    void Gridd::ShowStart(QWidget *wid){
    
        QGridLayout *layout = new QGridLayout;
    
        for (unsigned int r = 0; r < rows; r++) { 
            for (unsigned int c = 0; c < cols; c++) {
    
                ClickableLabel  *label = new ClickableLabel(r,c); 
                QString str = " background-color : " ;
    
                if ((*this)(r,c).getState() == "fireless"){ 
    
                    str += (*this)(r,c).getMaterial().getColor();
                }
                else {
                    int bt = (*this)(r,c).getBurningTime();
                    QColor col (250-20*bt,(80+bt * 160)/(1+bt*bt),0);
                }
                label->setStyleSheet(str);
                layout->addWidget(label, r, c);
                str="";
    
                (*this)(r,c).setLabel(label);
    
            }
        }
        layout->setHorizontalSpacing(0);
        layout->setVerticalSpacing(0);
      
        wid->setLayout(layout);
        wid->show();
    }
    

    I think it originates from here because it does not want to show my start grid here. Thanks in advace!


  • Lifetime Qt Champion

    @Davay said in QGriddlayout works for grid 80*80 but not for 100*100 or more (SIGSEGV error):

    int bt = (*this)(r,c).getBurningTime();

    wtf?
    What is 'this' dervied from. This looks very strange.

    Also when it crashes - use a debugger to see where it crashes and fix it.



  • @Christian-Ehrlicher said in QGriddlayout works for grid 80*80 but not for 100*100 or more (SIGSEGV error):

    ied from. This looks very strange.
    Also when it crashes - use a debug

    I'm sorry it looks so strange but I'll try to answer this is a matrix (a std vector of a std vector of a class section and the section on that row and column gets given back by (*this)(r,c), I overloaded the () operator) which has an attribute named burning time.)

    I want to use the debugger but it does not give me more info on exactly where it crashes, It seeminly crashes on this function in my main:

    void delay(int secs)
    {
        QTime dieTime= QTime::currentTime().addSecs(secs);
        while (QTime::currentTime() < dieTime)
            QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
    }
    

    But if I leave it out it crashes as well so I know for sure it isn't this.


  • Lifetime Qt Champion

    This function should be removed and the logic be fixed. Don't do this. If you want a 500ms timer then use QTimer but don't call the eventloop this way. It will just lead to problems.



  • @Christian-Ehrlicher said in QGriddlayout works for grid 80*80 but not for 100*100 or more (SIGSEGV error):

    This function should be removed and the logic be fixed. Don't do this. If you want a 500ms timer then use QTimer but don't call the eventloop this way. It will just lead to problems.

    Thanks a lot for your answer! Can you elaborate why it would lead to problems and why to problems only occur for big enough grids?


  • Lifetime Qt Champion

    @Davay said in QGriddlayout works for grid 80*80 but not for 100*100 or more (SIGSEGV error):

    Can you elaborate why it would lead to problems

    You call the eventloop but it's not run in the 'outer' loop. So basically you have

    outer main eventloop -> your program logic -> inner event loop

    but this can lead to problems, especially when your program logic was called as a result of a queued signal/slot connection. When you then call the eventloop inside this slot, the signal may be evaluated again since it's not yet finished. Also the order of the signals can be mixed up due to this and other stuff which is very hard to debug. Therefore avoid it, especially when you try to delay something. Qt is event driven and you should not do such things.

    and why to problems only occur for big enough grids?

    timing or others, no idea but what you're doing is not recommended.



  • @Davay confirm that as you increase the grid from 80x80 to 100x100, the following index access from the matrix is valid?

    (*this)(r,c).getState()
    (*this)(r,c).getMaterial()
    (*this)(r,c).getBurningTime()

    My guess is that matrix is being initialized to 80x80



  • @nagesh Thank you for your comment, all of them seem to be initialized. I also checked for hard coding errors several times but don't seem to find any of that sort sadly.



  • @Christian-Ehrlicher Thank you for the elaboration! I've followed your advice and it did help me with some laggy issues of my program but sadly not with the grid. I've changed my delay function in:

    QTimer ts;
    ts.start(200);
    QEventLoop loop;
    QObject::connect(&ts, SIGNAL(timeout()), &loop, SLOT(quit()));
    loop.exec();
    


  • @Davay said in QGriddlayout works for grid 80*80 but not for 100*100 or more (SIGSEGV error):

    I get a SIGSEGV segmentation fault error in the debugger, it doesn't exactly tell my why.

    I don't see anywhere you have looked at/shown the stack trace for this?



  • @JonB Thank you for your reply, I hope this picture helps but I wasn't really sure what you've asked for (since I'm really unfamiliar with qt (and c++). 69476755-cc96-4a7d-9c9e-d1913c47b835-image.png The first picture is just before I get the Sigsev error and this second one is just after: 7301dcb5-adb9-43b2-a423-731a18eaeb8f-image.png



  • @Davay
    It probably won't help, but you would need to go back further in the stack trace (bottom window) till you reach somewhere in your own code.



  • The debug output looks like you have infinite recursion. With every function call you allocate more stack space. Eventually you will run out of stack space (as it is quite limited compared to the heap) which will in turn result in a segmentation fault.

    Like @JonB already said, you need to provide more of the stack trace until you reach your own code. Click on <More> in the stack trace until you see something familiar. You should also figure out if Qt uses more than 1 Thread. If I am not mistaken GUI stuff runs in Thread #0 and not #1 like you show it. (I might be mistaken about this, though.) Sometimes switching the thread will show your own code in the stack trace.



  • @JonB & @SimonSchroeder Thanks a lot for your answers let me start of with the threads, I'll only show those where something new shows up: 06534ce5-7576-49b6-bc54-a62e4c57fa3f-image.png
    427b1a47-5eb9-490f-90af-354e0ab5bd49-image.png
    c9170c0b-c429-41c0-9605-9a7e529e4d1a-image.png
    883871e2-edc3-47aa-87cc-1df9514a894e-image.png
    2aa92dff-1bc3-46f7-8c26-5c96137a1e0e-image.png

    92eed9ec-79ae-4fca-bca5-30962454ed84-image.png
    986ca12d-e65d-456c-bd07-a374c4dc3e56-image.png
    35300ee1-ae56-42e0-a5e5-d404d368634b-image.png
    370bffc2-1475-4f04-a3cb-369d9df1632c-image.png
    Now if I click on more in thread 1 (see the previous reply) it doesn't do anything now if I click on it a couple of times, QT crashes without error or any warning.

    @SimonSchroeder do you mean with this limited amount of stack space that I can't make more of my clickable labels than lets say than a grid filled with them more than (80 on 80) so about 6400?



  • @Davay

    It probably wont help to solve your problem, but why do you need 10.000 labels or more in a QGridLayout at the same time? Maybe there is a workaround or better/different solution for your use case.
    -> QGraphicsView+ QGraphicsItems instead of labels in a grid.



  • Well, you are probably right that I don't need them but for a beginner like me it seemed a logical choice to use labels or buttons since those are things I know. I'm trying to make a forest fire simulation and each tile can be made of a different material that can be changed throughout the simulation. Hence they need to be clickable. I would be open to any suggestions on how to do this better of course!


  • Lifetime Qt Champion

    Hi,

    As suggested by @Pl45m4 the Graphics View Framework. You'll be able to build your simulation in an easier way to manage your various tiles on the map.



  • @Davay Is it possible to create a single image for display of your simulation? You can change the colors of its pixels. Your simulation data is generated behind the scene. One tile consists of a matrix of pixels.



  • @Davay

    So your "map" looks like raster graphics with a low resolution where each label in some color represents a tile of a different material?
    Yes, you can do this with the GraphicsView framework.
    Of course they can be clickable. But there is so much more you can do, when you use the GraphicsView framework (zooming, custom items etc).
    It probably takes some time to re-design your interface but 6.400 or over 10k labels on a widget is by far not the best solution :)



  • Thank all of you for your good suggestions, I'll look into them!



  • @Davay one more observations..How many threads do your application has?
    in stack trace it's shown upto 21.



  • @nagesh I do not know, I don't tell something about the threads in my application so it happens automatically I think.


Log in to reply