Browse Source

fix setPosition could potentially destroy an array

Kolja Strohm 8 months ago
parent
commit
421830bb71
6 changed files with 134 additions and 135 deletions
  1. 6 60
      Array.h
  2. 21 21
      DX12PixelShader.h
  3. 17 17
      DX12VertexShader.h
  4. 53 0
      Framework Tests/Array.cpp
  5. 22 22
      UIPixelShader.h
  6. 15 15
      UIVertexShader.h

+ 6 - 60
Array.h

@@ -372,36 +372,9 @@ namespace Framework
             if (i == p) return;
             if (i < 0 || p < 0 || i >= count || p >= count)
                 throwOutOfRange(__FILE__, __LINE__, i, count);
-            ArrayEintrag<TYP>* e = entries;
-            ArrayEintrag<TYP>* ve = 0;
-            for (int a = 0; a < i; ++a)
-            {
-                ve = e;
-                e = e->next;
-            }
-            ArrayEintrag<TYP>* e2 = entries == e ? e->next : entries;
-            ArrayEintrag<TYP>* ve2 = 0;
-            for (int a = 0; a < p; ++a)
-            {
-                ve2 = e2;
-                if (e2->next == e)
-                    e2 = e->next;
-                else
-                    e2 = e2->next;
-            }
-            if (e == last)
-                last = e2;
-            else if (e2 == last)
-                last = e;
-            if (!ve2)
-                entries = e;
-            else
-                ve2->next = e;
-            if (ve)
-                ve->next = e->next;
-            else
-                entries = e->next;
-            e->next = e2;
+            TYP t = get(i);
+            remove(i);
+            add(t, p);
         }
 
         //! Löscht ein Bestimmtes Element
@@ -706,36 +679,9 @@ namespace Framework
             if (i == p) return;
             if (i < 0 || p < 0 || i >= count || p >= count)
                 throwOutOfRange(__FILE__, __LINE__, i, count);
-            ArrayEintrag<TYP*>* ve = 0;
-            ArrayEintrag<TYP*>* e = entries;
-            for (int a = 0; a < i; ++a)
-            {
-                ve = e;
-                e = e->next;
-            }
-            ArrayEintrag<TYP*>* e2 = entries == e ? e->next : entries;
-            ArrayEintrag<TYP*>* ve2 = 0;
-            for (int a = 0; a < p; ++a)
-            {
-                ve2 = e2;
-                if (e2->next == e)
-                    e2 = e->next;
-                else
-                    e2 = e2->next;
-            }
-            if (last == e)
-                last = e2;
-            else if (last == e2)
-                last = e;
-            if (!ve2)
-                entries = e;
-            else
-                ve2->next = e;
-            if (ve)
-                ve->next = e->next;
-            else
-                entries = e->next;
-            e->next = e2;
+            TYP *t = get(i);
+            remove(i);
+            add(t, p);
         }
 
         //! Löscht ein Bestimmtes Element

+ 21 - 21
DX12PixelShader.h

@@ -92,10 +92,10 @@ ret
 
 const BYTE DX12PixelShaderBytes[] =
 {
-     68,  88,  66,  67, 107, 167, 
-    128, 193, 201,  13, 208, 118, 
-    217,  33,  32, 185,  38,  26, 
-    130, 194,   1,   0,   0,   0, 
+     68,  88,  66,  67, 125, 207, 
+     92, 130,  74,  42, 223,  25, 
+      9,  80, 223,  25,  71,  33, 
+     30, 180,   1,   0,   0,   0, 
     184,  91,   0,   0,   6,   0, 
       0,   0,  56,   0,   0,   0, 
      36,   2,   0,   0, 188,   2, 
@@ -763,11 +763,11 @@ const BYTE DX12PixelShaderBytes[] =
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0, 148,  46, 
-     49,   1,  45, 245, 215, 100, 
-      1,   0,   0,   0, 253,  41, 
-    155, 200, 124,  60, 160,  67, 
-    171, 215, 192, 199, 174, 199, 
-    135, 103,   0,   0,   0,   0, 
+     49,   1, 206, 156, 223, 100, 
+      1,   0,   0,   0, 163, 206, 
+    108,  96,  46,  58, 121,  72, 
+    190, 254,  25, 194, 153, 239, 
+    192, 179,   0,   0,   0,   0, 
       0,   0,   0,   0,   1,   0, 
       0,   0,   1,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
@@ -938,9 +938,9 @@ const BYTE DX12PixelShaderBytes[] =
       3,   0, 242,  56,   1,   0, 
      43, 236,   3,   0,  28,  19, 
       2,   0,  65,  36,   1,   0, 
-    236, 179,   1,   0, 215, 127, 
-      0,   0, 125,  10,   2,   0, 
-    125, 181,   2,   0, 205, 178, 
+    236, 179,   1,   0,  72,  48, 
+      1,   0, 125,  10,   2,   0, 
+    125, 181,   2,   0,  86,  28, 
       1,   0, 193,  33,   3,   0, 
      65, 185,   2,   0, 140, 239, 
       1,   0, 246,  49,   0,   0, 
@@ -1788,7 +1788,7 @@ const BYTE DX12PixelShaderBytes[] =
     117, 114, 101,  50,  68,  32, 
     115, 104,  97, 100,  27, 226, 
      48,   1, 128,   0,   0,   0, 
-    214,  34,  12,  94,  97, 205, 
+    142,  16,  75, 101, 241, 209, 
     217,   1,   1,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
@@ -2348,14 +2348,14 @@ const BYTE DX12PixelShaderBytes[] =
       0,   0,  23,   0,   1,   0, 
       5,  16,   0,   0,  14,   0, 
      23,  21,   0,  16,   0,   0, 
-      3,   2,  96,  89,   0,   0, 
+      3,   2, 112, 255,   0,   0, 
     242, 241,  10,   0,  24,  21, 
       8,  16,   0,   0,   1,   0, 
       1,   0,  10,   0,  24,  21, 
       9,  16,   0,   0,   1,   0, 
       0,   2,  14,   0,  23,  21, 
       0,   0,   0,   0,  10,   2, 
-     96,  89,   0,   0, 242, 241, 
+    112, 255,   0,   0, 242, 241, 
      10,   0,  24,  21,  11,  16, 
       0,   0,   1,   0,   1,   0, 
      10,   0,  24,  21,  12,  16, 
@@ -3408,11 +3408,11 @@ const BYTE DX12PixelShaderBytes[] =
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
-    148,  46,  49,   1,  45, 245, 
-    215, 100,   1,   0,   0,   0, 
-    253,  41, 155, 200, 124,  60, 
-    160,  67, 171, 215, 192, 199, 
-    174, 199, 135, 103, 128,   0, 
+    148,  46,  49,   1, 206, 156, 
+    223, 100,   1,   0,   0,   0, 
+    163, 206, 108,  96,  46,  58, 
+    121,  72, 190, 254,  25, 194, 
+    153, 239, 192, 179, 128,   0, 
       0,   0,  47,  76, 105, 110, 
     107,  73, 110, 102, 111,   0, 
      47, 110,  97, 109, 101, 115, 
@@ -3512,7 +3512,7 @@ const BYTE DX12PixelShaderBytes[] =
       0,   0,   2,   0,   9,   0, 
     220,   4,   0,   0,   0,   0, 
       0,   0, 156,   1,   0,   0, 
-      1,   0, 250, 150,   0,   0, 
+      1,   0,  57, 243,   0,   0, 
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0, 109,  97, 
     105, 110,   0, 110, 111, 110, 

+ 17 - 17
DX12VertexShader.h

@@ -131,10 +131,10 @@ ret
 
 const BYTE DX12VertexShaderBytes[] =
 {
-     68,  88,  66,  67,  82, 198, 
-    123, 136, 210, 186,  67, 232, 
-    123, 138, 238, 214, 195, 135, 
-    249, 213,   1,   0,   0,   0, 
+     68,  88,  66,  67,   8,  64, 
+    180, 236,  44, 154,  31, 218, 
+    200, 114, 224, 230,  93, 240, 
+     47, 157,   1,   0,   0,   0, 
     144,  78,   0,   0,   6,   0, 
       0,   0,  56,   0,   0,   0, 
     124,   2,   0,   0,  52,   3, 
@@ -923,11 +923,11 @@ const BYTE DX12VertexShaderBytes[] =
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
-    148,  46,  49,   1,  45, 245, 
-    215, 100,   1,   0,   0,   0, 
-    222, 106,  44,  43, 110, 235, 
-    107,  73, 185,  36,  69, 234, 
-     90,  40, 183, 209,   0,   0, 
+    148,  46,  49,   1, 206, 156, 
+    223, 100,   1,   0,   0,   0, 
+    172, 175, 101, 145, 247, 163, 
+    249,  76, 147,  53, 223,  67, 
+      6,  58, 134, 159,   0,   0, 
       0,   0,   0,   0,   0,   0, 
       1,   0,   0,   0,   1,   0, 
       0,   0,   0,   0,   0,   0, 
@@ -1103,7 +1103,7 @@ const BYTE DX12VertexShaderBytes[] =
       0,   0, 103, 159,   1,   0, 
     179, 120,   1,   0, 238,  97, 
       2,   0,  90,  28,   0,   0, 
-     99,  68,   3,   0,  53, 174, 
+    234, 210,   2,   0,  53, 174, 
       3,   0, 206,  21,   0,   0, 
     193, 205,   3,   0, 207, 193, 
       1,   0,  62,   3,   3,   0, 
@@ -1607,7 +1607,7 @@ const BYTE DX12VertexShaderBytes[] =
      97, 109, 101, 114,  97,  13, 
      10, 115, 116, 114,  27, 226, 
      48,   1, 128,   0,   0,   0, 
-    161,  50,  38,  94,  97, 205, 
+    193,  78,  98, 101, 241, 209, 
     217,   1,   1,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
@@ -2125,7 +2125,7 @@ const BYTE DX12VertexShaderBytes[] =
      24,  21,  12,  16,   0,   0, 
       1,   0,   1,   0,  14,   0, 
      23,  21,  13,  16,   0,   0, 
-     36,   2,  80, 114,   0,   0, 
+     36,   2,  64,  52,   0,   0, 
     242, 241,  10,   0,  24,  21, 
      14,  16,   0,   0,   1,   0, 
       0,   2,  18,   0,  22,  21, 
@@ -3057,10 +3057,10 @@ const BYTE DX12VertexShaderBytes[] =
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
       0,   0, 148,  46,  49,   1, 
-     45, 245, 215, 100,   1,   0, 
-      0,   0, 222, 106,  44,  43, 
-    110, 235, 107,  73, 185,  36, 
-     69, 234,  90,  40, 183, 209, 
+    206, 156, 223, 100,   1,   0, 
+      0,   0, 172, 175, 101, 145, 
+    247, 163, 249,  76, 147,  53, 
+    223,  67,   6,  58, 134, 159, 
     129,   0,   0,   0,  47,  76, 
     105, 110, 107,  73, 110, 102, 
     111,   0,  47, 110,  97, 109, 
@@ -3160,7 +3160,7 @@ const BYTE DX12VertexShaderBytes[] =
       0,   0,   0,   0,   2,   0, 
       9,   0, 156,   5,   0,   0, 
       0,   0,   0,   0, 236,   2, 
-      0,   0,   1,   0,  33, 114, 
+      0,   0,   1,   0,   7,  52, 
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
     109,  97, 105, 110,   0, 110, 

+ 53 - 0
Framework Tests/Array.cpp

@@ -303,6 +303,8 @@ namespace FrameworkTests
                 L"invalid value at index 2 of array after swapping elements");
             Assert::IsTrue((int)*array.z(0) == 100,
                 L"invalid value at index 0 of array after swapping elements");
+            Assert::IsTrue(array.getEintragAnzahl() == 4,
+                L"invalid value count in array after swapping elements");
             array.leeren();
             Assert::IsTrue(deleteCounter == 4, L"Memory leaks detected");
         }
@@ -332,5 +334,56 @@ namespace FrameworkTests
             array.leeren();
             Assert::IsTrue(deleteCounter == 7, L"Memory leaks detected");
         }
+
+        TEST_METHOD(IteratorTest)
+        {
+            Framework::RCArray<Test<RCArrayTests>> array;
+            array.add(new Test<RCArrayTests>(100, this));
+            array.begin().remove();
+            Assert::IsTrue(array.getEintragAnzahl() == 0,
+                				L"after adding 1 element and removing it "
+            				L"getEintragAnzahl() should be 0");
+            array.add(new Test<RCArrayTests>(100, this));
+            array.add(new Test<RCArrayTests>(20, this), 1);
+            array.add(new Test<RCArrayTests>(10, this), 0);
+            array.add(new Test<RCArrayTests>(0, this));
+            auto i = array.begin();
+            Assert::IsTrue(i->getVal() == 10,
+                				L"invalid value at index 0 of array after adding elements");
+            Assert::IsTrue(i.hasNext(), L"No next element after first element");
+            Assert::IsTrue((++i)->getVal() == 100,
+                								L"invalid value at index 1 of array after adding elements");
+            Assert::IsTrue(i.hasNext(), L"No next element after second element");
+            Assert::IsTrue((++i)->getVal() == 20,
+                												L"invalid value at index 2 of array after adding elements");
+            Assert::IsTrue(i.hasNext(), L"No next element after third element");
+            Assert::IsTrue((++i)->getVal() == 0,
+                																		L"invalid value at index 3 of array after adding elements");
+            Assert::IsFalse(i.hasNext(), L"Next element after last element");
+            i = array.begin();
+            int count = 4;
+            while (i)
+            {
+                i.remove();
+                Assert::IsTrue(array.getEintragAnzahl() == --count,
+                    L"after removing 1 element "
+                    L"getEintragAnzahl() should be reduced");
+            }
+            Assert::IsTrue(deleteCounter == 5, L"Memory leaks detected");
+        }
+
+        TEST_METHOD(MoveTest)
+        {
+            Framework::RCArray<Test<RCArrayTests>> array;
+            array.add(new Test<RCArrayTests>(100, this));
+            array.add(new Test<RCArrayTests>(20, this));
+            array.setPosition(0, 1);
+            array.add(new Test<RCArrayTests>(10, this));
+            Assert::IsTrue(array.getEintragAnzahl() == 3,
+                L"after adding 3 elements "
+                L"getEintragAnzahl() should be 0");
+            Assert::IsTrue(array.z(2)->getVal() == 10,
+                L"third element of array has an invalid value");
+        }
     };
 } // namespace FrameworkTests

+ 22 - 22
UIPixelShader.h

@@ -352,10 +352,10 @@ ret
 
 const BYTE UIPixelShader[] =
 {
-     68,  88,  66,  67, 164, 180, 
-     32, 182, 208,  34,  51,  57, 
-    193,   6, 190,  81, 108, 136, 
-    106, 100,   1,   0,   0,   0, 
+     68,  88,  66,  67, 141, 119, 
+      3,  70,  20, 233, 122,  96, 
+     32, 187, 229, 236,  38, 170, 
+    135,  57,   1,   0,   0,   0, 
      12, 134,   0,   0,   6,   0, 
       0,   0,  56,   0,   0,   0, 
     140,   6,   0,   0,  28,   7, 
@@ -1805,11 +1805,11 @@ const BYTE UIPixelShader[] =
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0, 148,  46, 
-     49,   1,  46, 245, 215, 100, 
-      1,   0,   0,   0,  34,  58, 
-     36, 131,  29,  49,  81,  78, 
-    139,  11, 142, 139,  27, 193, 
-     68, 210,   0,   0,   0,   0, 
+     49,   1, 206, 156, 223, 100, 
+      1,   0,   0,   0,  25,   6, 
+    117, 115, 201, 218, 227,  64, 
+    181, 201,  73, 246, 139, 241, 
+     23, 100,   0,   0,   0,   0, 
       0,   0,   0,   0,   1,   0, 
       0,   0,   1,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
@@ -1980,9 +1980,9 @@ const BYTE UIPixelShader[] =
       3,   0, 242,  56,   1,   0, 
      43, 236,   3,   0,  28,  19, 
       2,   0,  65,  36,   1,   0, 
-    236, 179,   1,   0, 215, 127, 
-      0,   0, 125,  10,   2,   0, 
-    125, 181,   2,   0, 205, 178, 
+    236, 179,   1,   0,  72,  48, 
+      1,   0, 125,  10,   2,   0, 
+    125, 181,   2,   0,  86,  28, 
       1,   0, 193,  33,   3,   0, 
      65, 185,   2,   0,   9, 241, 
       2,   0, 146, 230,   3,   0, 
@@ -3000,8 +3000,8 @@ const BYTE UIPixelShader[] =
      84, 101, 120, 116, 117, 114, 
     101,  50,  68,  32, 115, 104, 
      97, 100,  27, 226,  48,   1, 
-    128,   0,   0,   0, 163, 123, 
-     55,  94,  97, 205, 217,   1, 
+    128,   0,   0,   0,  12,  10, 
+    117, 101, 241, 209, 217,   1, 
       1,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
@@ -4243,14 +4243,14 @@ const BYTE UIPixelShader[] =
       6,  16,   0,   0,  23,   0, 
       1,   0,   5,  16,   0,   0, 
      14,   0,  23,  21,   0,  16, 
-      0,   0,   3,   2,  96,  89, 
+      0,   0,   3,   2, 112, 255, 
       0,   0, 242, 241,  10,   0, 
      24,  21,   8,  16,   0,   0, 
       1,   0,   1,   0,  10,   0, 
      24,  21,   9,  16,   0,   0, 
       1,   0,   0,   2,  14,   0, 
      23,  21,   0,   0,   0,   0, 
-     10,   2,  96,  89,   0,   0, 
+     10,   2, 112, 255,   0,   0, 
     242, 241,  10,   0,  24,  21, 
      11,  16,   0,   0,   1,   0, 
       1,   0,  10,   0,  24,  21, 
@@ -5474,11 +5474,11 @@ const BYTE UIPixelShader[] =
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
-    148,  46,  49,   1,  46, 245, 
-    215, 100,   1,   0,   0,   0, 
-     34,  58,  36, 131,  29,  49, 
-     81,  78, 139,  11, 142, 139, 
-     27, 193,  68, 210, 128,   0, 
+    148,  46,  49,   1, 206, 156, 
+    223, 100,   1,   0,   0,   0, 
+     25,   6, 117, 115, 201, 218, 
+    227,  64, 181, 201,  73, 246, 
+    139, 241,  23, 100, 128,   0, 
       0,   0,  47,  76, 105, 110, 
     107,  73, 110, 102, 111,   0, 
      47, 110,  97, 109, 101, 115, 
@@ -5578,7 +5578,7 @@ const BYTE UIPixelShader[] =
       0,   0,   2,   0,   9,   0, 
     204,   8,   0,   0,   0,   0, 
       0,   0, 164,  14,   0,   0, 
-      1,   0, 204,  52,   0,   0, 
+      1,   0,  18, 248,   0,   0, 
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,  84, 101, 
     120, 116, 117, 114, 101,  80, 

+ 15 - 15
UIVertexShader.h

@@ -121,10 +121,10 @@ ret
 
 const BYTE UIVertexShader[] =
 {
-     68,  88,  66,  67, 209,  39, 
-    116, 215,  20,  97, 179,  98, 
-     87,  70,  84,  52, 195, 111, 
-    212,  87,   1,   0,   0,   0, 
+     68,  88,  66,  67, 235, 188, 
+    126, 227, 239, 194, 170,  28, 
+      0,  92,  44,  29, 209,  78, 
+    102,   6,   1,   0,   0,   0, 
     204,  77,   0,   0,   6,   0, 
       0,   0,  56,   0,   0,   0, 
      20,   2,   0,   0, 204,   2, 
@@ -881,10 +881,10 @@ const BYTE UIVertexShader[] =
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
       0,   0, 148,  46,  49,   1, 
-     46, 245, 215, 100,   1,   0, 
-      0,   0,  41, 217, 161, 203, 
-    186, 104, 182,  78, 133,  81, 
-     26, 221,   8, 166, 186, 211, 
+    206, 156, 223, 100,   1,   0, 
+      0,   0,  40,   0, 195, 146, 
+    155, 118,   6,  79, 151, 107, 
+    150,  47, 174, 145, 110, 208, 
       0,   0,   0,   0,   0,   0, 
       0,   0,   1,   0,   0,   0, 
       1,   0,   0,   0,   0,   0, 
@@ -1564,8 +1564,8 @@ const BYTE UIVertexShader[] =
      13,  10,  47,  47,  32,  84, 
      89,  80,  69,  68,  69,  70, 
      27, 226,  48,   1, 128,   0, 
-      0,   0,  46, 113,  81,  94, 
-     97, 205, 217,   1,   1,   0, 
+      0,   0,   5,   2, 148, 101, 
+    241, 209, 217,   1,   1,   0, 
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
@@ -2929,10 +2929,10 @@ const BYTE UIVertexShader[] =
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
       0,   0, 148,  46,  49,   1, 
-     46, 245, 215, 100,   1,   0, 
-      0,   0,  41, 217, 161, 203, 
-    186, 104, 182,  78, 133,  81, 
-     26, 221,   8, 166, 186, 211, 
+    206, 156, 223, 100,   1,   0, 
+      0,   0,  40,   0, 195, 146, 
+    155, 118,   6,  79, 151, 107, 
+    150,  47, 174, 145, 110, 208, 
     129,   0,   0,   0,  47,  76, 
     105, 110, 107,  73, 110, 102, 
     111,   0,  47, 110,  97, 109, 
@@ -3032,7 +3032,7 @@ const BYTE UIVertexShader[] =
       0,   0,   0,   0,   2,   0, 
       9,   0, 104,   5,   0,   0, 
       0,   0,   0,   0, 236,   2, 
-      0,   0,   1,   0, 129,  80, 
+      0,   0,   1,   0, 188, 164, 
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
      84, 101, 120, 116, 117, 114,