`cuda.OpenCVResult` 中 C 分配的 `OpenCVResult.Message` 可能存在内存泄漏
作者: OvOhao创建于 2026年8月28日更新于 2026年8月28日
- Go calls any wrapped
cudaoperation — e.g.cuda/arithm.go:25(return OpenCVResult(C.GpuAbs(src.p, dst.p, nil))). - The C++ wrapper runs the OpenCV call inside
try/catch; oncv::Exceptionit returnserrorResult(e.code, e.what()). errorResultmallocsstrlen(message)+1bytes, copies the message in, and stores the pointer in the returned-by-value struct fieldri.Message. cgo copies the struct into the Go frame, soresult.Messagebecomes the only reference to that heap block in the program.cuda/errors.go:14callsC.GoString(result.Message), which copies the bytes into a new Go string, then returns. There is nodefer, nounsafeimport, and noC.freeanywhere in the file;resultis a by-value parameter, so after the function returns no Go code holds the pointer and no other function is able to free it.- One
mallocblock ofstrlen(e.what())+1bytes is leaked per failed operation. OpenCV exception strings have the formOpenCV(<ver>) <abs-path>:<line>: error: (<code>:<name>) <detail> in function '<fn>', typically 120–400 bytes and growing with the source path length.
内容来源: hybridgroup/gocv