Register
handmade.network»Forums»Problem with hot-reloading code(dll)
Roman
8 posts

Roman Shvayko.I love low-level programming and games.

Problem with hot-reloading code(dll)
1 month, 1 week ago Edited by Roman on April 26, 2020, 4:54 p.m. Reason: Initial post
Hi everyone! I try do hot-reloading as casey in early episodes but I got problem. I do unload and load dll when my project compiled but it doesn't work. If I add in main loop Sleep(~250) then it will be work. Also for me, my load and unload functions works fine but when I do changes in dll code nothing changes. If I will unload and load every frame it also will be work fine. Thanks for any help!
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
internal void unloadGameCode(win32_game_code gameCode)
{
    if(gameCode.loadGameCodeDLL)
    {
        FreeLibrary(
                    gameCode.loadGameCodeDLL
                    );
    }
    gameCode.loadGameCodeDLL = 0;
    gameCode.isValid = false;
    gameCode.updateAndRender = game_update_and_render_stub;
}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
internal win32_game_code loadGameCode(char *dllFileName)
{
    win32_game_code result = {};
    
    
    result.lastWriteTime = getLastWriteTime(dllFileName);
    s32 num = CopyFile(dllFileName,
                       "handmadeTemp.dll",
                       FALSE
                       );
    
    result.loadGameCodeDLL = LoadLibraryA("handmadeTemp.dll");
    
    if(result.loadGameCodeDLL)
    {
        result.updateAndRender = (game_update_and_render*)GetProcAddress(result.loadGameCodeDLL, "gameUpdateAndRender");
        
        if(result.updateAndRender)
        {
            result.isValid = true;
        }
    }
    
    if(!result.isValid)
    {
        result.updateAndRender = game_update_and_render_stub;
        result.loadGameCodeDLL = LoadLibraryA("handmade.dll"); 
    }
    
    return result;
}


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
while(appIsRunning)
{
FILETIME DLLnewWriteTime = getLastWriteTime(sourceDLLname);
        
        if(CompareFileTime(&DLLnewWriteTime, &game.lastWriteTime) != 0 )
        {
            unloadGameCode(game);
            game = loadGameCode(sourceDLLname);
        }
}

This if statement works fine when I debug. Really thanks for any help!
Rafael Abreu
7 posts
Problem with hot-reloading code(dll)
1 month ago Edited by Rafael Abreu on April 26, 2020, 5:24 p.m.
I don't know if this is your problem but have you seen the episode where Casey does a .lock file to fix problems with the hotloading?

If I'm not mistaken it's somewhere in the middle to end of day 39. You can see the discussion about it:
https://hero.handmade.network/for..._autoload_when_hotloading_the_dll
Roman
8 posts

Roman Shvayko.I love low-level programming and games.

Problem with hot-reloading code(dll)
1 month ago Edited by Roman on April 26, 2020, 5:44 p.m.
Exactly! It helped! Very interesting. Thanks very much for help.
But I still do not fully understand what caused the problem. As I understand it is because I do random name for pdb file? So I will watch how casey explain this
Roman
8 posts

Roman Shvayko.I love low-level programming and games.

Problem with hot-reloading code(dll)
1 month ago
I understood. It is happens because compiler first writes exe
Rafael Abreu
7 posts
Problem with hot-reloading code(dll)
1 month ago
I'm glad it helped. As for what the problem was I don't remember honestly, better to watch Casey and/or wait for someone else to come here explain. Best of luck!