Description
When using the viewer function updatePointCloud a length_error in vector::reserve is thrown out.
Context
I want to delete points out of a point cloud. To do that, I created a KeyEvent function that deletes the points using the BackSpace key.
The code to delete the newest point (which means the last index inside the point cloud) and update the cloud goes as follows:
if(event.getKeySym () == "BackSpace" && event.keyDown()) {
if(!cloud.empty) {
// delete the "newest" point in the point cloud.
cloud->erase(cloud->end() - 1);
// Use any color you like to visualize the cloud
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB> red(cloud, 235, 19, 19);
m_viewer->updatePointCloud(cloud, red, "cloud");
}
}
Expected behavior
The user should be able to delete all points of the cloud until no points remain.
Current Behavior
If the point cloud contains more than three points and points are deleted until only two points remain, the following issue will be displayed if the user wants to delete a point again:
terminate called after throwing an instance of 'std::length_error'
what(): vector::reserve
This error is only thrown if the function updatePointCloud is used. But if the function is used this error will be thrown while running the function viewer->spinOnce(100);
To Reproduce
KeyEvent function using an arbitrary key to delete points using the function as mentioned above.-> Now this error occures.
-> At first, I thought this issue might be caused due to the
cloud->erase(cloud->end() - 1);
because I thought it might be some bad code. But this is definitely not the issue. I tried to delete points using an ExtractIndices-function (as mentioned here and the error occured nonetheless.
-> If the point cloud contains only two points, everything works fine, so no error is displayed. It only happens if the cloud contains three or more points.
-> I already created a stackoverflow entry for this issue (see here) but there is now answer until now.
-> I have a picture of the full stacktrace after the error was thrown. I'll put it here:

Tested environment
Let me know if you need some more information. Cheers!
This is not a MCVE. Seems unrelated to PCL based on information provided
Also the erase call looks suspiciously bad. Try if (!vector.empty()) vec.erase(vec.crbegin());
This is not a MCVE. Seems unrelated to PCL based on information provided
Are there some more informations needed? I pointed out that this issue is related to updatePointCloud.
Also the erase call looks suspiciously bad. Try
if (!vector.empty()) vec.erase(vec.crbegin());
The erase call was used due to an administrator post from here. Also it's not possible to call crbegin(). It's only possible to do that for the points vector _within_ the cloud. Is that what you mean?
I didn't notice you were using the cloud. The usage appears correct, except at size() = 0
Regarding MCVE, you example isn't minimum or easily verifiable. The issue also doesn't show that the problem is in the library or some code you wrote (in which case SO is the best place)
Alright, I did some more testing and stripped down the issue. Hope it's more clear now.
Delete the points until only two points remain
Possible issue: user-code: The delete then tries to delete 2 or 3 poins, trying to delete position -1
Closing in favor of the SO question since issue seems unrelated to PCL library code.
The delete only deletes one point per function call and it is always the last point. This does work if the cloud only contains two points at the beginning. All ppints can be deleted. I've pointed that out- Also, I wrote that this problem occurs as well if an ExtractIndices-Function was used and that this issue only occures if the updatePointCloud-function is executed.
Therefore, I don't really understand why this issue is closed now. :-\
updatePointCloud which appears if the new point cloud is larger than the older. This isn't the case here. You can try using removePointCloud + addPointCloud instead of update, but based on discussions, it will not help you.erase function. But the check for empty (seems wrong to use variable instead of a function-call) implies that can't be the reason.The last is the key point. Without that, based on first 2 points, I can't be sure that the issue isn't in user-code.
Okay, I understand. Will remember it the next time. :-)
I found another issue on this page about this (listed here).
So I tried it using the removePointCloud + addPointCloud as you and the other users suggested. Frankly, that did it. It works for now. Thank you!
Should I create an MCVE nonetheless? I can provide a minimal example of code to give some more details, but the issue is already there so I'm not sure whether that's really necessary.
If you don't mind that would be great. It would save me some time setting up things ourselves and we could jump straight into debugging the issue :grimacing:
Alright, I've put a very small project together to reproduce this issue. Here you go:
LengthError.zip