Thread: Why am I getting a SEGV?

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    52

    Why am I getting a SEGV?

    Hi everyone,

    I'm trying to erase an array of pointers, but I get a segmentation fault.. any ideas what is happening :\

    Code:
      vector<employee*> arrayEmployees; // PostfixArray of functors that will be processed.
    
    	for (uint32_t k = 0; k < repeat; ++k) {
    		while (in) { 
                     ...
                    }
            ...
    	}
    
    	for (vector<employee*>::iterator i = arrayEmployees.begin(); i != arrayEmployees.end(); ++i) {
    		delete *i;  
    	}
    	return 0;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That depends entirely on what the pointers are that you are trying to free. Did they all come from new?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Let's see the hidden code. Better yet, post the smallest and simplest compilable code that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    52
    Oh.. I am using new to create the object and then push them in an array. However I do this in a sepparate function, could that be a problem.

    like this:
    Code:
    	Employee employee = NULL;
    	for (uint32_t i = 0; i < MAX; ++i) {
    			employee = new employee(false, post[i]);
    			arrayEmployees.push_back(employee);
    	}

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by afflictedd2 View Post
    Oh.. I am using new to create the object and then push them in an array. However I do this in a sepparate function, could that be a problem.

    like this:
    Code:
    	Employee employee = NULL;
    	for (uint32_t i = 0; i < MAX; ++i) {
    			employee = new employee(false, post[i]);
    			arrayEmployees.push_back(employee);
    	}
    Well, I don't see how that compiles. You can try again, with real code, but better still would be to follow Laserlight's suggestion and create a small, compilable, self-contained example that demonstrates your difficulty.

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    52
    I can't post the code for the app though. Where I am working they don't want any code exposed, I'm not sure how much is too much but I don't like to risk it. Most of the time I do if it's an example of my own code, though. Well anyway, I figured out the problem.

    I basically followed this example:
    Code:
    #include <vector>
    using namespace std;
    
    class Object
    {
    };
    
    int main()
    {
        vector<Object*> vpObject;
        Object* pObject;
    
        for (int i=0; i<5; i++) 
        {
            pObject= new Object;
            vpObject.push_back(pObject);
        }
    
        // delete all pointers in the vector
        while (!vpObject.empty())
        {
            // get first 'element'
            pObject = vpObject.front();
            
            // remove it from the list
            vpObject.erase(vpObject.begin());
    
            // delete the pointer
            delete pObject;
        }
    
    return 0;
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by afflictedd2
    I basically followed this example:
    And you couldn't do it this way?
    Code:
    #include <vector>
    
    class Object
    {
    };
    
    int main()
    {
        using namespace std;
    
        vector<Object*> vpObject;
    
        for (int i=0; i<5; i++)
        {
            vpObject.push_back(new Object);
        }
    
        // delete all pointers in the vector
        for (vector<Object*>::iterator iter = vpObject.begin(), end = vpObject.end();
            iter != end; ++iter)
        {
            delete *iter;
        }
    
        return 0;
    }
    Note that the code is not exception safe: if std::bad_alloc is thrown the objects already dynamically created would never be destroyed. On the other hand, the OS would probably do clean up, but this is just a toy program so we can accept that. Also, if you really want destruction to occur in reverse order of construction then we should write:
    Code:
        // delete all pointers in the vector
        for (vector<Object*>::reverse_iterator iter = vpObject.rbegin(), end = vpObject.rend();
            iter != end; ++iter)
        {
            delete *iter;
        }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-28-2008, 03:07 AM
  2. glade_xml_get_widget => SEGV
    By rabby in forum C++ Programming
    Replies: 0
    Last Post: 01-06-2008, 03:23 PM