From b370ec92315384b4ff055b8ee46a978e0a75426c Mon Sep 17 00:00:00 2001 From: Sage Ahrac Date: Tue, 6 Jan 2026 14:11:14 +0200 Subject: [PATCH] fix(cgo): resolve CStrings memory leaks Signed-off-by: Sage Ahrac --- pkg/preprocessing/chat_completions/cgo_functions.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/preprocessing/chat_completions/cgo_functions.go b/pkg/preprocessing/chat_completions/cgo_functions.go index 68752d1c1..8826fe4da 100644 --- a/pkg/preprocessing/chat_completions/cgo_functions.go +++ b/pkg/preprocessing/chat_completions/cgo_functions.go @@ -148,7 +148,9 @@ func (w *ChatTemplatingProcessor) RenderChatTemplate(ctx context.Context, return nil, fmt.Errorf("failed to marshal request: %w", err) } // Call the cached Python function - cResult := C.Py_CallRenderJinjaTemplate(C.CString(string(reqJSON))) + cJSONString := C.CString(string(reqJSON)) + defer C.free(unsafe.Pointer(cJSONString)) + cResult := C.Py_CallRenderJinjaTemplate(cJSONString) if cResult == nil { traceLogger.Error(nil, "C function returned nil") return nil, fmt.Errorf("python render_jinja_template failed") @@ -182,7 +184,9 @@ func (w *ChatTemplatingProcessor) FetchChatTemplate( return "", nil, fmt.Errorf("failed to marshal request: %w", err) } // Call the cached Python function - cResult := C.Py_CallGetModelChatTemplate(C.CString(string(reqJSON))) + cJSONString := C.CString(string(reqJSON)) + defer C.free(unsafe.Pointer(cJSONString)) + cResult := C.Py_CallGetModelChatTemplate(cJSONString) if cResult == nil { traceLogger.Error(nil, "C function returned nil") return "", nil, fmt.Errorf("python get_model_chat_template failed")