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 Updated to NodeBB v4.3 + New Features

Clearing array of objects

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 548 Views 1 Watching
  • 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 30 Jul 2019, 14:32 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]

    M J 3 Replies Last reply 30 Jul 2019, 14:37
    0
    • J jefazo92
      30 Jul 2019, 14:32

      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]

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 30 Jul 2019, 14:37 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
        30 Jul 2019, 14:32

        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]

        J Offline
        J Offline
        JonB
        wrote on 30 Jul 2019, 14:40 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
        • V Offline
          V Offline
          VRonin
          wrote on 30 Jul 2019, 14:46 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 30 Jul 2019, 14:48 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
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 30 Jul 2019, 15:01 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
                30 Jul 2019, 14:32

                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]

                J Offline
                J Offline
                JonB
                wrote on 30 Jul 2019, 15:17 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

                1/7

                30 Jul 2019, 14:32

                • Login

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