Skip to content

Fix/reference leak #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/quickjs/ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,8 @@ void jsFreeRuntime(
final referenceleak = <String>[];
final opaque = runtimeOpaques[rt];
if (opaque != null) {
while (true) {
final ref = opaque._ref.firstWhereOrNull((ref) => ref is JSRefLeakable);
if (ref == null) break;
while (opaque._ref.isNotEmpty) {
final ref = opaque._ref.first;
ref.destroy();
runtimeOpaques[rt]?._ref.remove(ref);
}
Expand Down
38 changes: 37 additions & 1 deletion test/flutter_js_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:flutter_js/extensions/fetch.dart';
import 'package:flutter_js/extensions/xhr.dart';
import 'package:flutter_js/flutter_js.dart';
import 'package:flutter_test/flutter_test.dart';

Expand All @@ -7,7 +9,7 @@ void main() {
late JavascriptRuntime jsRuntime;

setUp(() {
jsRuntime = getJavascriptRuntime();
jsRuntime = getJavascriptRuntime(xhr: true);
});

tearDown(() {
Expand All @@ -24,4 +26,38 @@ void main() {
expect(result.rawResult, equals(125));
expect(result.stringResult, equals('125'));
});

test('leak test', () async {
final jsRt = getJavascriptRuntime();
jsRt.evaluate('''
delay = (delayInms) => {
return new Promise((resolve) => setTimeout(resolve, delayInms));
}
''');
jsRt.evaluate('''
async function asyncTest(del = 30) {
try {
console.log(`Starting \$\{del\}...`);
while (del > 0) {
console.log(del);
await delay(1000);
del--;
}
console.log(`Done \$\{del\}`);
return `Done \$\{del\}`;
} catch (e) {
console.log(`Error in asyncTest: \$\{e\}`);
return "Error";
}
}
''');
await jsRt.enableFetch();
jsRt.enableHandlePromises();
jsRt.enableXhr();
final promise = await jsRt.evaluateAsync('asyncTest(2)');
jsRt.executePendingJob();
JsEvalResult asyncResult = await jsRt.handlePromise(promise);
print('${asyncResult.stringResult}, ${asyncResult.stringResult}');
jsRt.dispose();
});
}