Skip to content

Commit 79050df

Browse files
committed
lua: Fix memory leak on aegisub.cancel()
1 parent 5050bad commit 79050df

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

src/auto4_lua.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ namespace Automation4 {
7878
std::deque<PendingCommit> pending_commits;
7979
/// Lines to delete once processing complete successfully
8080
std::vector<std::unique_ptr<AssEntry>> lines_to_delete;
81+
/// Lines that were allocated here and need to be deleted if the script is cancelled.
82+
std::vector<AssEntry *> allocated_lines;
8183

8284
/// Create copies of all of the lines in the script info section if it
8385
/// hasn't already happened. This is done lazily, since it only needs
@@ -118,6 +120,8 @@ namespace Automation4 {
118120
/// assumes a Lua representation of AssEntry on the top of the stack, and creates an AssEntry object of it
119121
static std::unique_ptr<AssEntry> LuaToAssEntry(lua_State *L, AssFile *ass=nullptr);
120122

123+
std::unique_ptr<AssEntry> LuaToTrackedAssEntry(lua_State *L);
124+
121125
/// @brief Signal that the script using this file is now done running
122126
/// @param set_undo If there's any uncommitted changes to the file,
123127
/// they will be automatically committed with this

src/auto4_lua_assfile.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,12 @@ namespace Automation4 {
328328
return result;
329329
}
330330

331+
std::unique_ptr<AssEntry> LuaAssFile::LuaToTrackedAssEntry(lua_State *L) {
332+
std::unique_ptr<AssEntry> e = LuaToAssEntry(L, ass);
333+
allocated_lines.push_back(e.get());
334+
return e;
335+
}
336+
331337
int LuaAssFile::ObjectIndexRead(lua_State *L)
332338
{
333339
switch (lua_type(L, 2)) {
@@ -453,7 +459,7 @@ namespace Automation4 {
453459
// insert
454460
CheckBounds(n);
455461

456-
auto e = LuaToAssEntry(L, ass);
462+
auto e = LuaToTrackedAssEntry(L);
457463
modification_type |= modification_mask(e.get());
458464
QueueLineForDeletion(n - 1);
459465
AssignLine(n - 1, std::move(e));
@@ -542,7 +548,7 @@ namespace Automation4 {
542548

543549
for (int i = 1; i <= n; i++) {
544550
lua_pushvalue(L, i);
545-
auto e = LuaToAssEntry(L, ass);
551+
auto e = LuaToTrackedAssEntry(L);
546552
modification_type |= modification_mask(e.get());
547553

548554
if (lines.empty()) {
@@ -586,7 +592,7 @@ namespace Automation4 {
586592
new_entries.reserve(n - 1);
587593
for (int i = 2; i <= n; i++) {
588594
lua_pushvalue(L, i);
589-
auto e = LuaToAssEntry(L, ass);
595+
auto e = LuaToTrackedAssEntry(L);
590596
modification_type |= modification_mask(e.get());
591597
InsertLine(new_entries, i - 2, std::move(e));
592598
lua_pop(L, 1);
@@ -625,7 +631,7 @@ namespace Automation4 {
625631

626632
int LuaAssFile::LuaParseKaraokeData(lua_State *L)
627633
{
628-
auto e = LuaToAssEntry(L, ass);
634+
auto e = LuaToTrackedAssEntry(L);
629635
auto dia = check_cast_constptr<AssDialogue>(e.get());
630636
argcheck(L, !!dia, 1, "Subtitle line must be a dialogue line");
631637

@@ -734,6 +740,7 @@ namespace Automation4 {
734740
void LuaAssFile::Cancel()
735741
{
736742
for (auto& line : lines_to_delete) line.release();
743+
for (AssEntry *line : allocated_lines) delete line;
737744
references--;
738745
if (!references) delete this;
739746
}

0 commit comments

Comments
 (0)