Wrap freeDialogs call inside a finally block

For instance, LocalDateTime.parse could fail,
and then the dialogs would be leaked.
This commit is contained in:
Lonami Exo 2022-10-23 20:27:15 +02:00
parent 442b019134
commit 07d0e5e505
1 changed files with 25 additions and 22 deletions

View File

@ -16,31 +16,34 @@ class NativeDialogRepository : DialogRepository {
val dialogs = mutableListOf<Dialog>() val dialogs = mutableListOf<Dialog>()
val dialogPtr = Native.getDialogs() val dialogPtr = Native.getDialogs()
val dialogCount = Native.dialogCount(dialogPtr) try {
for (i in 0 until dialogCount) { val dialogCount = Native.dialogCount(dialogPtr)
dialogs.add( for (i in 0 until dialogCount) {
Dialog( dialogs.add(
id = Native.dialogPacked(dialogPtr, i), Dialog(
title = Native.dialogTitle(dialogPtr, i), id = Native.dialogPacked(dialogPtr, i),
lastMessage = MessagePreview( title = Native.dialogTitle(dialogPtr, i),
sender = Native.dialogSender(dialogPtr, i), lastMessage = MessagePreview(
text = Native.dialogText(dialogPtr, i), sender = Native.dialogSender(dialogPtr, i),
date = LocalDateTime.parse( text = Native.dialogText(dialogPtr, i),
Native.dialogTime(dialogPtr, i), date = LocalDateTime.parse(
DateTimeFormatter.ISO_OFFSET_DATE_TIME Native.dialogTime(dialogPtr, i),
DateTimeFormatter.ISO_OFFSET_DATE_TIME
),
ack = when (Native.dialogAck(dialogPtr, i)) {
0 -> MessageAck.RECEIVED
1 -> MessageAck.SENT
2 -> MessageAck.SEEN
else -> MessageAck.RECEIVED
}
), ),
ack = when (Native.dialogAck(dialogPtr, i)) { pinned = Native.dialogPin(dialogPtr, i) != 0
0 -> MessageAck.RECEIVED )
1 -> MessageAck.SENT
2 -> MessageAck.SEEN
else -> MessageAck.RECEIVED
}
),
pinned = Native.dialogPin(dialogPtr, i) != 0
) )
) }
} finally {
Native.freeDialogs(dialogPtr)
} }
Native.freeDialogs(dialogPtr)
return dialogs return dialogs
} }