Skip to content

Calling cv::cuda::Stream::Null() results in a stray cudaMalloc() call #8725

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

Closed
nglee opened this issue May 12, 2017 · 1 comment
Closed

Calling cv::cuda::Stream::Null() results in a stray cudaMalloc() call #8725

nglee opened this issue May 12, 2017 · 1 comment
Labels
bug category: gpu/cuda (contrib) OpenCV 4.0+: moved to opencv_contrib

Comments

@nglee
Copy link
Contributor

nglee commented May 12, 2017

System information (version)
  • OpenCV => master
  • Operating System / Platform => Windows 10 64-Bit , CUDA 8.0
  • Compiler => Visual Studio 2015
Detailed description

If I call cv::cuda::Stream::Null() in my code, it seems it calls cudaMalloc() without freeing the allocated memory, resulting in a memory leak error when tested with the command line memory checker cuda-memcheck --leak-check full.

Steps to reproduce
__global__ void hello()
{
	printf("%u: hello\n", threadIdx.x);
}
int main()
{
	hello<<<1, 16, 0, cv::cuda::StreamAccessor::getStream(cv::cuda::Stream::Null())>>>();
	CheckCudaError(cudaDeviceSynchronize());
	CheckCudaError(cudaDeviceReset());
}

If you profile above code with nvprof then it calls cudaMalloc() without any cudaFree() followed.

@mshabunin mshabunin added the bug label Sep 7, 2017
nglee added a commit to nglee/opencv_cuda_test that referenced this issue Jan 30, 2018
CCL_3.4: stub for working with CCL cuda algorithm

cuda_stream_matser: related to opencv/opencv#8725
@nglee
Copy link
Contributor Author

nglee commented Jun 6, 2018

This comment is a summary note for the merged PR that fixed this issue.

Investigation
Experiment
  • When I added cudaSafeCall to cudaFree in MemoryPool::release(), which is responsible for deallocating the GPU memory, it returned driver shutting down (cudaErrorCudartUnloading = 29) error code.
Reasoning
  • With help from this and this, it seems that cuda runtime environment has been shut down before the destructor of DefaultDeviceInitializer is called. This should be the reason why driver shutting down (cudaErrorCudartUnloading = 29) error was returned in the experiment.
  • CUDA context being destroyed before cudaFree is called, cuda-memcheck may assume this situation as memory leak.
Solutions
  • (Option 1) Do not use a global instance that de-allocates CUDA memory in its destructor. This is already mentioned in the OpenCV doc for GpuMat, that says:

You are not recommended to leave static or global GpuMat variables allocated, that is, to rely on its destructor. The destruction order of such variables and CUDA context is undefined. GPU memory release function returns error if the CUDA context has been destroyed before.

  • (Option 2) Do not pre-allocate CUDA memory for each Stream object, so that the de-allocation does not happen at the destructor of a global instance. The merged PR that fixes this issue takes this path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug category: gpu/cuda (contrib) OpenCV 4.0+: moved to opencv_contrib
Projects
None yet
Development

No branches or pull requests

3 participants