Skip to content

Free device allocation when copy fails - #2220

Open
fallintoplace wants to merge 1 commit into
NVIDIA:mainfrom
fallintoplace:fix/device-allocate-copy-cleanup
Open

Free device allocation when copy fails#2220
fallintoplace wants to merge 1 commit into
NVIDIA:mainfrom
fallintoplace:fix/device-allocate-copy-cleanup

Conversation

@fallintoplace

Copy link
Copy Markdown
Contributor

Problem

device_allocate calls cudaMalloc and then copies the initial value with cudaMemcpy. If that copy returns an error, the function returns an empty pointer, but the successful device allocation has no owner and is leaked.

The exception path already tried to free the allocation, which made the missing normal-error cleanup easier to miss.

Fix

  • Give the allocation a device_ptr_t owner immediately after cudaMalloc succeeds.
  • Return that owner on success.
  • Let the owner release the allocation when cudaMemcpy fails or host construction throws.

Test

  • Added a focused test that forces cudaMemcpy to fail and counts the matching cudaFree call.
  • clang-format-21 --dry-run --Werror
  • CMake configure and build with CUDA disabled

@copy-pr-bot

copy-pr-bot Bot commented Aug 21, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@ericniebler

Copy link
Copy Markdown
Collaborator

/ok to test 7779c5d

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.

2 participants