Browse Source

fixed some memory leaks

Kolja Strohm 1 tuần trước cách đây
mục cha
commit
98c0ba3fd3
10 tập tin đã thay đổi với 114 bổ sung38 xóa
  1. 4 1
      AbstractElement.cpp
  2. 0 2
      DLLRegister.cpp
  3. 60 3
      DX12GraphicsApi.cpp
  4. 6 0
      DX12Shader.cpp
  5. 1 0
      DX12TLAS.cpp
  6. 4 0
      DataValidator.cpp
  7. 22 27
      Font.cpp
  8. 7 4
      Font.h
  9. 1 1
      List.cpp
  10. 9 0
      Screen.cpp

+ 4 - 1
AbstractElement.cpp

@@ -8,7 +8,10 @@ ElementPath::ElementPath(RCArray<Text>* path)
       index(0)
 {}
 
-ElementPath::~ElementPath() {}
+ElementPath::~ElementPath()
+{
+    path->release();
+}
 
 bool Framework::ElementPath::isValid() const
 {

+ 0 - 2
DLLRegister.cpp

@@ -21,9 +21,7 @@ DLLRegister::~DLLRegister()
         if (tmp)
         {
             tmp->name->release();
-#ifndef _DEBUG
             FreeLibrary(tmp->handle);
-#endif
         }
         delete tmp;
     }

+ 60 - 3
DX12GraphicsApi.cpp

@@ -156,6 +156,12 @@ DirectX12::~DirectX12()
         device->Release();
         getDLLRegister()->releaseDLL("dxgi.dll");
         getDLLRegister()->releaseDLL("d3d12.dll");
+#ifdef _DEBUG
+        if (debugDX)
+        {
+            getDLLRegister()->releaseDLL("WinPixGpuCapturer.dll");
+        }
+#endif
     }
     if (debug) debug->Release();
 }
@@ -573,6 +579,10 @@ typedef HRESULT(__stdcall* DXGIGetDebugInterface1Function)(
 void DirectX12::initialize(
     NativeWindow* fenster, Vec2<int> backBufferSize, bool fullScreen)
 {
+    if (device)
+    {
+        throw "DirectX 12 wurde bereits initialisiert.";
+    }
     GraphicsApi::initialize(fenster, backBufferSize, fullScreen);
 
 #ifdef _DEBUG
@@ -592,11 +602,23 @@ void DirectX12::initialize(
             new Text("Fehler"),
             new Text("dxgi.dll konnte nicht gefunden werden."),
             MB_ICONERROR);
+#ifdef _DEBUG
+        if (debugDX)
+        {
+            getDLLRegister()->releaseDLL("WinPixGpuCapturer.dll");
+        }
+#endif
         return;
     }
     HINSTANCE d3d12DLL = getDLLRegister()->loadDLL("d3d12.dll", "d3d12.dll");
     if (!d3d12DLL)
     {
+#ifdef _DEBUG
+        if (debugDX)
+        {
+            getDLLRegister()->releaseDLL("WinPixGpuCapturer.dll");
+        }
+#endif
         getDLLRegister()->releaseDLL("dxgi.dll");
         WMessageBox(fenster->getWindowHandle(),
             new Text("Fehler"),
@@ -609,6 +631,12 @@ void DirectX12::initialize(
             d3d12DLL, "D3D12SerializeRootSignature");
     if (!pfnD3D12SerializeRootSignature)
     {
+#ifdef _DEBUG
+        if (debugDX)
+        {
+            getDLLRegister()->releaseDLL("WinPixGpuCapturer.dll");
+        }
+#endif
         getDLLRegister()->releaseDLL("dxgi.dll");
         getDLLRegister()->releaseDLL("d3d12.dll");
         WMessageBox(fenster->getWindowHandle(),
@@ -625,6 +653,12 @@ void DirectX12::initialize(
             dxgiDLL, "CreateDXGIFactory2");
     if (!createFactory)
     {
+#ifdef _DEBUG
+        if (debugDX)
+        {
+            getDLLRegister()->releaseDLL("WinPixGpuCapturer.dll");
+        }
+#endif
         getDLLRegister()->releaseDLL("dxgi.dll");
         getDLLRegister()->releaseDLL("d3d12.dll");
         WMessageBox(fenster->getWindowHandle(),
@@ -641,6 +675,12 @@ void DirectX12::initialize(
             d3d12DLL, "D3D12CreateDevice");
     if (!createDevice)
     {
+#ifdef _DEBUG
+        if (debugDX)
+        {
+            getDLLRegister()->releaseDLL("WinPixGpuCapturer.dll");
+        }
+#endif
         getDLLRegister()->releaseDLL("dxgi.dll");
         getDLLRegister()->releaseDLL("d3d12.dll");
         WMessageBox(fenster->getWindowHandle(),
@@ -723,6 +763,12 @@ void DirectX12::initialize(
         createFactoryFlags, __uuidof(IDXGIFactory6), (void**)&factory);
     if (FAILED(res))
     {
+#ifdef _DEBUG
+        if (debugDX)
+        {
+            getDLLRegister()->releaseDLL("WinPixGpuCapturer.dll");
+        }
+#endif
         getDLLRegister()->releaseDLL("dxgi.dll");
         getDLLRegister()->releaseDLL("d3d12.dll");
         Logging::error() << "ERROR DXGI: createFactory returned " << res
@@ -781,6 +827,12 @@ void DirectX12::initialize(
     if (!adapter)
     {
         factory->Release();
+#ifdef _DEBUG
+        if (debugDX)
+        {
+            getDLLRegister()->releaseDLL("WinPixGpuCapturer.dll");
+        }
+#endif
         getDLLRegister()->releaseDLL("dxgi.dll");
         getDLLRegister()->releaseDLL("d3d12.dll");
         WMessageBox(fenster->getWindowHandle(),
@@ -797,6 +849,12 @@ void DirectX12::initialize(
     if (FAILED(res))
     {
         factory->Release();
+#ifdef _DEBUG
+        if (debugDX)
+        {
+            getDLLRegister()->releaseDLL("WinPixGpuCapturer.dll");
+        }
+#endif
         getDLLRegister()->releaseDLL("dxgi.dll");
         getDLLRegister()->releaseDLL("d3d12.dll");
         Logging::error() << "ERROR: createDevice returned " << res << "\n";
@@ -806,16 +864,15 @@ void DirectX12::initialize(
             MB_ICONERROR);
         return;
     }
+    // since device was created dll cleanup will be done in destructor from here
+    // on
     D3D12_FEATURE_DATA_D3D12_OPTIONS5 featureSupportData = {};
     device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS5,
         &featureSupportData,
         sizeof(featureSupportData));
     if (featureSupportData.RaytracingTier < D3D12_RAYTRACING_TIER_1_0)
     {
-        device->Release();
         factory->Release();
-        getDLLRegister()->releaseDLL("dxgi.dll");
-        getDLLRegister()->releaseDLL("d3d12.dll");
         Logging::error() << "ERROR: Raytracing is not available\n";
         WMessageBox(fenster->getWindowHandle(),
             new Text("Fehler"),

+ 6 - 0
DX12Shader.cpp

@@ -208,6 +208,7 @@ void Framework::DX12ShaderSignature::createSignature(ID3D12Device5* zDevice,
         = new D3D12_ROOT_PARAMETER[paramCount];
     int index = 0;
     D3D12_DESCRIPTOR_RANGE** descriptorRanges = new D3D12_DESCRIPTOR_RANGE*[2];
+    memset(descriptorRanges, 0, sizeof(D3D12_DESCRIPTOR_RANGE*) * 2);
     int rangeCount = 0;
     ArrayIterator<DX12ShaderRegisterUsage*> it = descriptorHeapBindings.begin();
     DX12DescriptorHeapType currentDescriptorHeapType;
@@ -334,6 +335,10 @@ void Framework::DX12ShaderSignature::createSignature(ID3D12Device5* zDevice,
                          << errorMessage;
         pErrorBlob->Release();
     }
+    for (int i = 0; i < 2; i++)
+    {
+        delete[] descriptorRanges[i];
+    }
     delete[] descriptorRanges;
     delete[] descriptorTable;
 }
@@ -1001,6 +1006,7 @@ void Framework::DX12Pipeline::createPipelineState(ID3D12Device5* zDevice,
     }
     delete[] rootSignatureExports;
     delete[] localRootAssociations;
+    delete[] subobjects;
 }
 
 ID3D12StateObject* Framework::DX12Pipeline::zPipelineState() const

+ 1 - 0
DX12TLAS.cpp

@@ -100,6 +100,7 @@ void Framework::DX12TLAS::endUpdate()
         {
             D3D12_RAYTRACING_INSTANCE_DESC* desc = nextInstanceDesc();
             desc->InstanceMask = 0; // Mark unused instances with a mask of 0
+            desc->InstanceContributionToHitGroupIndex = 0;
         }
         descriptorBuffer->zBuffer()->Unmap(0, nullptr);
     }

+ 4 - 0
DataValidator.cpp

@@ -1076,6 +1076,10 @@ ValidationResult* DataValidator::validate(ElementPath* pathToValidate,
                         dynamic_cast<XML::Element*>(zConstraints->getThis()),
                         res);
                 }
+                else
+                {
+                    res->release();
+                }
             }
         }
         break;

+ 22 - 27
Font.cpp

@@ -1,7 +1,7 @@
 #include "Font.h"
 
-#include "Image.h"
 #include "Globals.h"
+#include "Image.h"
 #include "Scroll.h"
 #include "Text.h"
 #ifdef WIN32
@@ -135,8 +135,7 @@ void Alphabet::setFontSize(int gr) // sets the font size
 }
 
 // constant
-Character* Alphabet::getCharacter(
-    unsigned char i) const // returns a character
+Character* Alphabet::getCharacter(unsigned char i) const // returns a character
 {
     if (zeichen[i]) return dynamic_cast<Character*>(zeichen[i]->getThis());
     return 0;
@@ -164,6 +163,14 @@ AlphabetArray::AlphabetArray()
     memset(alphabets, 0, sizeof(Alphabet*) * 256);
 }
 
+Framework::AlphabetArray::~AlphabetArray()
+{
+    for (int i = 0; i < 256; ++i)
+    {
+        if (alphabets[i]) alphabets[i]->release();
+    }
+}
+
 // non-constant
 bool AlphabetArray::addAlphabet(Alphabet* alphabet) // Adds an alphabet
 {
@@ -215,8 +222,7 @@ Font::~Font()
     delete alphabet;
 }
 
-bool Font::addAlphabet(
-    Alphabet* alphabet) // Adds an alphabet to the font
+bool Font::addAlphabet(Alphabet* alphabet) // Adds an alphabet to the font
 {
     if (this->alphabet->addAlphabet(alphabet))
     {
@@ -473,8 +479,7 @@ void TextRenderer::renderText(int x,
     const Point& zRObjOff = zRObj.getDrawOff();
     int beginX = x;
     int zh = getRowHeight();
-    if (y + (zh + lineSpacing) * Text(txt).countOf('\n') + zh + zRObjOff.y
-            < 0
+    if (y + (zh + lineSpacing) * Text(txt).countOf('\n') + zh + zRObjOff.y < 0
         || x + zRObjOff.x >= zRObjBr || y + zRObjOff.y >= zRObjHi)
         return;
     bool faerb = 0;
@@ -490,15 +495,13 @@ void TextRenderer::renderText(int x,
         if (txt[i] == ' ')
         {
             if (faerb)
-                zRObj.alphaRegion(
-                    x, y, fontSize / 2 + charSpacing, zh, ff);
+                zRObj.alphaRegion(x, y, fontSize / 2 + charSpacing, zh, ff);
             x += fontSize / 2 + charSpacing;
             continue;
         }
         if (txt[i] == '\t')
         {
-            if (faerb)
-                zRObj.alphaRegion(x, y, fontSize + charSpacing, zh, ff);
+            if (faerb) zRObj.alphaRegion(x, y, fontSize + charSpacing, zh, ff);
             x += fontSize + charSpacing;
             continue;
         }
@@ -654,8 +657,7 @@ void TextRenderer::renderChar(int& x,
         if (underlined)
             zRObj.drawLineHAlpha(x - (int)(charSpacing / 2.0 + 0.5),
                 y + getRowHeight() + getCharSpacing() / 2,
-                fontSize / 2 + charSpacing
-                    + (int)(charSpacing / 2.0 + 0.5),
+                fontSize / 2 + charSpacing + (int)(charSpacing / 2.0 + 0.5),
                 0xFF000000 | color);
         x += fontSize / 2 + charSpacing;
     }
@@ -670,8 +672,7 @@ void TextRenderer::renderChar(int& x,
         if (underlined)
             zRObj.drawLineHAlpha(x - (int)(charSpacing / 2.0 + 0.5),
                 y + getRowHeight() + getCharSpacing() / 2,
-                fontSize + charSpacing
-                    + (int)(charSpacing / 2.0 + 0.5),
+                fontSize + charSpacing + (int)(charSpacing / 2.0 + 0.5),
                 0xFF000000 | color);
         x += fontSize + charSpacing;
     }
@@ -955,8 +956,7 @@ void EngravedTextRenderer::renderChar(int& x,
         if (underlined)
             zRObj.drawLineHAlpha(x - (int)(charSpacing / 2.0 + 0.5),
                 y + getRowHeight() + getCharSpacing() / 2,
-                fontSize / 2 + charSpacing
-                    + (int)(charSpacing / 2.0 + 0.5),
+                fontSize / 2 + charSpacing + (int)(charSpacing / 2.0 + 0.5),
                 0xFF000000 | color);
         x += fontSize / 2 + charSpacing;
     }
@@ -971,8 +971,7 @@ void EngravedTextRenderer::renderChar(int& x,
         if (underlined)
             zRObj.drawLineHAlpha(x - (int)(charSpacing / 2.0 + 0.5),
                 y + getRowHeight() + getCharSpacing() / 2,
-                fontSize + charSpacing
-                    + (int)(charSpacing / 2.0 + 0.5),
+                fontSize + charSpacing + (int)(charSpacing / 2.0 + 0.5),
                 0xFF000000 | color);
         x += fontSize + charSpacing;
     }
@@ -1052,14 +1051,12 @@ void ItalicTextRenderer::renderChar(int& x,
                 int xp = x + zRObjOff.x, yp = y + zRObjOff.y;
                 int xStartBuffer = xp < zRObjPos.x ? (zRObjPos.x - xp) : 0,
                     yStartBuffer = yp < zRObjPos.y ? (zRObjPos.y - yp) : 0;
-                int bufferWidth = b->getWidth(),
-                    bufferHeight = b->getHeight();
+                int bufferWidth = b->getWidth(), bufferHeight = b->getHeight();
                 unsigned char colorAlpha = (unsigned char)(255 - (color >> 24));
                 color &= 0x00FFFFFF;
                 double xStepBuffer
                     = (double)b->getFontSize() / (double)fontSize,
-                    yStepBuffer
-                    = (double)b->getFontSize() / (double)fontSize;
+                    yStepBuffer = (double)b->getFontSize() / (double)fontSize;
                 double xBuffer = xStartBuffer * xStepBuffer,
                        yBuffer = yStartBuffer * yStepBuffer;
                 int charHeight = getCharHeight(c);
@@ -1130,8 +1127,7 @@ void ItalicTextRenderer::renderChar(int& x,
         if (underlined)
             zRObj.drawLineHAlpha(x - (int)(charSpacing / 2.0 + 0.5),
                 y + getRowHeight() + getCharSpacing() / 2,
-                fontSize / 2 + charSpacing
-                    + (int)(charSpacing / 2.0 + 0.5),
+                fontSize / 2 + charSpacing + (int)(charSpacing / 2.0 + 0.5),
                 0xFF000000 | color);
         x += fontSize / 2 + charSpacing;
     }
@@ -1146,8 +1142,7 @@ void ItalicTextRenderer::renderChar(int& x,
         if (underlined)
             zRObj.drawLineHAlpha(x - (int)(charSpacing / 2.0 + 0.5),
                 y + getRowHeight() + getCharSpacing() / 2,
-                fontSize + charSpacing
-                    + (int)(charSpacing / 2.0 + 0.5),
+                fontSize + charSpacing + (int)(charSpacing / 2.0 + 0.5),
                 0xFF000000 | color);
         x += fontSize + charSpacing;
     }

+ 7 - 4
Font.h

@@ -9,11 +9,11 @@
 
 namespace Framework
 {
-    class Image;      //! Image.h
+    class Image;     //! Image.h
     class Text;      //! Text.h
     class Character; //! from this file
     class Alphabet;  //! from this file
-    class Font;   //! from this file
+    class Font;      //! from this file
 
     //! Stores the alpha values of a character of a specific font.
     //! The other color values are determined by the font color. Hence only
@@ -113,6 +113,7 @@ namespace Framework
     public:
         //! Creates a new list
         DLLEXPORT AlphabetArray();
+        DLLEXPORT ~AlphabetArray();
 
         //! Adds an alphabet to the list.
         //! If an alphabet with the same font size already exists,
@@ -260,10 +261,12 @@ namespace Framework
             int selectedBackgroundColor = 0);
         //! Returns the font size used for drawing
         DLLEXPORT int getFontSize() const;
-        //! Determines how many pixels are needed to fully display a specific text
+        //! Determines how many pixels are needed to fully display a specific
+        //! text
         //! \param txt The text whose width in pixels should be determined
         DLLEXPORT virtual int getTextWidth(const char* txt) const;
-        //! Determines how many pixels are needed to fully display a specific text
+        //! Determines how many pixels are needed to fully display a specific
+        //! text
         //! \param txt The text whose height in pixels should be determined
         DLLEXPORT virtual int getTextHeight(const char* txt) const;
         //! Determines how many pixels are needed to fully display a specific

+ 1 - 1
List.cpp

@@ -1288,7 +1288,7 @@ Drawable* DrawableList::getEntry(int pos) const
 //! \param pos The index of the entry
 Drawable* DrawableList::zEntry(int pos) const
 {
-    return list.get(pos);
+    return list.z(pos);
 }
 
 //! Returns the needed height

+ 9 - 0
Screen.cpp

@@ -41,6 +41,15 @@ Screen::Screen(NativeWindow* f)
 // Destructor
 Screen::~Screen()
 {
+    if (actions.size())
+    {
+        Framework::Logging::warning()
+            << "please enshure that there are no unexecuted pending ections "
+               "before releasing the screen memory. otherwise memory leaks may "
+               "occure. calls screen.tick(0.0) to execute all pending actions "
+               "before releasing the screen."
+            << "\n";
+    }
     rwLock.lockWrite();
 #ifdef WIN32
     if (fenster) fenster->release();