Skip to content

Commit 47c083b

Browse files
committed
Fix memory management for tray icon title and tooltip
Replaces previous pointer allocation and freeing logic with correct usage of ffi.malloc.free for title and tooltip setters. Ensures proper memory deallocation and prevents potential memory leaks when setting tray icon properties.
1 parent f643c90 commit 47c083b

1 file changed

Lines changed: 26 additions & 10 deletions

File tree

packages/nativeapi/lib/src/tray_icon.dart

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,19 @@ class TrayIcon
165165
}
166166

167167
set title(String? title) {
168-
final titlePtr = title != null
169-
? title.toNativeUtf8().cast<Char>()
170-
: nullptr;
171-
bindings.native_tray_icon_set_title(_nativeHandle, titlePtr);
172-
bindings.free_c_str(titlePtr);
168+
if (title != null) {
169+
final titleUtf8 = title.toNativeUtf8();
170+
try {
171+
bindings.native_tray_icon_set_title(
172+
_nativeHandle,
173+
titleUtf8.cast<Char>(),
174+
);
175+
} finally {
176+
ffi.malloc.free(titleUtf8);
177+
}
178+
} else {
179+
bindings.native_tray_icon_set_title(_nativeHandle, nullptr);
180+
}
173181
}
174182

175183
String? get tooltip {
@@ -180,11 +188,19 @@ class TrayIcon
180188
}
181189

182190
set tooltip(String? tooltip) {
183-
final tooltipPtr = tooltip != null
184-
? tooltip.toNativeUtf8().cast<Char>()
185-
: nullptr;
186-
bindings.native_tray_icon_set_tooltip(_nativeHandle, tooltipPtr);
187-
ffi.calloc.free(tooltipPtr);
191+
if (tooltip != null) {
192+
final tooltipUtf8 = tooltip.toNativeUtf8();
193+
try {
194+
bindings.native_tray_icon_set_tooltip(
195+
_nativeHandle,
196+
tooltipUtf8.cast<Char>(),
197+
);
198+
} finally {
199+
ffi.malloc.free(tooltipUtf8);
200+
}
201+
} else {
202+
bindings.native_tray_icon_set_tooltip(_nativeHandle, nullptr);
203+
}
188204
}
189205

190206
Menu? get contextMenu {

0 commit comments

Comments
 (0)