Thread: Copying memory, pointers and the like.

  1. #31
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    I think the problem goes beyond the scope of the thread (and the C++ board). Looking at the thread watch in the debugger, the location of the current thread at the occurance of the error suddenly becomes 0000000 (give or take a zero), which to me, sounds bad. After playing around with it, I think it goes back to some OS specific windowing stuff. I've come up with a work-around, but I'm still annoyed that I don't know why I can't get it to work the other way.

    Anyway, thanks for all the help. If I discover anything else, I'll post.
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  2. #32
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'm not so sure it is that bad. I'm also not sure if you are understanding what we are saying. It shouldn't be that difficult to find the problem if you can follow the advice here. If the location of the trhead is 00000000, then you might simply be trying to execute a member function from a null pointer.

    Look at this code. Create an empty project and run it. Where does it fail? Do you get the same type of error?
    Code:
    #include <iostream>
    #include <vector>
    #include <cstddef>
    
    class One
    {
    public:
        One() { vec.push_back(19); }
        std::vector<int> vec;
    };
    
    class Two
    {
    public:
        Two() { ptr_to_one = NULL; }
        One* ptr_to_one;
    };
    
    int main()
    {
        Two* ptr_to_two = NULL;
        std::cout << ptr_to_two->ptr_to_one->vec[0];
    }
    What you need to do is find out where the null or invalid pointer is. Answering the questions and trying the things we've suggested should help you do that.

  3. #33
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Eh, OK. Sorry for being so dense. I'm not a real clear thinker, even on the best of days (and it's getting late, which isn't helping).

    I don't even need to run the above code to tell you that I' get memory corruption or invalid reference errors on:
    Code:
    std::cout << ptr_to_two->ptr_to_one->vec[0];
    , because ptr_to_two is set to NULL.

    I understand that that's the issue I'm having. I've narrowed the cause down more with the debugger and some logging. The issue is in one of the inherited classes of otherVector. When trying to allocate memory for the pointer, it chokes on one of the functions in the constructor that fills data from the inherited classes.

    Looking at the debugger at the inherited class for otherVector, the struct pointers all read "error: cannot obtain value", and the rest of the data isn't being filled (all equals zero). I don't see any sign of an invalid pointer, unless i'm still missing something.

    Again, sorry for my faliure to comprehend here. I'm going to go to bed, and hopefully this will make a bit more sence tomorrow. I'll probably re-read the thread tomorrow too.
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  4. #34
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I don't see any sign of an invalid pointer, unless i'm still missing something.
    You have to look further down the call stack. When you are at the point of the error, open the Call Stack window (or tab or whatever you can find in VC++ that has the Call stack). It should have the list of functions that have been called starting from main and going up to the current function. Start at the second one from the top if you recognize the function. If it is some vector function or some other class that you did not write, go down until you find the first function name you recognize. Double-click on that line and it will bring you to the function. You will be able to look at the variables in that scope. For example, if you change your code to this:
    Code:
    SubClass* subclass = data->subclass;
    std::vector<OtherVectorClass*> vec = subclass->vector;
    OtherVectorClass* ovc = vec[data->var];
    ovc->AddToVector();
    Now, when the error occurs, and you go to this place in the call stack, you can look at data, subclass, vec and ovc separately to see which if any have corrupted data. The first one (from top to bottom) to be bad is the one that you want to concentrate on. You can also verify that vec.size() is 1 and that data->var is 0.

    This doesn't guarantee that you will find the problem, but that's how you need to break it down.

    >> Again, sorry for my faliure to comprehend here.
    No worries. The VC++ debugger is a powerful tool but it takes a while to get used to. Once you get familiar with its features it will really help speed up the time it takes to find bugs like this, especially when the bug is created in a different part of the code than it shows up in.

  5. #35
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Huzzah! Found the problem! Well, I don't actually know what the problem is, but I've identified the code that causes the access violations. Relates to a pointer (passed as an argument) somehow, but I don't really know any details.

    I'll post up later when I know more.
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

Popular pages Recent additions subscribe to a feed