Thread: EAccess Violation Error using Vectors

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    11

    EAccess Violation Error using Vectors

    Hi,

    Im working on a GUI project using Borland Builder 5 and I have a container class that is of type vector which has to hold objects of another class.

    The objects to be stored are of Class Actor and ActorContainer is the class that should hold them.

    When I add an Actor to the ActorContainer I get an EAccess Violation error thats highlights the "return size;" in my getSize() function within my Container class. As I am learning to program in GUI and am using a listbox to show all the actors in my container, I am not sure whether i am going wrong in my GUI or problem domain side of things.

    Can someone suggest anything please that would make this work or an alternative way of doing it.

    cheers

    Code:
    //this is the addActor button function in My GUI class
    void __fastcall TForm2::AddActorClick(TObject *Sender)
    {
        Actor* newActor = new Actor;
    
        AnsiString nameofActor = InputBox("Actor Name", "Please enter name for the Actor", "");
    
        newActor->setActorName(nameofActor);
    
        newActor->setPosition (theActor->getSize()+1);
    
    //theActor is an object of ActorContainer to be used locally within my GUI class
    
        theActor->addActor(newActor);
    
        actors = new TStringList;
    
        for (int i=0;i<theActor->getSize();i++)
        {
            AnsiString name;
            name=theActor->getActor(i)->getPosition();
            name=theActor->getActor(i)->getActorName();
            actors->Add(name);
    
        }
        ListBox2->Items=actors;
    
    }
    
    ------------------------------------------------------------------------
    //This is the Actor Class
    
    class Actor{
    private:
        AnsiString actorName;
        int position;
    
    public:
        void setActorName(AnsiString s) {actorName=s;};
        void setPosition(int p) {position=p;};
        AnsiString getActorName() {return actorName;};
        int getPosition() {return position;};
    };
    
    
    ------------------------------------------------------------------------
    //The ActorContainer Class
    
    class ActorContainer{
    private:
        vector<Actor*>actorsNames;
        int size;
    
    public:
        ActorContainer(){size=0;};
        
    ~ActorContainer();
    
        void addActor(Actor* a)
        {
            actorsNames.push_back(a);
            size++;
        };
    
    
        Actor* getActor(int i)
        {
            return actorsNames.at(i);
        };
    
        int getSize() {return size;};//this is where the Eaccess Violation points to
      
    
        void removeActor(int i)
        {
            actorsNames.erase(actorsNames.begin()+i);
           //does the list.erase function delete the object as well?
    
           size--;
    
           for(int i=0 ; i < size ; i++)
           {
                Actor* temp=actorsNames.at(i);
                temp->setPosition(i+1);
           }
        };
    
    };
    p.s does the .erase() function for vectors delete objects from memory.
    I use BorlandBuilder 5

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    Why not return actorsNames.size() instead of using your own variable?

    Secondly, .erase does NOT delete objects from memory, as the vector, being a template, doesn't know your passing it pointers to objects, so you'll need to do this yourself after each .erase.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    It's not a good idea to fill containers with pointers; it places the burden to manually delete the pointed at object. Download Boost and use boost::shared_ptr to hold your object pointers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM