I’m getting some strange errors and crashes from my project atm.
It started out with crashes where I got this message:
terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_S_create
So I started adding some cerr << “1” etc to try and find at what line it crashed.
Now I’ve found the crash-line, allthough it doesn’t print out the same error code. In fact, it doesn’t say anything, just crash.
The line that causes the crash is this:
Item* tempItem = new Item(name, id2, desc);
Item.h constructor looks like this:
Item(std::string name_, int itemId_, std::string description_ = "")
: name(name_), itemId(itemId_), description(description_){}
and the variables I’m sending in looks like this:
string name = "Frying pan";
int id2 = 1;
string desc = "It's all rusty";
any ideas? (If you need more code just tell me what you need and I’ll edit it in)
EDIT
some updated info:
name.max_size() = 1073741820
name.length() = 9
desc.max_size() = 1073741820
desc.length() = 14
EDIT2
I’ve now tried creating the Item-objects with hard coding the variable values right before construction as such:
name = "itemName";
desc = "itemDesc";
id2 = 2;
and it STILL crashes right at the line:
Item* tempItem = new Item(name, id2, desc);
There is a limit on how long a
std::stringmay be.std::length_erroris thrown when that length is exceeded. The actual limit depends on your compiler’s implementation of the Standard Template Library.From the C++ standard (19.2.4):
You can check the maximum allowable string size by reading the value of
string::max_size.