Repository: godot
Issue: When using Windows 10 passing the root node of a scene to a GDNative method causes a reference leak and a silent crash on shutdown
Labels: bug,topic:gdextension,crash

### Godot version

v3.5.beta5.official [815f7fe63]

### System information

Windows 10, GLES 3, RTX 2070

### Issue description

If you pass the root node of a scene to a GDNative DLL using GDScript then the scene will crash silently on exit after it receives the `NOTIFICATION_WM_QUIT_REQUEST` notification but before it receives `NOTIFICATION_EXIT_WORLD`.  This crash also causes the Windows Compatibility Layer to flag Godot as unsafe to unload DLLs and so all future calls to `FreeLibrary()` are ignored by the operating system.

Note this happens in **all** versions of Godot 3.5.

### Steps to reproduce

If you have a GDNative object that looks like so:

```
class MyObject: public godot::Reference
{
	GODOT_CLASS(MyObject, godot::Reference);
public:
	void _init();
	static void _register_methods();

	void myFunc(godot::Spatial *mySpatial); // Register this
};

void MyObjec::myFunc(godot::Spatial *mySpatial)
{
	// Leave this empty.  It's just the call that's needed
}
```

Now:

1. create a scene with a root `Spatial` and give that node a GDScript script.  In that script's `_ready():` function create a `MyObject` and call it's `myFunc()` passing in `self`.  Be aware that this function can be completely empty to reproduce the error.
2. Run the project to trigger the code in `_ready():`, then close the window that was launched to quit it.
3. You will notice that you only get `NOTIFICATION_WM_QUIT_REQUEST`, then the window appears frozen for anything between a quarter second and minute.
4. You will notice that you receive **none** of the other messages you would expect, ie.
   1. `NOTIFICATION_EXIT_WORLD`
   2. `NOTIFICATION_EXIT_TREE`
   3. `NOTIFICATION_UNPARENTED`
   4. `NOTIFICATION_PREDELETE`
5. Close the Godot Editor
6. Open the Registry Editor and go to `Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers`.
7. You should see a new key there that looks something like this but with your own path: `C:\Program Files\GodotEngine\Godot_v3.5-beta4_win64.exe = $ IgnoreFreeLibrary<GDNative.dll>`.
8. This key was created by the Windows compatibility layer because the error at step 4 happened, but after you close the Godot Editor in step 5.
9. While this key is present in the registry any calls made by Godot to the Windows API `FreeLibrary()` will be ignored by the operating system.  From now on you can't recompile your GDNative DLL while the Godot Editor is found at the location given in the registry entry.
10. To reenabled recompiling the GDNative DLL while the editor is still running delete the registry entry and then restart your computer.
11. If you change the code in step one to rather pass in a child of the root node to the empty function instead of the root node itself none of the above mentioned errors happen and all expected shutdown messages are received.

My guess as to what is happening is that
1. When you pass the root node to the GDNative method I am guessing that a reference loop is being created and retained.  This means that the system can't properly delete the root node on exit which explains why no notifications are received after `NOTIFICATION_WM_QUIT_REQUEST`.
2. At some point despite this `FreeLibrary()` is called causing a silent crash which is noticed by the Windows 10 compatibility system which then flags the executable as being suspect by creating the registry entry.
3. From this point on, as long as that registry entry exists, all calls to `FreeLibrary()` by Godot are ignored by the operating system.
4. The side effect that brought this to my attention is that while the Godot Editor is open you cannot recompile and replace the GDNative DLL.

### Minimal reproduction project

_No response_