Skip to content

Commit

Permalink
GH-421 Fix 4.3 stack allocation crash issues with RefCounted
Browse files Browse the repository at this point in the history
  • Loading branch information
Naros committed Jun 29, 2024
1 parent d696fd1 commit 5bc2994
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/common/guid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ String Guid::to_string() const

Guid Guid::create_guid()
{
RandomNumberGenerator rng;
Ref<RandomNumberGenerator> rng(memnew(RandomNumberGenerator));

uint32_t a = rng.randi();
uint32_t b = rng.randi();
uint32_t c = rng.randi();
uint32_t d = rng.randi();
uint32_t a = rng->randi();
uint32_t b = rng->randi();
uint32_t c = rng->randi();
uint32_t d = rng->randi();

// The 4 bits of digit M indicate the GUID version, and the 1-3 most significant bits
// of digit N indicate the UUID variant.
Expand Down
7 changes: 5 additions & 2 deletions src/script/nodes/flow_control/chance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
class OScriptNodeChanceInstance : public OScriptNodeInstance
{
DECLARE_SCRIPT_NODE_INSTANCE(OScriptNodeChance);
RandomNumberGenerator _random;
Ref<RandomNumberGenerator> _random;
int _chance{ 0 };

public:
int step(OScriptExecutionContext& p_context) override
{
const int _calculated_chance = _random.randi_range(0, 100);
if (!_random.is_valid())
_random.instantiate();

const int _calculated_chance = _random->randi_range(0, 100);
return _calculated_chance <= _chance ? 0 : 1;
}
};
Expand Down
7 changes: 5 additions & 2 deletions src/script/nodes/flow_control/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class OScriptNodeRandomInstance : public OScriptNodeInstance
{
DECLARE_SCRIPT_NODE_INSTANCE(OScriptNodeRandom);

RandomNumberGenerator _random;
Ref<RandomNumberGenerator> _random;
int _possibilities{ 0 };

public:
Expand All @@ -29,7 +29,10 @@ class OScriptNodeRandomInstance : public OScriptNodeInstance
if (_possibilities == 0)
return -1;

return _random.randi_range(0, _possibilities - 1);
if (!_random.is_valid())
_random.instantiate();

return _random->randi_range(0, _possibilities - 1);
}
};

Expand Down

0 comments on commit 5bc2994

Please sign in to comment.