Skip to content

Commit 851e2d5

Browse files
committed
Add null checks, and improve swig error handling
1 parent c11a478 commit 851e2d5

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

messaging/src/FirebaseNotification.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ namespace Firebase.Messaging {
2222
/// implementation.
2323
public sealed class AndroidNotificationParams {
2424
internal static AndroidNotificationParams FromInternal(AndroidNotificationParamsInternal other) {
25+
if (other == null) return null;
26+
2527
AndroidNotificationParams android = new AndroidNotificationParams();
2628
android.ChannelId = other.channel_id;
2729
return android;
@@ -45,6 +47,8 @@ public void Dispose(bool disposing) { }
4547
/// library.
4648
public sealed class FirebaseNotification {
4749
internal static FirebaseNotification FromInternal(FirebaseNotificationInternal other) {
50+
if (other == null) return null;
51+
4852
FirebaseNotification notification = new FirebaseNotification();
4953
notification.Android = AndroidNotificationParams.FromInternal(other.android);
5054
notification.Badge = other.badge;

messaging/src/swig/messaging.i

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,18 +316,22 @@ void SendPendingEvents() {
316316
// messageReceivedDelegate.
317317
[MonoPInvokeCallback(typeof(MessageReceivedDelegate))]
318318
private static int MessageReceivedDelegateMethod(System.IntPtr message) {
319+
int tookOwnership = 0;
319320
return ExceptionAggregator.Wrap(() => {
320321
// Use a local copy so another thread cannot unset this before we use it.
321322
var handler = FirebaseMessagingInternal.MessageReceivedInternal;
322323
if (handler != null) {
324+
// Take ownership, and track it so that the caller of this knows, even
325+
// if an exception is thrown, since the C# object will still delete it.
323326
FirebaseMessageInternal messageInternal = new FirebaseMessageInternal(message, true);
327+
tookOwnership = 1;
324328
handler(null, new Firebase.Messaging.MessageReceivedEventArgs(
325329
FirebaseMessage.FromInternal(messageInternal)));
326330
messageInternal.Dispose();
327331
return 1;
328332
}
329333
return 0;
330-
}, 0);
334+
}, tookOwnership);
331335
}
332336

333337
// Called from ListenerImpl::TokenReceived() via the

0 commit comments

Comments
 (0)