mcpiroman / UnityNativeTool

Allows to unload native plugins in Unity3d editor
MIT License
183 stars 18 forks source link

Update unlock dll file #36

Closed tylearymf closed 2 years ago

mcpiroman commented 2 years ago

OK, so why should we try to FreeLibrary for 2 seconds on Windows? I have not met any problems with single try.

tylearymf commented 2 years ago

2 seconds is to prevent Unity from getting stuck. This submission is to solve the problem that the dll file cannot be replaced after unloading, and Unity prompts that the file is occupied.

tylearymf commented 2 years ago

Because when the reference count of the dll object in the system is greater than 0, the file handle will not be released. You can see this:freelibrary

The reference count is decremented each time the FreeLibrary or FreeLibraryAndExitThread function is called for the module. When a module's reference count reaches zero or the process terminates, the system unloads the module from the address space of the process. Before unloading a library module, the system enables the module to detach from the process by calling the module's DllMain function, if it has one, with the DLL_PROCESS_DETACH value. Doing so gives the library module an opportunity to clean up resources allocated on behalf of the current process. After the entry-point function returns, the library module is removed from the address space of the current process.

tylearymf commented 2 years ago

What I want is to replace the dll file without closing Unity after the dll unload.

mcpiroman commented 2 years ago

What I want is to replace the dll file without closing Unity after the dll unload.

Yes, ofc, but me and others were able to do that without your change.

Because when the reference count of the dll object in the system is greater than 0, the file handle will not be released.

Yes, I know, but because any given dll is loaded once, it's ref count is not greater than 1. Unless it is loaded in some other way, either manually with 'LoadLibrary', or likely in some unexpected way by Unity.

I think we should first investigate why the upstream way does not work for you. Do you have some additional assemblies? Do you have additional threads, coroutines? Maybe can you confirm this problem exists with some basic hello world project?

tylearymf commented 2 years ago

Projects.zip

Please help me. Open the Unity project (Projects/TestDll_Unity), then open the Scenes/SampleScene.unity scene, and then the Plugins/Test.Dll at this time is locked and cannot be deleted or replaced.

mcpiroman commented 2 years ago

Please open an issue describing your problem and the steps you follow, I'll close this PR for now.

tylearymf commented 2 years ago

Ok, thank you. Maybe I tried it the wrong way