1

I am trying to use push_back as part of a member function in class StrBlobm to add elements to a vector in a shared pointer also contained in StrBlobm, but I keep getting this error:

Exception thrown: read access violation. std::_Vector_alloc,std::allocator >,std::allocator,std::allocator > > > >::_Myend(...) returned 0xC. occurred

I have similar trouble using iterators to print from the vector inside the shared pointer as well. What is causing this violation, and how do I fix it?

#include <memory>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <initializer_list>

class StrBlobm {
public:
    StrBlobm() = default;
    StrBlobm(std::initializer_list<std::string> il) :
        mydata(std::make_shared<std::vector<std::string>>(il)) {};

    void add(std::string& a)
    {
        mydata->push_back(a);
    }

private:
    std::shared_ptr<std::vector<std::string>> mydata;
};

int main()
{
    StrBlobm myblob;
    std::ifstream is;
    is.open("somefilepathtotxtdocument.txt");
    while(is)
    {
        std::string mystr;
        std::getline(is, mystr);
        myblob.add(mystr);
    }
    is.close();
    return 0;
}
OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
Mild Max
  • 37
  • 1
  • 8
  • Could not reproduce ... code compiles and runs fine. (after adding the missing closing semicolon for main) – OrenIshShalom Aug 28 '18 at 04:04
  • 7
    The default constructor doesn't initialize mydata, right? And that's the constructor you're using? – zzxyz Aug 28 '18 at 04:07
  • 1
    Unrelated: `std::getline(is, mystr);` can fail unchecked. Use something like `std::string mystr; while(std::getline(is, mystr)) { myblob.add(mystr); }` instead. – user4581301 Aug 28 '18 at 04:12
  • Also unrelated. If you put 'using std::string;using std::vector;using std::shared_ptr;' after your includes, it can be 'shared_ptr> mydata;'. – zzxyz Aug 28 '18 at 04:22
  • 2
    @zzxyz - no it doesn't, and yes it is. Also thanks for the tip – Mild Max Aug 28 '18 at 04:28

1 Answers1

1

Your constructor StrBlobm(std::initializer_list<std::string> il) is not called.

But StrBlobm() is called, and there is no initialize code for mydata member variable. (It means, mydata is just pointing nullptr and you should not access with -> keyword)

You should code like follows. (It's just one example.)

#include <memory>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <initializer_list>

class StrBlobm {
public:
    StrBlobm() :
        mydata(std::make_shared<std::vector<std::string>>()) { // initialize mydata
    }

    StrBlobm(std::initializer_list<std::string> il) :
        mydata(std::make_shared<std::vector<std::string>>(il)) 
    {
    };

    void add(std::string& a)
    {
        mydata->push_back(a);
    }

private:
    std::shared_ptr<std::vector<std::string>> mydata;
};

int main()
{
    //call StrBlobm's default constructor (the constructor that takes no parameters.)
    StrBlobm myblob; 

    std::ifstream is;
    is.open("somefilepathtotxtdocument.txt");
    while (is)
    {
        std::string mystr;
        std::getline(is, mystr);
        myblob.add(mystr);
    }
    is.close();
    return 0;
}