Thread: Access Violation?

  1. #1
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96

    Question Access Violation?

    I am writing a program to help me out at work. I run the dispatch board at a local trucking company and the program is designed to help me keep track of pick up requests that are called in by customers. I am getting an error that I cannot track down. When I run the program (at least, what I have so far) Windows tells me that the program encountered a problem and needs to shut it down. After quite a bit of debugging I found this window that poped up "An access violation (Segmentation Fault) raised in your program". This is a fairly large program with multiple modules so I will just post the relevant parts.
    I have a struct to record pick up requests
    Code:
    struct PickUp_Info
    {
        string name;        // customer name
        string address1;    //          address
        string address2;    //          address
        string city;        //          city
        string state;       //          state
        string ozip;        //          zip
        string route;       // route number
        string drivername;  // assigned driver name         
        int drivernumber;   //                 number
        int punum;          // pick up number
        int pcs[10];        // (up to 10 bills) pieces
        int weight[10];     //                  weight
        string dzip[10];    //                  destination zip code
        string pro[10];     //                  tracking number
        string enter_time;  // time pick up was entered
        string ready_time;  // time pick up is ready
        string close_time;  // time customer closes
        string assign_time; // time assigned to driver
        string confirm_time;// time driver made pick up
        string contact;     // company contact name
        string phone;       // company contact number
        string comments;    // comments on pickup, special directions
    };
    and this class to manage all of the pick up requests
    Code:
    class PickUp_Manager
    {
        public :
            PickUp_Manager() { };
            ~PickUp_Manager() { };
            void Print_Report();
            void Clear_Plan();
            int Get_Number();
            void Get(int x);
            vector<PickUp_Info>::iterator Check_PickUp(int x);
            void Clear_Fields();
            void Save(bool save);
            
            vector<int> reference_list;
            
            PickUp_Info pickup;
            int total_pcs, total_weight, total_calls;
            int line_num, confirm_count, pu_num;
            
        private :
            void PickUp_Manager::Sort_PickUp();
            
            vector<PickUp_Info> pickup_list;
    };
    The program displays a screen with blanks for all of the information for the pick up request and the user can tab through the blanks and enter all of the information. Then ReadConsoleOutputCharacter() is used to record all the information into the struct using this function
    Code:
    string ReadCursor(int x, int y, int z)
    {
        char tempchar[z];
        string tempstring;
        COORD Position;
        DWORD NumRead;
        HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
        Position.X = x;
        Position.Y = y;
          ReadConsoleOutputCharacter(hOut, tempchar, z, Position, &NumRead);
        tempstring.assign(tempchar, 0, z);
        return tempstring;
    }
    And here is the part of the program that I am having problems with
    Code:
    void Save_PickUp(PickUp_Manager& PickUp, bool save)
    {
        string temp;
        PickUp.pickup.name      = ReadCursor(26,  3, 25);
        PickUp.pickup.address1  = ReadCursor(26,  5, 25);
        PickUp.pickup.address2  = ReadCursor(26,  7, 25);
        PickUp.pickup.city      = ReadCursor(26,  9, 15);
        PickUp.pickup.state     = ReadCursor(26, 11,  2);
        PickUp.pickup.ozip      = ReadCursor(26, 13,  5);
        PickUp.pickup.route     = ReadCursor(26, 15,  2);
        PickUp.pickup.contact   = ReadCursor(26, 17, 25);
        PickUp.pickup.phone     = ReadCursor(26, 19, 12);
        for (int i = 0; i <= 9; i++)
        {
            temp = ReadCursor( ( (i * 8) + 8), 23, 3);
              if (temp != "   ")
                  PickUp.pickup.pcs[i] = atoi(temp.c_str() );
              else PickUp.pickup.pcs[i] = 0;
            temp = ReadCursor( ( (i * 8) + 8), 25, 5);
              if (temp != "     ")
                  PickUp.pickup.weight[i] = atoi(temp.c_str() );
              else PickUp.pickup.weight[i] = 0;
            PickUp.pickup.dzip[i] = ReadCursor( ( (i * 8) + 8), 27, 5);
        }
        PickUp.pickup.comments   = ReadCursor(10, 29, 80);
        PickUp.pickup.ready_time = ReadCursor(13, 31,  4);
        PickUp.pickup.close_time = ReadCursor(41, 31,  4);
        PickUp.pickup.enter_time = ReadCursor(70, 31,  4);
        
        PickUp.Save(save);
        return;
    }
    After the for loop calls the ReadCursor function for the third time in the first cycle the error comes up as soon as the function returns to the caller. I thought that it might be trying to access bad memory but I don't see it. I am at a loss here. I hope I explained this well enough for everyone to understand, any help will be greatly appreciated.
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Hmmm... I don't know, man.

    Are you sure it's returning? The only thing I would think of is you might, for some reason, start going out of bounds in your screen's buffer with 27 are your Y coordinate. Nothing else seems like it would cause an error. You're assigning a string to a string. You're not going out of your array bounds or anything...

    Anyway, I'd also say that you should be using stringstreams instead of atoi(), because that has a bad habit of undefined behavior in the result of it failing.

    Another thing that sparks though, and I really have no reason to consider this other than a hunch is maybe for whatever reason your compiler won't let you create a structure as large as you're looking to construct and cuts you off before dzip. I don't know any instance of a compiler doing that, but hey, I guess you might as well consider anything at this point.

    EDIT: Ah, scratch that last bit, you are reading into contacts before that, after all. Anyway, make sure the memory for dzip is properly allocated by the compiler. Just make a temp program that assigns something into it. Also try reading from the same screen coordinates into another variable.
    Last edited by SlyMaelstrom; 05-03-2006 at 08:04 AM.
    Sent from my iPadŽ

  3. #3
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    Yeah, I have been working on this problem for 2 days now and it has me completely baffeled
    Are you sure it's returning?
    Yup, I had watches on all my variables and could see each one change as the function returned.
    I am also running the console at full screen which I measured at 99 x 35 (i think it was 35... 30 something for sure)

    make sure the memory for dzip is properly allocated by the compiler.
    Hmmm... Let me try that.


    BTW, I am planning on doing stringstreams, just have not got to it yet...
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Does Dev-C++ have a good debugger? If not, consider downloading MSVC++ 2005 Express (it's free) and using its debugger. If you run from the debugger and the access violation occurs, it will stop at that point in the code, and you can look in the call stack and watch variables to see what is going on. It takes a little bit of getting used to, but you can find out a lot of good information that way.

    >> char tempchar[z];
    If you do try MSVC++, you'll have to change code like this. It isn't legal in C++, and is allowed by gcc (your compiler) as an extension because C allows it. I would use a vector here instead. To pass a vector<char> to code expecting a character array, just pass &tempchar[0].

  5. #5
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Not sure if this applies, and I haven't looked at your code at all...but the last time I saw a similar problem--i.e. access violation on the return statement--it was because the return address had been overwritten on the stack. This can come up when you overrun a stack array for instance.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Does Dev-C++ have a good debugger?
    It comes with GDB.

    There's a tutorial on www.cprogramming.com somewhere that will get you started with gdb.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    Hey, Thanks for all the help guys! I finally found the problem! It seems that in a totally different part of the program I over ran the pickup.dzip array. hmmmm..... Oh well, live and learn i guess :P
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access violation... can't figure it out...
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2007, 10:52 AM
  2. Access violation when reading a string.
    By Desolation in forum C++ Programming
    Replies: 16
    Last Post: 05-01-2007, 10:25 AM
  3. FtpFileFind access violation with MS VC++ 6.0
    By earth_angel in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2005, 07:02 PM
  4. Help! CListCtrl access violation
    By bonkey in forum Windows Programming
    Replies: 4
    Last Post: 11-18-2003, 02:40 PM
  5. 0xC0000005: Access Violation
    By Strider in forum Windows Programming
    Replies: 3
    Last Post: 11-07-2001, 02:46 PM