2

I have a class that looks like the following:

class ModelCommand {
public:
    virtual ~ModelCommand() {};
};

class FolderCommand : public ModelCommand {
public:
    std::string text;
    unsigned width;
    bool isBackspace;

    FolderCommand(bool isBackspace, unsigned width, std::string text = "") : text(text), width(width), isBackspace(isBackspace) {}
};

class CModel {
private:
    string _folder;
public:
    void Update(std::shared_ptr<ModelCommand> &cmd);
};

Then in my controller I have an instance of my model and update it using the new FolderCommand object I create:

shared_ptr<CModel> model;

shared_ptr<ModelCommand> cmd = dynamic_pointer_cast<ModelCommand>(make_shared<FolderCommand>(false, 20, "a"));

model->Update(cmd);

And then inside my update method of CModel I try to do the following:

void CModel::Update(std::shared_ptr<ModelCommand> &cmd) {

    if (auto folderCmd = dynamic_pointer_cast<FolderCommand>(cmd)) {
        if(!folderCmd->isBackspace)

            // This is where _folder is unable to read memory
            _folder += folderCmd->text;

        else if(folderCmd->isBackspace && _folder.length() > 0)
            _folder.erase(--_folder.end());

        folderCmd->text = _folder;
    }
}

This results in the CModel's _folder variable being "Unable to Read Memory".

Can someone explain and provide a solution to this problem?

Thanks.

UPDATE Added some more code for clarification

5
  • Do you initialize the _folder member to something, before appending to it? Commented Apr 13, 2016 at 18:57
  • Yes I've tried initializing _folder to an empty string when the CModel gets constructed it resulted in the same error. Commented Apr 13, 2016 at 18:59
  • Please, add code, using Update method Commented Apr 13, 2016 at 19:03
  • and the instanciation of FolderCommand, what are you passing to the constructor exactly Commented Apr 13, 2016 at 19:04
  • Provide a complete (but minimal!) example. Commented Apr 13, 2016 at 19:17

1 Answer 1

3

Using the first snippet you posted, I called the Update method as below, and no "Unable to Read Memory" issue arose.

int main()
{
  shared_ptr<ModelCommand> fc(new FolderCommand("somestring\\"));
  CModel mod;

  mod.Update(fc);
  mod.print_folder();

  return 0;
}

I think that would be interesting to see how the Update method is being used.

Obs.: I added a public member function, print_folder, to print ou the _folder private member. Also, I'd make just a comment, however I still don't have this privilege.

Sign up to request clarification or add additional context in comments.

5 Comments

I believe this should be a comment and not an answer.
Yep, I pointed that out. I just didn't have the privilege by then.
In my current code I used a shared_ptr to my model. When I looked at this example I simply made an instance of my CModel and called Update using that. As to why using a shared_ptr to my model resulted in an error I'm unsure but thanks Adriano Carafini!!
@Booster you did not instanciate CModel in your example.
.. That would definitely do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.