﻿# Retain callback-owned resources with CUDA graph user objects

Associate external callback resources with explicit graph user-object references and respect asynchronous release and destructor constraints.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/guide-perf-graph-user-object-lifetime/
Language: en

A graph host callback uses an external resource whose ordinary C++ owner goes out of scope before replay completes. Successful capture does not extend that resource's lifetime automatically. CUDA user objects provide a reference mechanism associated with graphs, but it must be connected to the actual owner and asynchronous execution rather than treated as a general memory-safety switch.

## Name the externally owned resource

Identify the callback's resource, its ordinary owner and the final operation that can access it. A pointer captured in node parameters is not ownership. Preserve graph cloning, instantiation and launch relationships. If an application retains only the executable handle while releasing the object used by a host node, a later callback can outlive the ordinary lexical scope that originally prepared it.

## Track user-object references

The runtime creates a user object with caller-owned references and a destructor callback. References can be retained by a graph or transferred through the documented move behavior. Record each owner rather than releasing a count merely because a local graph variable was destroyed. Releasing references not owned by the caller is outside the contract and cannot safely simplify bookkeeping.

## Follow asynchronous launch lifetime

Graph cloning and executable instantiation participate in the documented retained-reference behavior. An asynchronous launch can require deferred release even after an executable handle is destroyed. Do not equate handle destruction with completion of the callback's access. The ownership record must cover the launch generation still in flight, not just the objects visible in the submitting function.

## Respect destructor constraints

User-object destructors run under runtime constraints: they cannot call CUDA APIs and should avoid blocking the shared internal thread. The current required flag also makes destructor synchronization an explicit application responsibility. If another thread must finish cleanup, arrange a supported signal without blocking CUDA forward progress. A generic C++ destructor that synchronizes the device is not automatically suitable here.

## Verify delayed callback cleanup

In a harmless isolated graph fixture, retain a small external marker, launch asynchronously and release caller and graph ownership in a documented order. Observe callback access and eventual cleanup separately. Acceptance establishes the tested reference lifecycle. It does not retain arbitrary unregistered pointers, guarantee request-level correctness or allow a destructor to wait on CUDA work whose progress depends on the same runtime thread.

## Primary references

[CUDA Runtime Graph Management](https://docs.nvidia.com/cuda/cuda-runtime-api/cuda_runtime_api/group__CUDART__GRAPH.html).

[CUDA Graph Update and Resource Ownership](https://docs.nvidia.com/cuda/cuda-programming-guide/04-special-topics/cuda-graphs.html).

[CUDA Asynchronous Execution](https://docs.nvidia.com/cuda/cuda-programming-guide/02-basics/asynchronous-execution.html).

Related reading: [CUDA Graph capture: why some operations cannot join the capture](/journal/learn-graph-capture-forbidden-operations/); [CUDA streams: express dependencies before asking for overlap](/journal/learn-stream-ordering-dependencies/).
