Thread: Help with deleting array of pointers

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    41

    Help with deleting array of pointers

    Hi All,

    I've got a class that declares an array of pointers like this:

    Code:
    class RemoraServer : public CommonApplication
    {
    
    ................
    
          //these are the hosts this server maintains data for 
          RemoraHost  *host[MAX_HOSTS_PER_SERVER];
          int         numberOfHosts; //ongoing count of how many hosts we're dealing with right now
    
    }; // end class RemoraServer

    later in the code, it sets an index of that array to point to an object, thereby instantiating the object, like this:

    Code:
                host[numberOfHosts] = new RemoraHost(hostName, hostRef, configFile);
    
                //we just gained a new host!
                numberOfHosts++;
    I'm convinced the instantiation works, since I can interact with the newly created objects.

    Now I want to delete all the objects and rebuild them, so I was trying this, but it doesn't work:

    Code:
       
       //first destroy all of our current hosts, so we can re-initialize them all
       for (int i = 0; i < numberOfHosts; i++)
       {
          delete host[i];
       } //end for
    All of my classes use virtual destructors.
    My program SIGSEGV when it tries to delete the host[i], however (according to gdb) it stays within RemoraServer, so I don't think it's a problem with my RemoraHost code.

    What am I doing wrong? (more than 1 answer is allowed. ). Thanks for any pointers (pun intended).

  2. #2
    root
    Join Date
    Sep 2003
    Posts
    232
    >My program SIGSEGV when it tries to delete the host[i]
    The value of i at the time? Also the value of numberOfHosts and MAX_HOSTS_PER_SERVER would be useful. Also, can you come up with something we can compile and play with that still exhibits the problem? That way you'll get more than just guesses.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    161
    Just one guess for now. Did you remember to set numberOfHosts back to zero after you delete all the objects?
    Last edited by thefroggy; 10-09-2003 at 11:07 AM.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    41
    'i' is 0 when the SIGSEGV occurs.
    MAX_HOSTS_PER_SERVER is 50
    numberOfHosts == 1 at that point. There is 1 host, so that number is correct.
    The code is too much to post, since RemoraHost has an array of pointers of other objects, which have arrays of pointers of other objects, etc...

    I set numberOfHosts back to zero when I'm done deleting them, but it never finishes. The very first
    delete host[0];
    causes the SIGSEGV.
    Last edited by registering; 10-09-2003 at 11:44 AM.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    41

    Talking

    Oops, my bad. I was doing
    delete [] host;
    in the actual executable. I had changed it to
    delete host[i];
    and remade it but never loaded the new executable, so I was executing old code when I thouht I was executing new code. So far, testing the actual code I posted (duh), seems to work fine.

    Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 08:21 AM
  2. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 07:13 AM