Skip to content

Commit 909c753

Browse files
committed
indicator: fix memory leaks
closes: #6
1 parent 6ed7439 commit 909c753

File tree

1 file changed

+72
-21
lines changed

1 file changed

+72
-21
lines changed

indicator.go

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package appindicator
22

33
// #cgo pkg-config: appindicator3-0.1
4+
// #include <stdlib.h>
45
// #include <libappindicator/app-indicator.h>
56
import "C"
67
import (
@@ -31,23 +32,37 @@ func (indicator *Indicator) Object() *glib.Object {
3132

3233
// New creates a fresh indicator.
3334
func New(id, iconName string, category Category) *Indicator {
35+
idC := C.CString(id)
36+
defer C.free(unsafe.Pointer(idC))
37+
iconNameC := C.CString(iconName)
38+
defer C.free(unsafe.Pointer(iconNameC))
39+
categoryC := (C.AppIndicatorCategory)(category)
40+
3441
return &Indicator{
3542
indicator: C.app_indicator_new(
36-
C.CString(id),
37-
C.CString(iconName),
38-
category.native(),
43+
idC,
44+
iconNameC,
45+
categoryC,
3946
),
4047
}
4148
}
4249

4350
// NewWithPath creates a fresh indicator with custom icon theme path.
4451
func NewWithPath(id, iconName string, category Category, iconThemePath string) *Indicator {
52+
idC := C.CString(id)
53+
defer C.free(unsafe.Pointer(idC))
54+
iconNameC := C.CString(iconName)
55+
defer C.free(unsafe.Pointer(iconNameC))
56+
categoryC := (C.AppIndicatorCategory)(category)
57+
iconThemePathC := C.CString(iconThemePath)
58+
defer C.free(unsafe.Pointer(iconThemePathC))
59+
4560
return &Indicator{
4661
indicator: C.app_indicator_new_with_path(
47-
C.CString(id),
48-
C.CString(iconName),
49-
category.native(),
50-
C.CString(iconThemePath),
62+
idC,
63+
iconNameC,
64+
categoryC,
65+
iconThemePathC,
5166
),
5267
}
5368
}
@@ -56,26 +71,36 @@ func NewWithPath(id, iconName string, category Category, iconThemePath string) *
5671

5772
// SetStatus sets status of indicator.
5873
func (indicator *Indicator) SetStatus(status Status) {
74+
statusC := (C.AppIndicatorStatus)(status)
75+
5976
C.app_indicator_set_status(
6077
indicator.indicator,
61-
status.native(),
78+
statusC,
6279
)
6380
}
6481

6582
// SetAttentionIcon sets attention icon of indicator.
6683
func (indicator *Indicator) SetAttentionIcon(iconName string) {
84+
iconNameC := C.CString(iconName)
85+
defer C.free(unsafe.Pointer(iconNameC))
86+
6787
C.app_indicator_set_attention_icon(
6888
indicator.indicator,
69-
C.CString(iconName),
89+
iconNameC,
7090
)
7191
}
7292

7393
// SetAttentionIconFull sets attention icon of indicator with description.
7494
func (indicator *Indicator) SetAttentionIconFull(iconName, iconDesc string) {
95+
iconNameC := C.CString(iconName)
96+
defer C.free(unsafe.Pointer(iconNameC))
97+
iconDescC := C.CString(iconDesc)
98+
defer C.free(unsafe.Pointer(iconDescC))
99+
75100
C.app_indicator_set_attention_icon_full(
76101
indicator.indicator,
77-
C.CString(iconName),
78-
C.CString(iconDesc),
102+
iconNameC,
103+
iconDescC,
79104
)
80105
}
81106

@@ -91,18 +116,26 @@ func (indicator *Indicator) SetMenu(menu *gtk.Menu) {
91116

92117
// SetIcon sets icon of indicator.
93118
func (indicator *Indicator) SetIcon(iconName string) {
119+
iconNameC := C.CString(iconName)
120+
defer C.free(unsafe.Pointer(iconNameC))
121+
94122
C.app_indicator_set_icon(
95123
indicator.indicator,
96-
C.CString(iconName),
124+
iconNameC,
97125
)
98126
}
99127

100128
// SetIconFull sets icon of indicator with description.
101129
func (indicator *Indicator) SetIconFull(iconName, iconDesc string) {
130+
iconNameC := C.CString(iconName)
131+
defer C.free(unsafe.Pointer(iconNameC))
132+
iconDescC := C.CString(iconDesc)
133+
defer C.free(unsafe.Pointer(iconDescC))
134+
102135
C.app_indicator_set_icon_full(
103136
indicator.indicator,
104-
C.CString(iconName),
105-
C.CString(iconDesc),
137+
iconNameC,
138+
iconDescC,
106139
)
107140
}
108141

@@ -111,28 +144,38 @@ func (indicator *Indicator) SetIconFull(iconName, iconDesc string) {
111144
// Second parameter "guide" is used to set maximum width for label.
112145
// Don't know if it works. Feel free to pass empty string.
113146
func (indicator *Indicator) SetLabel(label, guide string) {
147+
labelC := C.CString(label)
148+
defer C.free(unsafe.Pointer(labelC))
149+
guideC := C.CString(guide)
150+
defer C.free(unsafe.Pointer(guideC))
151+
114152
C.app_indicator_set_label(
115153
indicator.indicator,
116-
C.CString(label),
117-
C.CString(guide),
154+
labelC,
155+
guideC,
118156
)
119157
}
120158

121159
// SetIconThemePath sets icon theme path of indicator.
122160
func (indicator *Indicator) SetIconThemePath(iconThemePath string) {
161+
iconThemePathC := C.CString(iconThemePath)
162+
defer C.free(unsafe.Pointer(iconThemePathC))
163+
123164
C.app_indicator_set_icon_theme_path(
124165
indicator.indicator,
125-
C.CString(iconThemePath),
166+
iconThemePathC,
126167
)
127168
}
128169

129170
// SetOrderingIndex sets ordering index of indicator.
130171
//
131172
// It may or may not work.
132173
func (indicator *Indicator) SetOrderingIndex(orderingIndex uint) {
174+
orderingIndexC := C.guint(orderingIndex)
175+
133176
C.app_indicator_set_ordering_index(
134177
indicator.indicator,
135-
C.guint(orderingIndex),
178+
orderingIndexC,
136179
)
137180
}
138181

@@ -147,9 +190,12 @@ func (indicator *Indicator) SetSecondaryActivateTarget(menuItem *gtk.MenuItem) {
147190

148191
// SetTitle sets title of indicator.
149192
func (indicator *Indicator) SetTitle(title string) {
193+
titleC := C.CString(title)
194+
defer C.free(unsafe.Pointer(titleC))
195+
150196
C.app_indicator_set_title(
151197
indicator.indicator,
152-
C.CString(title),
198+
titleC,
153199
)
154200
}
155201

@@ -294,9 +340,14 @@ func (indicator *Indicator) GetSecondaryActivateTarget() *gtk.MenuItem {
294340
//
295341
// To be honest I don't know how it works.
296342
func (indicator *Indicator) BuildMenuFromDesktop(desktopFile, desktopProfile string) {
343+
desktopFileC := C.CString(desktopFile)
344+
defer C.free(unsafe.Pointer(desktopFileC))
345+
desktopProfileC := C.CString(desktopProfile)
346+
defer C.free(unsafe.Pointer(desktopProfileC))
347+
297348
C.app_indicator_build_menu_from_desktop(
298349
indicator.indicator,
299-
C.CString(desktopFile),
300-
C.CString(desktopProfile),
350+
desktopFileC,
351+
desktopProfileC,
301352
)
302353
}

0 commit comments

Comments
 (0)