Skip to content

Commit 956ac5a

Browse files
committed
Fix toNativeUtf8/Utf16 memory leak
Signed-off-by: Sunbreak <[email protected]>
1 parent 14512b3 commit 956ac5a

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

lib/src/logic_conf_linux.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ class LogicConfLinux extends LogicConfPlatform {
1919

2020
@override
2121
bool openDevice(String path) {
22-
var descriptor = _libc.open2(path.toNativeUtf8().cast(), O_RDWR);
22+
var nativeUtf8 = path.toNativeUtf8();
23+
var descriptor = _libc.open2(nativeUtf8.cast(), O_RDWR);
24+
malloc.free(nativeUtf8);
25+
2326
if (descriptor < 0) {
2427
// TODO strerror()
2528
print('open2 error');

lib/src/logic_conf_macos.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ class LogicConfMacos extends LogicConfPlatform {
2525

2626
@override
2727
bool openDevice(String path) {
28-
_entryPtr = _io.IORegistryEntryFromPath(kIOMasterPortDefault, path.toNativeUtf8().cast());
28+
var nativeUtf8 = path.toNativeUtf8();
29+
_entryPtr = _io.IORegistryEntryFromPath(kIOMasterPortDefault, nativeUtf8.cast());
30+
malloc.free(nativeUtf8);
31+
2932
if (_entryPtr == nullptr) {
3033
print('IORegistryEntryFromPath error');
3134
return false;

lib/src/logic_conf_windows.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ class LogicConfWindows extends LogicConfPlatform {
5959
// FIXME Utf16.decode
6060
var devicePath = utf8.decode(deviceInterfaceDetailDataPtr.getDevicePathData(requiredSizePtr.value));
6161

62-
devHandle = CreateFile(devicePath.toNativeUtf16(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, NULL);
62+
var nativeUtf16 = devicePath.toNativeUtf16();
63+
devHandle = CreateFile(nativeUtf16, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, NULL);
64+
malloc.free(nativeUtf16);
65+
6366
if (devHandle == INVALID_HANDLE_VALUE) {
6467
print('CreateFile error ${GetLastError()}');
6568
continue;
@@ -125,15 +128,18 @@ class LogicConfWindows extends LogicConfPlatform {
125128

126129
@override
127130
bool openDevice(String path) {
131+
var nativeUtf16 = path.toNativeUtf16();
128132
_devHandle = CreateFile(
129-
path.toNativeUtf16(),
133+
nativeUtf16,
130134
GENERIC_READ | GENERIC_WRITE,
131135
FILE_SHARE_READ | FILE_SHARE_WRITE,
132136
nullptr,
133137
OPEN_EXISTING,
134138
0,
135139
NULL,
136140
);
141+
malloc.free(nativeUtf16);
142+
137143
return _devHandle != INVALID_HANDLE_VALUE;
138144
}
139145

0 commit comments

Comments
 (0)