0

I have a vector of names. Each time the user does a mouse interaction, I want to add an element to my vector and have the user name it. Using the cinder framework parameters as the GUI. Here's the code:

#include "cinder/app/App.h"
#include "cinder/app/RendererGl.h"
#include "cinder/gl/gl.h"
#include "cinder/params/Params.h"

using namespace ci;
using namespace ci::app;
using namespace std;

class TestParamsApp : public App {
  public:
    void setup() override;
    void mouseUp( MouseEvent event ) override;
    void draw() override;

    params::InterfaceGlRef mParams;
    vector<std::string> names;
    int nameIdx = 0;
};

void TestParamsApp::setup()
{
    mParams = params::InterfaceGl::create(getWindow(), "Test Params", toPixels(ivec2(200, 200)));
}

void TestParamsApp::mouseUp(MouseEvent event) {
    names.push_back("");
    mParams->addParam("Actor " + std::to_string(nameIdx), &names[nameIdx]);
    nameIdx++;
}

void TestParamsApp::draw()
{
    gl::clear( Color( 0, 0, 0 ) ); 
    mParams->draw();
}

CINDER_APP( TestParamsApp, RendererGl )

However, after adding the 2nd element i get Exception thrown: read access violation. _First was 0xDDDDDDDD.

Kat
  • 475
  • 1
  • 6
  • 21
  • Have you tried debugging? What do you find? – David G Sep 11 '18 at 00:42
  • Yes, I have been debugging for a long time. When I try printing actorNames[0] I get ascii junk. And upon creating the second element the name I input in the GUI switches to ascii. – Kat Sep 11 '18 at 00:44
  • Based on the [MS's documented magic numbers for dynamic memory management](https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations), you're assuredly invoking *undefined behavior*. Odds are very strong if you (a) change your container subscript operators from `obj[idx]` to form `obj.at(idx)` to get runtime boundary checking, and (b) run in a debugger, you will *quickly* find the problem. We clearly don't have sufficient code to do that, so its on you. – WhozCraig Sep 11 '18 at 00:45
  • 1
    The code you've given us doesn't reproduce the issue. Can you give us a minimal example that we can compile ourselves? – David G Sep 11 '18 at 00:46
  • Code has been updated, it's using the framework cinder – Kat Sep 11 '18 at 00:58
  • `push_back` might cause reallocation and invalidate all the pointers to element of the `vector`. You might need to call `reserve` in advance (e.g. in `setup`). – songyuanyao Sep 11 '18 at 01:04

0 Answers0