# 运行时错误：CUDA错误：在释放仍在使用的张量后遇到非法内存访问

- **ID:** `cuda/illegal-memory-access-after-free`
- **领域:** cuda
- **类别:** runtime_error
- **错误码:** `cudaErrorIllegalAddress`
- **验证级别:** ai_generated
- **修复率:** 79%

## 根因

张量或缓冲区通过cudaFree或torch.cuda.empty_cache被释放，而内核或异步操作仍持有引用，导致GPU上的释放后使用。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| CUDA 12.2 | active | — | — |
| PyTorch 2.2.0 | active | — | — |
| NVIDIA Driver 550.54.14 | active | — | — |

## 解决方案

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.
   ```
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.
   ```

## 无效尝试

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