when pushback function of std::vector make program crash? How to handle it?
-
wrote on 17 Dec 2021, 14:34 last edited by Qt embedded developer
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.
-
wrote on 17 Dec 2021, 14:50 last edited by JoeCFD
if ( strVehicleNo.length() > 4 ) { VecVehicleCodeDigit.push_back(strVehicleNo.substr(strVehicleNo.length() - 4)); } else { std::cout << " string " << strVehicleNo << " is ignored" <<std::endl; }
-
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.
wrote on 17 Dec 2021, 14:50 last edited by JonB- 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
-
wrote on 17 Dec 2021, 14:50 last edited by JoeCFD
if ( strVehicleNo.length() > 4 ) { VecVehicleCodeDigit.push_back(strVehicleNo.substr(strVehicleNo.length() - 4)); } else { std::cout << " string " << strVehicleNo << " is ignored" <<std::endl; }
-
if ( strVehicleNo.length() > 4 ) { VecVehicleCodeDigit.push_back(strVehicleNo.substr(strVehicleNo.length() - 4)); } else { std::cout << " string " << strVehicleNo << " is ignored" <<std::endl; }
wrote on 18 Dec 2021, 06:31 last edited by@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....
wrote on 18 Dec 2021, 06:34 last edited by@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.
wrote on 18 Dec 2021, 08:27 last edited by JonB@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.
wrote on 18 Dec 2021, 14:32 last edited by@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.
-
wrote on 19 Dec 2021, 13:54 last edited by
Thank you all dear for answers...
2/9