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

Fixing valgrind notifications #1078

Merged

Conversation

wolfv
Copy link
Member

@wolfv wolfv commented Aug 15, 2018

With these changes valgrind stops complaining.

The big ones: not using realloc in svector anymore. This might hit performance somewhat but I think it's better to have a clean valgrind output.

Use the std::allocator to allocate memory for the owning buffer. That one is a bit annoying but I think warranted for library functions. Otherwise you get an error because of a mismatch between free and new ... [] where valgrind wants one to use the corresponding delete[] instead of free or whatever the allocator is using.

The workaround would be to write a shim allocator that calls dealloc with delete[]

There was also some tiny bugs in the tests as well as one memory leak with move assignment on owning buffers.

@@ -378,7 +378,6 @@ namespace xt
inline auto xbuffer_owner_storage<CP, A>::operator=(self_type&& rhs) -> self_type&
{
swap(rhs);
rhs.m_moved_from = true;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was a memory leak because the swapped to rhs didn't deallocate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 makes sense

@@ -347,6 +347,7 @@ namespace xt
row_major_result<> rm;
dyn_opt_ass_type vec;
vec.resize(rm.m_shape, layout_type::row_major);
vec.fill(123);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this stuff made valgrind complain about conditional jumps depending on uninitialized memory.

using owner_adaptor = xbuffer_adaptor<double*&, acquire_ownership>;

TEST(xbuffer_adaptor, owner_destructor)
{
size_t size = 100;
double* data = new double[size];
double* data = allocator{}.allocate(size);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weird that we were not deleting in the tests.

Why using an allocator btw?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're not deleting because that's what the owning buffer adaptor is supposed to do and the allocator stuff because of otherwise mismathcing new / delete

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yes, that is right

@@ -482,7 +482,7 @@ namespace xt
// Allocate memory
m_word_size = std::size_t(atoi(&typestring[2]));
m_n_bytes = compute_size(shape) * m_word_size;
m_buffer = new char[m_n_bytes];
m_buffer = std::allocator<char>{}.allocate(m_n_bytes);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What change does it make to use the allocator instead of the new operator?

Another question is: If we use std::allocator::allocate, shouldn't we use std::allocator::deallocate instead of delete on the other side?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's what's used in the buffer adaptor

@SylvainCorlay
Copy link
Member

All makes sense. Merging when all is green.

@SylvainCorlay SylvainCorlay merged commit a3224e0 into xtensor-stack:master Aug 15, 2018
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

Successfully merging this pull request may close these issues.

None yet

2 participants