0

There are some questions with the same error but no one matches with my case.

I have two classes: Loader and Processor. The loader loads a vector of string. The processor calls, in the constructor, the Loader method to load and then, for each string call its method processString. Here is the code:

class Loader
{   
public:
    Loader() {}
    void loadAllId() { loaded_ids_.push_back("a"); }    
    vector<string> loaded_ids_;    
};

class Processor 
{
public:
    Processor() {
        loader_ = new Loader();
        loader_->loadAllId();
        vector<string> loaded = loader_->loaded_ids_;

        // Here the loaded vector contains the string

        for (uint i=0;i<loaded.size();i++)
            processString(loaded[i]);
    }

    void processString(string s) { cout << s << endl; }
private:
    Loader* loader_;
};

int main(int argc, char *argv[])
{
    Processor* proc = new Processor();
}

Between the creation of "loaded" and the for loop that calls processStrings, the vector contains the correct strings. That is what the debug shows.

It crashes at the first call of the method processString. It doesnt even run the first line of that method.

I've tried:

  • Using a string instead of a string vector.

  • Call the Loader constructor, the loadAllId and the processString methods outside in the main function where I create the Processor.

  • Use directly the loader ids vector without making a copy of it (loaded).

All those changes gave me the same error at the processString call.

acm
  • 12,183
  • 5
  • 39
  • 68
Yanet
  • 171
  • 1
  • 7
  • 1
    Please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), and learn how to create a [Minimal, **Complete**, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Jan 23 '18 at 13:13
  • 1
    Use a debugger. Seems you create a string with a nullptr, somewhere the code tried to create a std::string object by passing a null pointer to the constructor that takes const char*. You'll have to figure out where: your code snapshot is useless at all. – 273K Jan 23 '18 at 13:18
  • Could not reproduce: http://coliru.stacked-crooked.com/a/02f017db2860a255 – YSC Jan 23 '18 at 13:59
  • Posted code looks fine to me. You're probably not running the code you think you are. – john Jan 23 '18 at 14:34
  • 1
    Lots of `new`, no `delete`. But don't fix that by adding `delete`, instead remove all the unnecessary `new`. C++ is not Java or C#. – MSalters Jan 23 '18 at 16:30
  • Similar problem, unable to use debugger (occurs only outside IDE). Program randomly crashes with that exception. – John Doe Oct 10 '20 at 07:44

0 Answers0