Skip to content

Commit cef0650

Browse files
authored
Memory Leak Fixes: Release CFArrayRef and App Instances (#43)
* fix mem leaks * Remove dstore file
1 parent 5e114c6 commit cef0650

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

lib/macos.mm

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,41 @@ bool _requestAccessibility(bool showDialog) {
2828

2929
for (NSDictionary *info in (NSArray *)windowList) {
3030
NSNumber *windowNumber = info[(id)kCGWindowNumber];
31-
31+
3232
if ([windowNumber intValue] == handle) {
33+
CFRelease(windowList);
3334
return info;
3435
}
3536
}
3637

38+
CFRelease(windowList);
3739
return NULL;
3840
}
3941

4042
AXUIElementRef getAXWindow(int pid, int handle) {
4143
auto app = AXUIElementCreateApplication(pid);
4244

43-
NSArray *windows;
44-
AXUIElementCopyAttributeValues(app, kAXWindowsAttribute, 0, 100, (CFArrayRef *) &windows);
45+
CFArrayRef windows;
46+
AXUIElementCopyAttributeValues(app, kAXWindowsAttribute, 0, 100, &windows);
4547

46-
for (id child in windows) {
48+
for (id child in (NSArray *)windows) {
4749
auto window = (AXUIElementRef) child;
4850

4951
CGWindowID windowId;
5052
_AXUIElementGetWindow(window, &windowId);
5153

5254
if (windowId == handle) {
55+
CFRelease(windows);
5356
return window;
5457
}
5558
}
5659

60+
CFRelease(windows);
5761
return NULL;
5862
}
5963

6064
void cacheWindow(int handle, int pid) {
61-
if (_requestAccessibility(false)) {
65+
if (_requestAccessibility(false)) {
6266
if (windowsMap.find(handle) == windowsMap.end()) {
6367
windowsMap[handle] = getAXWindow(pid, handle);
6468
}
@@ -115,6 +119,7 @@ AXUIElementRef getAXWindowById(int handle) {
115119
arr[i] = vec[i];
116120
}
117121

122+
CFRelease(windowList);
118123
return arr;
119124
}
120125

@@ -130,11 +135,17 @@ AXUIElementRef getAXWindowById(int handle) {
130135

131136
auto app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]];
132137

133-
if (![app isActive]) continue;
138+
if (![app isActive]) {
139+
[app release];
140+
continue;
141+
}
134142

143+
[app release];
144+
CFRelease(windowList);
135145
return Napi::Number::New(env, [windowNumber intValue]);
136-
}
146+
}
137147

148+
CFRelease(windowList);
138149
return Napi::Number::New(env, 0);
139150
}
140151

@@ -147,17 +158,18 @@ AXUIElementRef getAXWindowById(int handle) {
147158

148159
if (wInfo) {
149160
NSNumber *ownerPid = wInfo[(id)kCGWindowOwnerPID];
150-
auto app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]];
161+
NSRunningApplication *app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]];
151162

152163
auto obj = Napi::Object::New(env);
153164
obj.Set("processId", [ownerPid intValue]);
154165
obj.Set("path", [app.bundleURL.path UTF8String]);
155166

156167
cacheWindow(handle, [ownerPid intValue]);
157-
168+
169+
[app release];
158170
return obj;
159171
}
160-
172+
161173
return Napi::Object::New(env);
162174
}
163175

@@ -222,7 +234,7 @@ AXUIElementRef getAXWindowById(int handle) {
222234
CFTypeRef sizeStorage = (CFTypeRef)(AXValueCreate((AXValueType)kAXValueCGSizeType, (const void *)&size));
223235
AXUIElementSetAttributeValue(win, kAXSizeAttribute, sizeStorage);
224236
}
225-
237+
226238
return Napi::Boolean::New(env, true);
227239
}
228240

@@ -305,4 +317,4 @@ AXUIElementRef getAXWindowById(int handle) {
305317
return exports;
306318
}
307319

308-
NODE_API_MODULE(addon, Init)
320+
NODE_API_MODULE(addon, Init)

0 commit comments

Comments
 (0)