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.
-
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
-
if ( strVehicleNo.length() > 4 ) { VecVehicleCodeDigit.push_back(strVehicleNo.substr(strVehicleNo.length() - 4)); } else { std::cout << " string " << strVehicleNo << " is ignored" <<std::endl; }
@JoeCFD i want to know pushback function why make program crash. Is it unauthorized memory access 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....
@JonB hi this data come from server and it need to be like "GJ-05-4578". But when server data wrong come like " M3". Then this crash happen.
- Check your
-
@JonB hi this data come from server and it need to be like "GJ-05-4578". But when server data wrong come like " M3". Then this crash happen.
@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.
-
@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.
@JonB i want to know pushback function why make program crash. Is it unauthorized memory access problem?
-
@JonB i want to know pushback function why make program crash. Is it unauthorized memory access problem?
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...