Repository: godot
Issue: Inconsistent crashing using get_var with custom classes.
Labels: bug,topic:core,needs testing,crash

### Godot version

v3.4.4.stable.official [419e713a2]

### System information

Windows 10

### Issue description

I'm trying to use File.store_var and File.get_var to save and load data in a custom class. I've tried different combinations of what the class inherits and where it is defined (it was suggested in [this](https://www.reddit.com/r/godot/comments/u89lud/odd_crash_behaviour_following_get_var_with_a/) thread that inner classes were the problem), and am getting some strange results and crashes. (Sorry if this should be multiple issues - I'm not sure to what extent the causes of the behaviours are related.)

The class is of the form

```
class_name DataHolder
inherits X

export(String) var data:String
```
and generally, a crash can be caused by attempting to access a loaded instance of `data_holder`, without even trying to access `data`*. The crashes do not occur when the data holder is accessed in `_ready`, but on later frames. Pausing execution (e.g. with a breakpoint) after the data holder is loaded inside `_ready` will consistently cause the crash, however.

\* Except when the class is defined in a separate file and inherits Node, but this presents other issues.

The behaviours are as follows, according to what the DataHolder class inherits, and where it's defined:

***
**No inheritence, internally defined:** Usually silently crashes within the first couple of frames, but may survive longer.
***
**No inheritence, externally defined:** Crashes similarly.
***
**Inherits node, internally defined:** Gives error `Script inherits from native type 'Reference', so it can't be instanced in object of type: 'Node'`, which I think makes sense as I'm trying to use `.new()`. Also, it gives me the error

```
ERROR: Resource file not found: res://.
   at: (core/io/resource_loader.cpp:275)
```

in the console, which will show every time the project is run until the project is reloaded. (Checking resource_loader.cpp:275 shows that the dot at the end is just formatting - the path passed is empty.)
***
**Inherits node, externally defined:** No longer gives the error about the script inheriting from reference, even though I'm using `.new()` on a Node. No crashes observed, and can even access `data_holder.data`.

But now when I close the program (by clicking X in the corner) I get the following in the system console only (and not in the editor's console):

```
ERROR: Condition "_first != nullptr" is true.
   at: ~List (./core/self_list.h:108)
ERROR: Condition "_first != nullptr" is true.
   at: ~List (./core/self_list.h:108)
WARNING: ObjectDB instances leaked at exit (run with --verbose for details).
     at: cleanup (core/object.cpp:2064)
ERROR: Resources still in use at exit (run with --verbose for details).
   at: clear (core/resource.cpp:417)
```

Running an _exported_ version with --verbose, I now _do_ get the `Script inherits from native type 'Reference', so it can't be instanced in object of type: 'Node'` error (similarly to #49851). This error seems to be thrown when get_var is called, not when I first call `.new()`. The verbose bit at the end is:

```
    ERROR: Condition "_first != nullptr" is true.
       at: ~List (./core/self_list.h:108)
    ERROR: Condition "_first != nullptr" is true.
       at: ~List (./core/self_list.h:108)
    WARNING: ObjectDB instances leaked at exit (run with --verbose for details).
       at: cleanup (core/object.cpp:2064)
    Leaked instance: Node:1183 - Node name:
    Leaked instance: Node:1186 - Node name:
    Leaked instance: GDScriptNativeClass:554
    Leaked instance: GDScriptNativeClass:595
    Leaked instance: GDScript:1187 - Resource path:
    Leaked instance: GDScript:1176 - Resource path: res://DataHolder.gd
    Hint: Leaked instances typically happen when nodes are removed from the scene tree (with `remove_child()`) but not freed (with `free()` or `queue_free()`).
    ERROR: Resources still in use at exit (run with --verbose for details).
       at: clear (core/resource.cpp:417)
    Resource still in use: res://DataHolder.gd (GDScript)
    Orphan StringName: data
    Orphan StringName: Reference
    Orphan StringName: GDScriptNativeClass
    Orphan StringName: Node
    Orphan StringName: _init
    Orphan StringName: GDScript
    Orphan StringName: res://DataHolder.gd
    StringName: 7 unclaimed string names at exit.
```
***
**Inherits resource, internally or externally defined:** Crashes inconsistently.

***
***
>  how do you expect it to work instead?

I don't know if any of what I was trying to do (initially, the externally-defined Resource-inheriting class) makes sense, but at the very least some sort of error message would be useful.

### Steps to reproduce

Fundamentally, use `File.store_var` and `File.get_var` to save then load an instance of a custom class. Then, in a later frame, try to access that class or a property of it. See minimal reproduction project for more detail.

### Minimal reproduction project

`test_mode` and `try_access_data` can be used to test the differerent cases.
[store_var crash minimal.zip](https://github.com/godotengine/godot/files/8534433/store_var.crash.minimal.zip)