I was going through the dynamic code loading episodes. Using the load counter seemed to work perfectly fine. However when I switched to using the last write time I get an issue where most of the time the CopyFile call fails. It sometimes doesn't however. Basically when I load up my program, try to change something and compile, it is completely random whether the CopyFile function fails. It could succeed the first time I try to recompile, or it could take like 7 times of me trying to recompile, and then work. I recently switched to a much faster machine, I don't remember having a problem on my old machine. I don't know if that has anything to do with it. I tried to use GetLastError to see how CopyFile is failing and it shows ERROR_SHARING_VIOLATION. I'm not sure if CompareFileTime somehow locks the file or if it's and issue with the FindFirstFile. I'm just clueless as to what is locking the file randomly.
The Code is being loaded in winmain like this its basically the way of day 22
FILETIME NewDLLWriteTime = Win32GetLastWriteTime(DllName);
if(CompareFileTime(&NewDLLWriteTime, &GameCode.DllLastWriteTime) != 0)
{
Win32UnloadGameCode(&GameCode);
GameCode = Win32LoadGameCode(DllName);
}
These are all the function calls
inline FILETIME Win32GetLastWriteTime(char* DllName)
{
FILETIME LastWriteTime = {};
WIN32_FIND_DATA FindData;
HANDLE FindHandle = FindFirstFileA(DllName, &FindData);
if(FindHandle != INVALID_HANDLE_VALUE)
{
LastWriteTime = FindData.ftLastWriteTime;
FindClose(FindHandle);
}
return LastWriteTime;
}
internal win32_game_code Win32LoadGameCode(char* DllName)
{
win32_game_code Result = {};
char* TempDLLName = "handmade_temp.dll";
Result.DllLastWriteTime = Win32GetLastWriteTime(DllName);
CopyFileA(DllName, TempDLLName, FALSE);
DWORD Error = GetLastError();
Result.GameCodeDLL = LoadLibraryA(TempDLLName);
Assert(Result.GameCodeDLL != NULL)
if(Result.GameCodeDLL)
{
Result.UpdateAndRender = (game_update_and_render*)GetProcAddress(Result.GameCodeDLL, "GameUpdateAndRender");
Result.GetSoundSamples = (game_get_sound_samples*)GetProcAddress(Result.GameCodeDLL, "GameGetSoundSamples");
Result.IsValid = (Result.UpdateAndRender && Result.GetSoundSamples);
}
if(!Result.IsValid)
{
Result.UpdateAndRender = GameUpdateAndRenderStub;
Result.GetSoundSamples = GameGetSoundSamplesStub;
}
return Result;
}
internal void Win32UnloadGameCode(win32_game_code* GameCode)
{
if(GameCode->GameCodeDLL)
{
Assert(FreeLibrary(GameCode->GameCodeDLL));
}
GameCode->IsValid = false;
GameCode->UpdateAndRender = GameUpdateAndRenderStub;
GameCode->GetSoundSamples = GameGetSoundSamplesStub;
}