Skip to content

Commit ae101a7

Browse files
Ensure that RefCountersImpl is deleted in the same module where it was created (fix #704)
1 parent 6414a66 commit ae101a7

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

Common/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ set(SOURCE
6363
src/GeometryPrimitives.cpp
6464
src/ImageTools.cpp
6565
src/MemoryFileStream.cpp
66+
src/RefCountedObjectImpl.cpp
6667
src/Serializer.cpp
6768
src/SpinLock.cpp
6869
src/ThreadPool.cpp

Common/interface/RefCountedObjectImpl.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,9 @@ class RefCountersImpl final : public IReferenceCounters
448448
}
449449
}
450450

451-
void SelfDestroy()
452-
{
453-
delete this;
454-
}
451+
static RefCountersImpl* MakeNew();
452+
453+
void SelfDestroy();
455454

456455
~RefCountersImpl()
457456
{
@@ -654,7 +653,7 @@ class MakeNewRCObj
654653
{
655654
// Constructor of RefCountersImpl class is private and only accessible
656655
// by methods of MakeNewRCObj
657-
pNewRefCounters = new RefCountersImpl{};
656+
pNewRefCounters = RefCountersImpl::MakeNew();
658657
pRefCounters = pNewRefCounters;
659658
}
660659
ObjectType* pObj = nullptr;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2025 Diligent Graphics LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* In no event and under no legal theory, whether in tort (including negligence),
17+
* contract, or otherwise, unless required by applicable law (such as deliberate
18+
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
19+
* liable for any damages, including any direct, indirect, special, incidental,
20+
* or consequential damages of any character arising as a result of this License or
21+
* out of the use or inability to use the software (including but not limited to damages
22+
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
23+
* all other commercial damages or losses), even if such Contributor has been advised
24+
* of the possibility of such damages.
25+
*/
26+
27+
#include "RefCountedObjectImpl.hpp"
28+
29+
namespace Diligent
30+
{
31+
32+
// Define new and delete in the .cpp file to ensure that they are called from the same module
33+
// (https://github.com/DiligentGraphics/DiligentCore/issues/704)
34+
RefCountersImpl* RefCountersImpl::MakeNew()
35+
{
36+
return new RefCountersImpl{};
37+
}
38+
39+
void RefCountersImpl::SelfDestroy()
40+
{
41+
delete this;
42+
}
43+
44+
} // namespace Diligent

0 commit comments

Comments
 (0)