Hi !
I have memory leaks in this code, but I don't know how I could fix them.
Can you help me ?
DWORD WINAPI D3D11Rendering::texturesLoading(LPVOID lpParam)
{
LoadingThreadsParameters* parameters = (LoadingThreadsParameters*)lpParam;
for (unsigned short i = parameters->beginning; i <= parameters->end; i++)
{
string textureName(objectsData[i].textureName);
//_ASSERT(i != 236);
if (textureName.length() > 1)
{
string textureDirectory("./Assets/Textures/");
textureName.replace(textureName.length() - 4, 4, ".jpg");
string textureFilePath = textureDirectory + textureName;
// Load the texture and initialize an ID3D11Texture2D object.
HRESULT result = D3DX11CreateTextureFromFile(deviceDX11, textureFilePath.c_str(), NULL, NULL, (ID3D11Resource **)&textureData[i], NULL);
D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
// Fill in the D3D11_SHADER_RESOURCE_VIEW_DESC structure.
shaderResourceViewDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;// textureDesc.Format;
shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
shaderResourceViewDesc.Texture2D.MipLevels = 1;
textureSRV.resize(Loader3ds::nbObj+1);
normalMapSRV.resize(Loader3ds::nbObj + 1);
// Create the shader resource view.
HRESULT hResult = deviceDX11->CreateShaderResourceView((ID3D11Resource*)textureData[i], &shaderResourceViewDesc, &textureSRV[i]);
textureData[i]->Release();
textureData[i] = NULL;
if (strstr(objectsData[i].textureName, "SKY")!=0)
backgroundIndex = i;
//Loading of the normal maps
if ( ( strstr(textureFilePath.c_str(), "backgroun") == 0 ) /*|| (strstr(textureFilePath.c_str(), "badTree") == 0 )*/)
{
string textureDirectory("./Assets/Textures/");
textureName.replace(textureName.length() - 4, 4, "");
string textureFilePath = textureDirectory + textureName;
textureFilePath += string("_NormalMap.png");
// Load the texture and initialize an ID3D11Texture2D object.
HRESULT result = D3DX11CreateTextureFromFile(deviceDX11, textureFilePath.c_str(), NULL, NULL, (ID3D11Resource**)&normalMapTexture[i], NULL);
D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
// Fill in the D3D11_SHADER_RESOURCE_VIEW_DESC structure.
shaderResourceViewDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;// textureDesc.Format;
shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
shaderResourceViewDesc.Texture2D.MipLevels = 1;
//textureSRV.resize(Loader3ds::nbObj + 1);
//normalMapSRV.resize(Loader3ds::nbObj + 1);
// Create the shader resource view.
HRESULT hResult = deviceDX11->CreateShaderResourceView((ID3D11Resource*)normalMapTexture[i], &shaderResourceViewDesc, &normalMapSRV[i]);
// normalMapTexture[i]->Release();
// normalMapTexture[i] = NULL;
MemoryUtilities::SafeDeleteD3Dobject(normalMapTexture[i]);
}
}
}
_CrtDumpMemoryLeaks();
/*MemoryUtilities::SafeDelete(lpParam);
MemoryUtilities::SafeDelete(parameters);*/
return 0;
}
void D3D11Rendering::parallelizeTextureLoading()
{
LoadingThreadsParameters loadingThreadsParameters0;
LoadingThreadsParameters loadingThreadsParameters1;
LoadingThreadsParameters loadingThreadsParameters2;
LoadingThreadsParameters loadingThreadsParameters3;
LoadingThreadsParameters loadingThreadsParameters4;
LoadingThreadsParameters loadingThreadsParameters5;
LoadingThreadsParameters loadingThreadsParameters6;
LoadingThreadsParameters loadingThreadsParameters7;
SetThreadPriority(handleLoadingTexturesThread[0], THREAD_PRIORITY_TIME_CRITICAL);
SetThreadPriority(handleLoadingTexturesThread[1], THREAD_PRIORITY_TIME_CRITICAL);
SetThreadPriority(handleLoadingTexturesThread[2], THREAD_PRIORITY_TIME_CRITICAL);
SetThreadPriority(handleLoadingTexturesThread[3], THREAD_PRIORITY_TIME_CRITICAL);
SetThreadPriority(handleLoadingTexturesThread[4], THREAD_PRIORITY_TIME_CRITICAL);
SetThreadPriority(handleLoadingTexturesThread[5], THREAD_PRIORITY_TIME_CRITICAL);
SetThreadPriority(handleLoadingTexturesThread[6], THREAD_PRIORITY_TIME_CRITICAL);
SetThreadPriority(handleLoadingTexturesThread[7], THREAD_PRIORITY_TIME_CRITICAL);
loadingThreadsParameters0.beginning = 0;
loadingThreadsParameters0.end = nbObj / 8;
handleLoadingTexturesThread[0] = CreateThread(
NULL, // default security attributes
0, // use default stack size
&D3D11Rendering::texturesLoading, // thread function name
&loadingThreadsParameters0, // argument to thread function
0, // use default creation flags
0);
loadingThreadsParameters1.beginning = (nbObj / 8)+1;
loadingThreadsParameters1.end = (nbObj/8)*2;
handleLoadingTexturesThread[1] = CreateThread(
NULL, // default security attributes
0, // use default stack size
&D3D11Rendering::texturesLoading, // thread function name
&loadingThreadsParameters1, // argument to thread function
0, // use default creation flags
0);
loadingThreadsParameters2.beginning = ((nbObj / 8) * 2)+1;
loadingThreadsParameters2.end = (nbObj / 8) * 3;
handleLoadingTexturesThread[2] = CreateThread(
NULL, // default security attributes
0, // use default stack size
&D3D11Rendering::texturesLoading, // thread function name
&loadingThreadsParameters2, // argument to thread function
0, // use default creation flags
0);
loadingThreadsParameters3.beginning = ((nbObj / 8) * 3) +1 ;
loadingThreadsParameters3.end = (nbObj / 8) * 4;
handleLoadingTexturesThread[3] = CreateThread(
NULL, // default security attributes
0, // use default stack size
&D3D11Rendering::texturesLoading, // thread function name
&loadingThreadsParameters3, // argument to thread function
0, // use default creation flags
0);
loadingThreadsParameters4.beginning = ((nbObj / 8) * 4)+1;
loadingThreadsParameters4.end = (nbObj / 8) * 5;
handleLoadingTexturesThread[4] = CreateThread(
NULL, // default security attributes
0, // use default stack size
&D3D11Rendering::texturesLoading, // thread function name
&loadingThreadsParameters4, // argument to thread function
0, // use default creation flags
0);
loadingThreadsParameters5.beginning = ((nbObj / 8) * 5)+1;
loadingThreadsParameters5.end = (nbObj / 8) * 6;
handleLoadingTexturesThread[5] = CreateThread(
NULL, // default security attributes
0, // use default stack size
&D3D11Rendering::texturesLoading, // thread function name
&loadingThreadsParameters5, // argument to thread function
0, // use default creation flags
0);
loadingThreadsParameters6.beginning = ((nbObj / 8) * 6)+1;
loadingThreadsParameters6.end = (nbObj / 8) * 7;
handleLoadingTexturesThread[6] = CreateThread(
NULL, // default security attributes
0, // use default stack size
&D3D11Rendering::texturesLoading, // thread function name
&loadingThreadsParameters6, // argument to thread function
0, // use default creation flags
0);
loadingThreadsParameters7.beginning = ((nbObj / 8) * 7)+1;
loadingThreadsParameters7.end = (nbObj);
handleLoadingTexturesThread[7] = CreateThread(
NULL, // default security attributes
0, // use default stack size
&D3D11Rendering::texturesLoading, // thread function name
&loadingThreadsParameters7, // argument to thread function
0, // use default creation flags
0);
WaitForMultipleObjects(8, handleLoadingTexturesThread, TRUE, INFINITE);
CloseHandle(handleLoadingTexturesThread[0]);
CloseHandle(handleLoadingTexturesThread[1]);
CloseHandle(handleLoadingTexturesThread[2]);
CloseHandle(handleLoadingTexturesThread[3]);
CloseHandle(handleLoadingTexturesThread[4]);
CloseHandle(handleLoadingTexturesThread[5]);
CloseHandle(handleLoadingTexturesThread[6]);
CloseHandle(handleLoadingTexturesThread[7]);
}
To free memory, I use this :
template <typename C>
static void SafeDeleteD3Dobject(C& pointer)
{
if (pointer > 0x0000000000000000)
{
pointer->Release();
pointer = nullptr;
}
}