Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Clearing array of objects
Forum Update on Monday, May 27th 2025

Clearing array of objects

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 524 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jefazo92
    wrote on last edited by
    #1

    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]

    mrjjM JonBJ 3 Replies Last reply
    0
    • J jefazo92

      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]

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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. ?

      1 Reply Last reply
      0
      • J jefazo92

        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]

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @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?

        1 Reply Last reply
        2
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          https://doc.qt.io/qt-5/qtalgorithms.html#qDeleteAll-1

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          1
          • J Offline
            J Offline
            jefazo92
            wrote on last edited by
            #5

            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());

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              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.
                  }
              
              1 Reply Last reply
              4
              • J jefazo92

                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]

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @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).

                1 Reply Last reply
                2

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved