Repository: godot
Issue: "ObjectDB instances leaked at exit" with specific RefCounted class _init(..) override referenced by Autoloads
Labels: bug,topic:core,topic:gdscript

### Godot version

4.0 Alpha 11 x64 (afdae67cc)

### System information

Windows 10, x86_64, Vulcan Desktop v1.2.0, Ryzen 5 1600, RTX3070 (516.59 Studio Driver)

### Issue description

Discovered an apparent memory leak and accompanying odd output when closing a running project that has a RefCounted class_name class, that contains a `Return` statement in it's `_init(..)` function override, if it is referenced in an _Autoload_ script, even as little as used as a static typing and not initialised, or if initialised in a variable.

It seems that this same leak does NOT occur if you reference the exact same RefCounted class in a node-attached script in a regular scene, it appears to have to be a node script loaded via the Autoload system. Toggling the Global Variable flag does not appear to affect the result.

Discovered this one while porting an old 3.4 project to 4.0 where I had a `Return` under an `If` condition to short-circuit the `_init(..)` function based on fed-in parameters to initialisation via `.new(..)`, and was also able to reproduce in an extremely minimal example fresh within Godot 4.0 alpha 11 x64.

I re-re-created the reproduction project in the most recent official stable release: Godot 3.4.4 x64, after reproducing the issue and it did NOT produce the same odd errors and memory leak.

Typical error output looks like as follows, rarely omits the "ObjectDB instances leaked at exit" portion (Race condition?)
Once simply cut off half way through that part of the output, although this was not easily reproduced.

- **ERROR**: Condition "_first != nullptr" is true.
    at: ~List (./core/templates/self_list.h:106)
- **ERROR**: Condition "_first != nullptr" is true.
    at: ~List (./core/templates/self_list.h:106)
- **WARNING**: ObjectDB instances leaked at exit (run with --verbose for details).
      at: cleanup (core/object/object.cpp:1918)
- **ERROR**: Resources still in use at exit (run with --verbose for details).
    at: clear (core/io/resource.cpp:474)

<img width="663" alt="Example Error output" src="https://user-images.githubusercontent.com/45514637/178973891-52d26ad0-0f61-4b6a-a679-1ec9f0321bfb.PNG">

Was able to circumvent the errors and the leak by simply removing the `Return` line from the class, and slightly restructuring the _init statement to match.

Absolutely no idea if I've somehow commited an obscure programming sin or not, but since it doesn't occur in Godot 3.4.4 with the Reference inherited type I presume it's some sort of odd regression?
Not a showstopper by itself, but might be indicitive of a deeper issue.

### Steps to reproduce

- Open Godot 4 alpha 11 via command line or the included .cmd script (to view errors later)
- Create a script with a `class_name` attribute so you can refer to it as a var type later.
- Create another script to Autoload. Add a single line to the `_ready():` function as follows: `var this_causes_errors : YourClassName` (where `YourClassName` is the name you gave to the `class_name` declaration earlier.
- Add this script to the Autoload list in Project Settings.
- Create an empty scene with a generic Node
- Run the project.
- Close the running project via the close button on the window's top bar (not via Godot Editor's STOP button), and inspect the Godot Engine command line output.

### Minimal reproduction project

[TestRefCountErrProject.zip](https://github.com/godotengine/godot/files/9111442/TestRefCountErrProject.zip)