Skip to content
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

[Bug]: Crash in finaliser if RealmObject implementation throws in object initialisation #3045

Closed
peppy opened this issue Oct 5, 2022 · 0 comments · Fixed by #3046
Closed
Assignees

Comments

@peppy
Copy link

peppy commented Oct 5, 2022

What happened?

Regressed in 2d2f595#diff-ffe0558165702693f2ba96b95df2b7f6bf559ca7a2ef344e9cab6af24f75f6ae (due to missing null check).

This is a bit of a caveat with NRT and pre-constructor time crashes. We get around similar issues by using a helper method to check for null (https://github.com/ppy/osu-framework/blob/7fea93e6f5a30d5a47e2bd609fe1cf7ef45c507d/osu.Framework/Extensions/ObjectExtensions/ObjectExtensions.cs#L30-L33, usage: https://github.com/ppy/osu-framework/blob/3a9acd01eac691fcbea0163038743a05bb0ba41d/osu.Framework/IO/Stores/RawCachingGlyphStore.cs#L178). Another way would be to make the _accessor nullable

With the sample code included below, a crash is observed which can halt an application that is not manually handling all unobserved exceptions:

JetBrains.Rider-EAP.2022-10-05.at.07.58.44.mp4

Repro steps

  • Have a class that throws an exception before reaching the constructor (ie. object initialisers).
  • Observe crash on first GC/finalize operation.

Version

10.16.0

What SDK flavour are you using?

Local Database only

What type of application is this?

Console/Server

Client OS and version

n/a

Code snippets

// See https://aka.ms/new-console-template for more information

using System.Diagnostics;
using Realms;

while (true)
{
    try
    {
        var model = new TestModel();
    }
    catch (Exception e)
    {
        Console.WriteLine("Caught exception");
    }
}

class TestModel : RealmObject
{
    public string Test { get; set; }

    public object ThisWillThrowBeforeConstruction = new ThrowingTest();

    internal class ThrowingTest
    {
        public ThrowingTest()
        {
            throw new Exception("die in ctor");
        }
    }
}

Stacktrace of the exception/crash you're getting

System.NullReferenceException: Object reference not set to an instance of an object.
   at Realms.RealmObjectBase.UnsubscribeFromNotifications()
   at Realms.RealmObjectBase.Finalize()

Relevant log output

No response

@nirinchev nirinchev assigned elle-j and unassigned papafe Oct 5, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants