Why am I getting "Read access violation. _Right_data was 0xFFFFFFFFFFFFFFF7" when calling a function of a parent class?

80 Views Asked by At

I have a class (A) that contains a list of objects of another class (B). In creating an object of class A, it makes a list of objects of class B and then calls a function of each of the B objects. Each B object stores a reference to the A object that created them. The aforementioned function returns the list of b objects by calling get_bs() on the B object's reference to object A. Depending on where in A's constructor I call the function on the B objects, the program either runs or gives an error, and I don't understand what's changing and why the error happens. Rubbish description I know but I don't know how better to word it.

I'm getting the error at runtime and it's pointing to a line in the included file vector which I don't really understand.

Here's the smallest code I could make that reproduced the error:

#include <vector>

class A;

class B {
public:
    B(A &a) : m_a(a) {}
    ~B() {}
    void func();
    B &operator=(const B &b) { return *this; };
private:
    A &m_a;
};

class A {
public:
    A() {
        for (int i = 0; i < 10; i++) {
            m_temp_bs.push_back(B(*this));
            m_bs.push_back(&m_temp_bs[i]);
            m_bs[i]->func(); // fine
        }
        for (int i = 0; i < 10; i++) {
            m_bs[i]->func(); // error ????
        }
    }
    ~A() {}
    std::vector<B*> get_bs() { return m_bs; }
private:
    std::vector<B> m_temp_bs = {};
    std::vector<B*> m_bs = {};
};

void B::func() {
    std::vector<B*> bs = m_a.get_bs();
}

int main() {
    A a = A();
}

Furthermore, when trying to shift things around, defining B::func() inline in the class definition, I get a different error at build: Use of undefined type 'A' which I also don't understand. I imagine the two errors are linked in some way.

Call Stack:

[External Code]
main() Line 40
A::A() Line 25
B::func() Line 36
A::get_bs() Line 28
std::vector<B *,std::allocator<B *>>::vector<B *,std::allocator<B *>>(const std::vector<B *,std::allocator<B *>> & _Right) Line 683     (This is in the vector file)

This is a snippet of said vector file where the error happened:

#endif // _HAS_CXX23 && defined(__cpp_lib_concepts)

    _CONSTEXPR20 vector(const vector& _Right)
        : _Mypair(_One_then_variadic_args_t{}, _Alty_traits::select_on_container_copy_construction(_Right._Getal())) {
        const auto& _Right_data = _Right._Mypair._Myval2;
        const auto _Count       = static_cast<size_type>(_Right_data._Mylast - _Right_data._Myfirst); // Exception thrown: read access violation.
        _Construct_n(_Count, _Right_data._Myfirst, _Right_data._Mylast); //                              _Right_data was 0xFFFFFFFFFFFFFFF7.
    }

I hope the names A and B aren't too short to make the code difficult to read. I don't know what I'd call them otherwise.

I don't really know how to describe what my aim is without going into tons of detail about my original program which I don't think would be appropriate.

Thanks in advance to anyone that helps and sorry for the strange code - I'm pretty new to C++ Oh and I'm fairly new to Stack exchange as well so if I'm doing anything wrong with how I'm asking my question please tell me.

1

There are 1 best solutions below

0
Edward Green On

@molbdnilo Pointed out to me that vectors reallocate memory when they get bigger, which invalidates the pointers stored in it. Turns out you can just preallocate memory for the vector and it works - I added m_temp_bs.reserve(10); to the start of A's constructor and it all works now.

Thanks all!