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

Clearing array of objects



  • Hi everyone,

    I'm having the following issue. I have made a GUI in Qt but it is crashing and I'm not sure of the reason. I've tried to debugged it to no success. My program creates a hash table of objects but then I try to clear it when I open another file. When I tried to debug it, the program was skipping this function and (I know it's going to sound weird, when I commented out the function call, the program seemed to try to execute the comment. This what I have. Please if this is wrong or can be improved, would you mind to post it ? Any help is really appreciated

    [code]

    void Directory::DeleteTable(){

        for(int i=0; i < TABLESIZE; i++)
        {
            if(table[i] != NULL)
            {
                delete table[i];
            }
        }
    }
    

    [/code]


  • Lifetime Qt Champion

    Hi
    What do you put inside table[i]; ?
    is that Widgets objects that you used in layouts and other places ?
    can you please show its definition. ?



  • @jefazo92 said in Clearing array of objects:

    When I tried to debug it, the program was skipping this function and (I know it's going to sound weird, when I commented out the function call, the program seemed to try to execute the comment.

    This is usually an indication (skipping lines, seeming to execute comments) that the debug information is not correctly in-sync with the source file(s). A common cause is changing source code but failing to recompile (or failing to notice recompilation failed). Make sure you are compiling for debug, delete old debug information, do a fresh, clean rebuild of everything in your project (not Qt itself, nor other libraries), and try again?





  • Hi @mrjj, this is the definition:

    Person* table[TABLESIZE];

    The person object stores 6 QString items for a given index of the array. So it stores it like this:

    table[index] = new Person(qtMember.getPhone(), qtMember.getFirstname(), qtMember.getSurname(), qtMember.getVehicle(), qtMember.getNation(), qtMember.getEmail());


  • Lifetime Qt Champion

    Hi
    One note. since you test for NULL, you should set it when you delete item.

    for(int i=0; i < TABLESIZE; i++)
        {
            if(table[i] != nullptr)
            {
                delete table[i];
                table[i] = nullptr;
            }
        }
    

    update:
    mr @aha_1980 pointed out that delete does a null check it self so you can just do

    for(int i=0; i < TABLESIZE; i++)
        {
                delete table[i];
                table[i] = nullptr; // this ONLY needed if you would ever loop the list 2 times.
        }
    


  • @jefazo92
    As @mrjj says. Else if you re-use the array you might try to re-delete a pointer you previously deleted. It also prevents against you executing this code twice (which you shouldn't do) on the same array values.

    If it's not that: make sure you know how your table[TABLESIZE] is initialized/scoped? If it's not set to all zeroes you will be trying to delete random memory contents (beyond the ones you actually use).


Log in to reply