Std::bad_alloc error

Hi All,

I am getting this error running an OF application (an example inside an addon):
terminate called after throwing an instance of ‘std::bad_alloc’
what(): std::bad_alloc

the line where it breaks is at ofxLabel.cpp line 39:
textMesh = getTextMesh(name + (string)label, b.x + textPadding, b.y + b.height / 2 + 4)

Debugging it I found out that the (string)label is the problem but I still don’t know why.
So tried to change how my code writes the label text from:

prepProgressBarLabel->getParameter().cast() = to_string(prepProgressBarParam).substr(0,3) + " sec until recording starts";

to:

std::string timeString = to_string(prepProgressBarParam).substr(0,3) + " sec until recording starts";
prepProgressBarLabel->setup(timeString);

The fascinating part is that this error only happens on Linux and when I fixed it on Linux it broke on Mac OS…
By Linux I mean Ubuntu 14.04.
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
QtCreator 3.6.1

My C++ knowledge only goes so far…and OF is totally new for me.

What am I missing?
Any ideas?

Thanks in advance!
Camila

which type is label and prepProgessBarParam?
What is the error you get on Mac OS?

prepProgressBarParam is: ofParameter<float>
and on Mac is the same error: terminate called after throwing an instance of ‘std::bad_alloc’
what(): std::bad_alloc

the setup method is meant for setup rather. you can assign a new string value to ofxLabel with operator =, so it’s simply:

prepProgressBarLabel = prepProgressBarParam.toString() + " sec until recording starts";

try it and see if works for you.

PS: what did you want to do with substr(0,3) actually? if you want to set the precision/width, use ofToString() http://openframeworks.cc/documentation/utils/ofUtils/#show_ofToString like:

ofToString(prepProgressBarParam.get(), <precision>, <width>, <fill>)

also, generally prefer ofToString() over to_string if you care about portability. there have been issues with to_string on some older compiler versions http://stackoverflow.com/questions/12975341/to-string-is-not-a-member-of-std-says-g

In the end what worked was:
std::string progress = ofToString(prepProgressBarParam.get(), 3) + " sec until recording starts";
progressBarLabel->getParameter().cast().set(progressBarLabel->getName(), progress);

It is now running correcting on Linux and Mac.

Thanks for the help!

P.S: I also ran my program with valgrind and it found memory leaks. This solution is definitely just a workaround (for now).