Skip to content

Commit 821e48d

Browse files
authored
Merge pull request #6798 from Goober5000/message_memory_fix
fix builtin_message memory usage
2 parents c68f8d7 + 5380d88 commit 821e48d

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

code/mission/missionmessage.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,39 @@ float Command_announces_enemy_arrival_chance = 0.25;
5050
SCP_vector<SCP_string> Builtin_moods;
5151
int Current_mission_mood;
5252

53+
builtin_message::builtin_message(const char* _name, int _occurrence_chance, int _max_count, int _min_delay, int _priority, int _timing, int _fallback, bool _used_strdup)
54+
: name(_name), occurrence_chance(_occurrence_chance), max_count(_max_count), min_delay(_min_delay), priority(_priority), timing(_timing), fallback(_fallback), used_strdup(_used_strdup)
55+
{}
56+
57+
builtin_message::~builtin_message()
58+
{
59+
if (used_strdup)
60+
{
61+
vm_free(const_cast<char*>(name));
62+
name = nullptr;
63+
}
64+
}
65+
66+
builtin_message::builtin_message(const builtin_message& other)
67+
: name(other.name), occurrence_chance(other.occurrence_chance), max_count(other.max_count), min_delay(other.min_delay), priority(other.priority), timing(other.timing), fallback(other.fallback), used_strdup(other.used_strdup)
68+
{
69+
if (other.used_strdup)
70+
name = vm_strdup(other.name);
71+
}
72+
73+
builtin_message& builtin_message::operator=(const builtin_message& other)
74+
{
75+
name = other.used_strdup ? vm_strdup(other.name) : other.name;
76+
occurrence_chance = other.occurrence_chance;
77+
max_count = other.max_count;
78+
min_delay = other.min_delay;
79+
priority = other.priority;
80+
timing = other.timing;
81+
fallback = other.fallback;
82+
used_strdup = other.used_strdup;
83+
return *this;
84+
}
85+
5386
SCP_vector<builtin_message> Builtin_messages = {
5487
#define X(_, NAME, CHANCE, COUNT, DELAY, PRIORITY, TIME, FALLBACK) { \
5588
NAME, \
@@ -58,7 +91,8 @@ SCP_vector<builtin_message> Builtin_messages = {
5891
DELAY, \
5992
MESSAGE_PRIORITY_ ## PRIORITY, \
6093
MESSAGE_TIME_ ## TIME, \
61-
MESSAGE_ ## FALLBACK \
94+
MESSAGE_ ## FALLBACK, \
95+
false \
6296
}
6397
BUILTIN_MESSAGE_TYPES
6498
#undef X
@@ -709,7 +743,7 @@ void parse_custom_message_types(bool live = true) {
709743
int priority = parse_message_priority();
710744
if (live) {
711745
if (get_builtin_message_type(name) == MESSAGE_NONE) {
712-
Builtin_messages.push_back({ strdup(name), 100, -1, 0, priority, MESSAGE_TIME_SOON, fallback });
746+
Builtin_messages.emplace_back(vm_strdup(name), 100, -1, 0, priority, MESSAGE_TIME_SOON, fallback, true);
713747
} else {
714748
Warning(LOCATION, "Custom message type %s is already defined", name);
715749
}

code/mission/missionmessage.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ typedef struct builtin_message {
7272
int priority;
7373
int timing;
7474
int fallback;
75+
bool used_strdup;
76+
77+
builtin_message(const char* _name, int _occurrence_chance, int _max_count, int _min_delay, int _priority, int _timing, int _fallback, bool _used_strdup);
78+
// since we need a destructor, we need the other four special member functions as well
79+
~builtin_message();
80+
builtin_message(const builtin_message& other);
81+
builtin_message& operator=(const builtin_message& other);
82+
builtin_message(builtin_message&& other) noexcept = default;
83+
builtin_message& operator=(builtin_message&& other) noexcept = default;
7584
} builtin_message;
7685

7786
// If these are changed or updated be sure to update the map in scripting/api/libs/mission.cpp and the connected lua enumerations!

code/parse/sexp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15228,8 +15228,8 @@ void sexp_send_message(int n)
1522815228

1522915229
ship* get_builtin_message_sender(const char* name) {
1523015230
auto ship_entry = ship_registry_get(name);
15231-
if (ship_entry && ship_entry->has_shipp()) {
15232-
return ship_entry->shipp();
15231+
if (ship_entry) {
15232+
return ship_entry->shipp_or_null();
1523315233
}
1523415234

1523515235
auto wing_index = wing_lookup(name);

0 commit comments

Comments
 (0)