when pushback function of std::vector make program crash? How to handle it?
-
Hi have faced one issue when my application get crash due to below line:
strVehicleNo = "abc";
VecVehicleCodeDigit.push_back(strVehicleNo.substr(strVehicleNo.length() - 4));
i want to find generic solution for this problem.
-
- Check your
VecVehicleCodeDigit
. strVehicleNo.substr(strVehicleNo.length() - 4
): given that you can see your string is 3 characters long, what do you expect from this? Strange question....
- Check your
-
@Qt-embedded-developer
Did you read what I wrote and actually think about it?strVehicleNo.substr(strVehicleNo.length() - 4)
: given that you can see your string is 3 characters long, what do you expect from this?Obviously
M3
is even shorter. So you cannot subtract 4 from its length, can you?But when server data wrong come like " M3". Then this crash happen.
Either sort out why "wrong data" can come from server, or code to allow for it so your client does not crash.
I really do not understand what your actual question is or what you expect people to say about it.
-
Hi,
@Qt-embedded-developer did you actually to do the math that @JonB suggested ?
It's not the push_back call that is the issue, it's your substr call.
Use a debugger.
-
Thank you all dear for answers...