Skip to content

Commit

Permalink
Fix memory leak in TextBuffer
Browse files Browse the repository at this point in the history
Thanks to @tomet for pointing it out.
  • Loading branch information
pwiecz committed May 15, 2023
1 parent b636f0b commit 599ba40
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions text.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

type StyleTableEntry struct {
Color Color
Font Font
Size int
Font Font
Size int
}

type TextBuffer struct {
Expand Down Expand Up @@ -51,12 +51,16 @@ func (b *TextBuffer) Append(txt string) {
}

func (b *TextBuffer) Text() string {
return C.GoString(C.go_fltk_TextBuffer_text(b.ptr()))
cStr := C.go_fltk_TextBuffer_text(b.ptr())
defer C.free(unsafe.Pointer(cStr))
return C.GoString(cStr)
}

// GetTextRange - get text from start and end position
func (b *TextBuffer) GetTextRange(start, end int) string {
return C.GoString(C.go_fltk_TextBuffer_text_range(b.ptr(), C.int(start), C.int(end)))
cStr := C.go_fltk_TextBuffer_text_range(b.ptr(), C.int(start), C.int(end))
defer C.free(unsafe.Pointer(cStr))
return C.GoString(cStr)
}

// Highlight - highlight text from start and end position
Expand Down Expand Up @@ -151,8 +155,9 @@ func (b *TextBuffer) GetSelectionPosition() (int, int) {

// GetSelectionText return the text within the current selection
func (b *TextBuffer) GetSelectionText() string {
txtstr := C.go_fltk_TextBuffer_selection_text(b.ptr())
return C.GoString(txtstr)
cStr := C.go_fltk_TextBuffer_selection_text(b.ptr())
defer C.free(unsafe.Pointer(cStr))
return C.GoString(cStr)
}

// UnSelect - unselect any selections in the buffer
Expand Down

0 comments on commit 599ba40

Please sign in to comment.