# RuntimeError: CUDA error: an illegal memory access was encountered after a cudaFree call on a tensor still in use

- **ID:** `cuda/illegal-memory-access-after-free`
- **Domain:** cuda
- **Category:** runtime_error
- **Error Code:** `cudaErrorIllegalAddress`
- **Verification:** ai_generated
- **Fix Rate:** 79%

## Root Cause

A tensor or buffer was freed via cudaFree or torch.cuda.empty_cache while a kernel or asynchronous operation still holds a reference, leading to a use-after-free on the GPU.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| CUDA 12.2 | active | — | — |
| PyTorch 2.2.0 | active | — | — |
| NVIDIA Driver 550.54.14 | active | — | — |

## Workarounds

1. **Ensure all CUDA streams are synchronized before freeing tensors. Example: torch.cuda.synchronize() before calling del tensor or torch.cuda.empty_cache(). For custom kernels, use cudaStreamSynchronize on the relevant stream.** (85% success)
   ```
   Ensure all CUDA streams are synchronized before freeing tensors. Example: torch.cuda.synchronize() before calling del tensor or torch.cuda.empty_cache(). For custom kernels, use cudaStreamSynchronize on the relevant stream.
   ```
2. **Use reference counting or weak references to track tensor lifetimes. In PyTorch, keep a strong reference to the tensor until the kernel completes, e.g., by storing it in a list until the next iteration.** (82% success)
   ```
   Use reference counting or weak references to track tensor lifetimes. In PyTorch, keep a strong reference to the tensor until the kernel completes, e.g., by storing it in a list until the next iteration.
   ```

## Dead Ends

- **** — Synchronization may hide the bug but does not fix the root cause; the free still happens before all uses complete. (70% fail)
- **** — Memory size is unrelated; the error is about lifetime management, not capacity. (95% fail)
