EAccess Violation Error using Vectors

This is a discussion on EAccess Violation Error using Vectors within the C++ Programming forums, part of the General Programming Boards category; Hi, Im working on a GUI project using Borland Builder 5 and I have a container class that is of ...

  1. #1
    Registered User
    Join Date
    Jan 2002

    EAccess Violation Error using Vectors


    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.


    //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->setPosition (theActor->getSize()+1);
    //theActor is an object of ActorContainer to be used locally within my GUI class
        actors = new TStringList;
        for (int i=0;i<theActor->getSize();i++)
            AnsiString name;
    //This is the Actor Class
    class Actor{
        AnsiString actorName;
        int position;
        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{
        int size;
        void addActor(Actor* a)
        Actor* getActor(int i)
            return actorsNames.at(i);
        int getSize() {return size;};//this is where the Eaccess Violation points to
        void removeActor(int i)
           //does the list.erase function delete the object as well?
           for(int i=0 ; i < size ; i++)
                Actor* temp=actorsNames.at(i);
    p.s does the .erase() function for vectors delete objects from memory.
    I use BorlandBuilder 5

  2. #2
    Registered User
    Join Date
    Jun 2003
    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
    Cat is offline
    Registered User
    Join Date
    May 2003
    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, 09: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, 03: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, 11:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 12:13 PM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21