Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifetime of Data for for TYPE_32FC1, TYPE_16UC1, TYPE_16S1, and TYPE_MONO #706

Open
cturcotte-qnx opened this issue Jun 9, 2021 · 1 comment
Assignees

Comments

@cturcotte-qnx
Copy link
Contributor

Improve lifetime of data_ptr in Rviz through the use of shared pointers.

The formats TYPE_32FC1, TYPE_16UC1, TYPE_16S1, and TYPE_MONO create a vector buffer inside the setFormatAndNormalizeDataIfNecessary function. This could cause memory leaks if they are not properly deallocated.

A change made using shared pointers for YUV422 and YUV422_YUY2 format images inside ros_image_texture.cpp could be a solution to this problem.

As seen in pull request #701

@clalancette
Copy link
Contributor

Actually, it is much worse than a memory leak. For instance, looking at this code:

} else if ( // NOLINT enforces bracket on the same line, which makes code unreadable
image_data.encoding_ == sensor_msgs::image_encodings::TYPE_16UC1 ||
image_data.encoding_ == sensor_msgs::image_encodings::TYPE_16SC1 ||
image_data.encoding_ == sensor_msgs::image_encodings::MONO16)
{
image_data.size_ /= sizeof(uint16_t);
std::vector<uint8_t> buffer = normalize<uint16_t>(
reinterpret_cast<const uint16_t *>(image_data.data_ptr_),
image_data.size_);
image_data.pixel_format_ = Ogre::PF_BYTE_L;
image_data.data_ptr_ = &buffer[0];
} else if (image_data.encoding_.find("bayer") == 0) {

We enter the scope, do a stack allocation of a std::vector, and then take the address of that. But then we leave the scope, which will destroy the std::vector. That means that image_data.data_ptr is holding onto a pointer to garbage data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants