Fix memory leak when receiving files (#1547)

This commit is contained in:
Tien Do Nam
2024-07-22 03:28:23 +02:00
committed by GitHub
parent 5406770af0
commit a8a24a2836
2 changed files with 13 additions and 1 deletions
+1
View File
@@ -1,5 +1,6 @@
## 1.15.2 (unreleased)
- fix: memory leak when receiving files, properly receive files that exceed available RAM (@Tienisto)
- fix(windows): make installer work on arm64 (@Tienisto)
## 1.15.1 (2024-07-18)
+12 -1
View File
@@ -25,7 +25,7 @@ Future<void> saveFile({
required DateTime? lastAccessed,
required void Function(int savedBytes) onProgress,
}) async {
if (!saveToGallery && androidSdkInt != null && androidSdkInt <= 29) {
if (androidSdkInt != null && androidSdkInt <= 29) {
final sdCardPath = getSdCardPath(destinationPath);
if (sdCardPath != null) {
// Use Android SAF to save the file to the SD card
@@ -45,6 +45,7 @@ Future<void> saveFile({
writeAsync: (data) async {
await _saf.writeChunk(sessionID, Uint8List.fromList(data));
},
flush: null,
close: () async {
await _saf.endWriteStream(sessionID);
},
@@ -63,6 +64,7 @@ Future<void> saveFile({
onProgress: onProgress,
write: sink.add,
writeAsync: null,
flush: sink.flush,
close: () async {
await sink.close();
if (lastModified != null) {
@@ -87,10 +89,12 @@ Future<void> _saveFile({
required void Function(int savedBytes) onProgress,
required void Function(List<int> data)? write,
required Future<void> Function(List<int> data)? writeAsync,
required Future<void> Function()? flush,
required Future<void> Function() close,
}) async {
try {
int savedBytes = 0;
int lastFlushedBytes = 0;
final stopwatch = Stopwatch()..start();
await for (final event in stream) {
if (writeAsync != null) {
@@ -104,8 +108,15 @@ Future<void> _saveFile({
stopwatch.reset();
onProgress(savedBytes);
}
const tenMB = 10 * 1024 * 1024;
if (flush != null && savedBytes >= lastFlushedBytes + tenMB) {
await flush();
lastFlushedBytes = savedBytes;
}
}
await flush?.call();
await close();
if (saveToGallery) {