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

Unsafe std::shared_ptr usage #2

Open
endorph-soft opened this issue Dec 12, 2018 · 0 comments
Open

Unsafe std::shared_ptr usage #2

endorph-soft opened this issue Dec 12, 2018 · 0 comments

Comments

@endorph-soft
Copy link

Hey,

I found your repo via your blog post here: https://ernestyalumni.wordpress.com/2017/09/28/bringing-cuda-into-the-year-2011-c11-smart-pointers-with-cuda-cub-nccl-streams-and-cuda-unified-memory-management-with-cub-and-cublas/

I see you're using code like this:

// Allocate problem device arrays
auto deleter=[&](float* ptr){ cudaFree(ptr); };
std::shared_ptr<float> d_sh_in(new float[Lx], deleter);
cudaMalloc((void **) &d_sh_in, Lx * sizeof(float));

This code is very dangerous. You can't cast a std::shared_ptr to void** and pass it to cudaMalloc. It will appear to work, but it will not free your memory (due to internal implementation details of std::shared_ptr).

You need to use something like:

// Allocate problem device arrays
float * dPtr;
cudaMalloc(&dPtr, Lx * sizeof(float));
std::shared_ptr<float> d_sh_in(dPtr, [&](float* ptr){ cudaFree(ptr); });
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

1 participant