I've tracked this problem through a lot my classes, and it surprised me, that the source of this error is a simple std::string = std::string
operation.
I won't post the whole code, just the functions that are executed in sequence. I still think that there is too much code for SO standards, but I see no other option.
Some context notes:
- OrderedPair is a public struct
{ int y, int x }
in a separatelib.inc
file. - Leading underscore marks a variable declared in the function header
- Trailing underscore marks a class member variable
ncefm.cc -- basically the main file
std::vector<std::string> juststring = {"teststring", "another string", "foobar"};
List* list0 = new List(w0_, juststring); // Source of the error
list.cc -- constructor List()
is called here
Just ignore the optional variables, they aren't called anyway
List::List(Frame* const _parent, std::vector<std::string> &_list,
const OrderedPair &_pos = {0, 0}, const unsigned int &_spacing = 1,
const unsigned int &_maxsize = 0) {
pos_ = _pos;
size_ = _list.size();
spacing_ = _spacing;
maxsize_ = _maxsize;
parent_ = _parent;
Fill(_list); //Source of the error
Redraw();
parent_->AddWidget(this);
}
list.cc -- Member function Fill()
list_ is a member variable of type std::vector
void List::Fill(std::vector<std::string> &_list) {
for (unsigned int loop = size_; loop < (size_ + _list.size()); loop++) {
list_.push_back(new Label(parent_, _list[loop], {pos_.y + (loop * spacing_),
pos_.x}, maxsize_)); // source of the error (the constructor Label() )
}
}
label.cc -- constructor Label() is called here
Label::Label(Frame* const _parent, std::string &_text,
const OrderedPair &_pos = {0,0}, const unsigned int &_maxsize = 0) {
pos_ = _pos;
maxsize_ = _maxsize;
parent_ = _parent;
SetText(_text); // Source of the error
parent_->AddWidget(this);
}
list.cc -- Member function SetText()
Here we finally are, the source of the error is...
void Label::SetText(std::string& _text) {
if (maxsize_ != 0 && _text.length() > maxsize_) _text.resize(maxsize_);
text_ = _text; // THIS?!
size_ = text_.length();
Redraw();
}
If I just comment out this line, the error dissapears, of course, that defeats the functionality. text_.assign(_text); doesn't work either.
label.h -- To show the header file and definition of some variable text_
class Label {
private:
OrderedPair pos_;
unsigned int size_;
unsigned int maxsize_;
std::string text_;
Frame* parent_;
public:
Label(Frame* const _parent, std::string &_text, const OrderedPair &_pos,
const unsigned int &_maxsize);
~Label();
inline const Frame* GetParent();
inline unsigned int GetSize();
inline std::string Text();
void SetText(std::string&);
void Move(const OrderedPair &_pos);
void RMove(const OrderedPair &_pos);
void Redraw();
void Clear();
};
If this is too messy, or you think you need more information about my classes, ask me to add them here, or look at my GitHub repo (public) on the dev branch here.
You set
size_
to_list.size()
in the list constructor, so it means you start iterating your list from the end, and_list[loop]
is out of bounds.Didn't you mean this instead ?