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

Stack Overflow error instantiating new objects



  • Hello everyone,

    This post derives directly from my previous one. I thought I could make another one since this is a different problem and could help others in the future without having to read through the previous one.

    What I am trying to achieve:
    Inside the same panel I have on the left a qListWidget with different items and on the right a StackedWidget object. I would like to be able to select the items on the list and (based on which item has been selected) show the corresponding widget in the StackedWidget.

    What I have done so far:
    Until now I have created the qListWidget also adding the various items to it. I also created a WidgetClass per widget that I want to add to the StackedWidget. Now, thanks to the other users' tips I created a SIGNAL and SLOT system that should do the magic.

    The code: (ConfigurationDialog.cpp)

    ConfigurationDialog::ConfigurationDialog(QWidget* parent)
    : QDialog(parent)
    {
    ui.setupUi(this);
    setWindowFlags(Qt::WindowStaysOnTopHint);

    ui.listWidget->addItem("Text1");
    ui.listWidget->addItem("Text2");
    ui.listWidget->addItem("Text3");
    ui.listWidget->addItem("Text4");
    ui.listWidget->addItem("Text5");

    item1Configuration* item1 = new item1Configuration;
    item2Configuration* item2 = new item2Configuration;
    item3Configuration* item3 = new item3Configuration;
    item4Configuration* item4 = new item4Configuration;
    item5Configuration* item5 = new item5Configuration;

    ui.stackedWidget->addWidget(item1);
    ui.stackedWidget->addWidget(item2);
    ui.stackedWidget->addWidget(item3);
    ui.stackedWidget->addWidget(item4);
    ui.stackedWidget->addWidget(item5);

    connect(ui.listWidget, SIGNAL(itemClicked(QListWidgetItem*)),
    this, SLOT(onListItemClicked(QListWidgetItem*)));
    }

    ConfigurationDialog::~ConfigurationDialog()
    {
    }

    void ConfigurationDialog::onListItemClicked(QListWidgetItem* item)
    {
    ui.stackedWidget->setCurrentIndex(ui.listWidget->currentRow());
    }

    Now I think the logic is correct. The problem is that the application does not open anymore when I compile. Instead after a little bit of waiting a window opens up with the following error:

    Unhandled exception at 0x00007FFD5793B866 (ntdll.dll) in Application.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x000000190E003FF8).

    I believe this is because of the instantiation of the objects. But why is it so? How should I fix this?
    Thank you in advance.


  • Lifetime Qt Champion

    Use a debugger and see where exactly it crashes.



  • @Christian-Ehrlicher The debugger points me to this:

    _CRT_SECURITYCRITICAL_ATTRIBUTE
    void* __CRTDECL operator new(size_t const size)
    {
    for (;;)
    {
    if (void* const block = malloc(size)) //THIS LINE
    {
    return block;
    }

        if (_callnewh(size) == 0)
        {
            if (size == SIZE_MAX)
            {
                __scrt_throw_std_bad_array_new_length();
            }
            else
            {
                __scrt_throw_std_bad_alloc();
            }
        }
    
        // The new handler was successful; try to allocate again...
    }
    

    }


  • Lifetime Qt Champion

    You should also take a look at the backtrace so you can see from where this was called.



  • @Christian-Ehrlicher Is "backtrace" a debugging function of VisualStudio?



  • It looks like the problem comes from the instantiation of the various objects:

    item1Configuration* item1 = new item1Configuration;
    item2Configuration* item2 = new item2Configuration;
    item3Configuration* item3 = new item3Configuration;
    item4Configuration* item4 = new item4Configuration;
    item5Configuration* item5 = new item5Configuration;

    When, for example, item2 is created after item1 the application freezes.


  • Lifetime Qt Champion

    Hi

    can you show the constructor for
    item2Configuration ?

    and also its .h

    Maybe you have some HUGE array inside or something similar as the rest of the code seems fine.



  • @mrjj I found my mistake: I had a line instantiating the object inside its constructor. I deleted that and now it all works, thank you guys!


  • Lifetime Qt Champion

    @TommyTLC
    Super :)


Log in to reply