C++ crash when deleting file after unload from LoadLibrary (custom deleyloading) Help

Code Wanderer 396 Reputation points
2020-08-29T18:16:00.293+00:00

I unpack dll files from application (when it start) and use load helper for delayloading. That means, I load dll with LoadLibrary from helper function. After app exit I want delete the extracted dlls from app. I unload dll with helper function __FUnloadDelayLoadedDLL2

Everything is working until it want delete directory with extracted dlls app crash and unfortunately I have no clue what it cause. I guess it is due app still hold loaded dll.

here is code:

void res_manager::end()
{
    if (__FUnloadDelayLoadedDLL2("guiInterop.dll") == TRUE)
    {
        cout << "guiInterop.dll unloaded\n"; // dll is unloaded successfully
    }
    else cout << "guiInterop.dll unload FAILED.\n";

    if (!m_fullPath.empty())
    {
        if (filesystem::exists(m_fullPath))
        {
            if (filesystem::remove_all(m_fullPath)) // this crash app
            {
                cout << "Files removed successfully.\n";
            }
            else
            {
                cout << "Files not removed or not exists.\n";
            }
        }
        else
        {
            cout << "Direcotry not exists.\n";
        }
    }
}
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
1,895 questions
No comments
{count} votes

2 answers

Sort by: Most helpful
  1. RLWA32 28,656 Reputation points
    2020-08-29T20:24:09.473+00:00

    The code calls filesystem::remove_all even if the attempt to unload the dll fails.

    In this case, filesystem::remove_all will throw an exception. If this exception is not caught then the application will terminate. Alternatively, you can call the non-throwing version -- remove_all
    Also, the non-throwing version does not return 0 upon failure so the if statement needs revision if you decide to use it.


  2. RLWA32 28,656 Reputation points
    2020-08-31T04:16:09.877+00:00

    The application uses CreateProcess to start a helper process that will delete the mixed-mode assembly after the application exits. In the following example, the application has passed its own process id and the path to the mixed-mode assembly to the helper process as command line parameters.

    Example helper process (error checking omitted) -
    21468-delhelper.png