Skip to content

Commit 90de18b

Browse files
committed
Fix many allocation/deallocation mismatches.
The LiveCode engine uses three different allocators with distinct semantics. - `malloc()`/`free()`: these are C library procedures, usually accessed via the `MCMemory*` libfoundation functions; they allocate memory on the heap and do no C++ construction or destruction of their contents. - `new`/`delete`: these C++ operators allocate/deallocate a single instance on the heap, and calls its constructor/destructor. - `new[]`/`delete[]`: these C++ operators allocate/deallocate an sized array of instances on the heap, and call the constructor/destructor for each of them. Mismatch (e.g. allocating with `new[]` and freeing with `free()` is a resource management error, since it can result in something having a destructor called when it shouldn't (or vice versa). Both Coverity Scan and the newest version of valgrind detect mismatched use. This patch is a mostly mechanical replacement of release statements to match the corresponding allocators, with addition of annotation of what the expected allocator is. (I added the annotation to help me detect when multiple codepaths were allocating in conflicting ways). As part of this, I changed a couple of inconsistent allocation paths. A particular error that occurs frequently is using an `MCAutoPointer<T>` to manage a `T*` that was created using `malloc()`; in particular, the usual error is to try and put the result of a libfoundation function that allocates a buffer into an `MCAutoPointer<T>`. This won't work, because libfoundation always allocates memory with `malloc()`, and `MCAutoPointer` always destroys its contents with `delete`. Several codepaths are corrected to use an appropriate `MCAutoCustomPointer` instead.
1 parent d54575f commit 90de18b

27 files changed

Lines changed: 69 additions & 60 deletions

engine/src/aclip.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ MCAudioClip::~MCAudioClip()
154154
stop(True);
155155
MCacptr = NULL;
156156
}
157-
delete samples;
157+
delete[] samples; /* Allocated with new[] */
158158
delete osamples;
159159
#ifdef TARGET_PLATFORM_LINUX
160160
if ( x11audio != NULL )

engine/src/answer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ MCAnswer::~MCAnswer()
6565
delete file . filter;
6666
for(uint4 t_type = 0; t_type < file . type_count; ++t_type)
6767
delete file . types[t_type];
68-
delete file . types;
68+
delete[] file . types; /* Allocated with new[] */
6969
break;
7070

7171
case AT_FOLDER:
@@ -78,7 +78,7 @@ MCAnswer::~MCAnswer()
7878
delete notify . prompt;
7979
for(uint4 t_button = 0; t_button < notify . button_count; ++t_button)
8080
delete notify . buttons[t_button];
81-
delete notify . buttons;
81+
delete[] notify . buttons; /* Allocated with new[] */
8282
break;
8383
}
8484
}

engine/src/ask.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ MCAsk::~MCAsk(void)
6767
delete file . filter;
6868
for(uint4 t_type = 0; t_type < file . type_count; ++t_type)
6969
delete file . types[t_type];
70-
delete file . types;
70+
delete[] file . types; /* Allocated with new[] */
7171
break;
7272

7373
default:

engine/src/dispatch.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ MCDispatch::~MCDispatch()
178178
}
179179
delete fonts;
180180

181-
delete startdir;
182-
delete enginedir;
181+
MCMemoryDeleteArray(startdir); /* Allocated by MCStringConvertToCString() */
182+
MCMemoryDeleteArray(enginedir); /* Allocated by MCStringConvertToCString() */
183183

184184
delete m_externals;
185185
// AL-2015-02-10: [[ Standalone Inclusions ]] Delete library mapping

engine/src/dsklnx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1395,7 +1395,7 @@ class MCLinuxDesktop: public MCSystemInterface
13951395
}
13961396
}
13971397

1398-
delete t_entry_path;
1398+
delete[] t_entry_path; /* Allocated with new[] */
13991399
closedir(dirptr);
14001400

14011401
return t_success;

engine/src/eps.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ MCEPS::MCEPS(const MCEPS &sref) : MCControl(sref)
9898

9999
MCEPS::~MCEPS()
100100
{
101-
delete postscript;
102-
delete prolog;
101+
delete[] postscript; /* Allocated with new[] */
102+
delete[] prolog; /* Allocated with new [] */
103103
delete pageIndex;
104104
delete image;
105105
}

engine/src/exec-array.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ bool MCArraysCopyExtents(MCArrayRef self, array_extent_t*& r_extents, uindex_t&
961961

962962
while (MCArrayIterate(self, t_index, t_key, t_value))
963963
{
964-
MCAutoPointer<index_t> t_indexes;
964+
MCAutoCustomPointer<index_t,MCMemoryDeleteArray> t_indexes;
965965
uindex_t t_index_count;
966966
bool t_all_integers;
967967
if (!MCArraysSplitIndexes(t_key, &t_indexes, t_index_count, t_all_integers))

engine/src/exec-interface-stack.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1485,7 +1485,7 @@ void MCStack::SetStackFiles(MCExecContext& ctxt, MCStringRef p_files)
14851485
MCValueRelease(stackfiles[nstackfiles].stackname);
14861486
MCValueRelease(stackfiles[nstackfiles].filename);
14871487
}
1488-
delete stackfiles;
1488+
delete[] stackfiles; /* Allocated with new[] */
14891489

14901490
if (stringtostackfiles(p_files, &stackfiles, nstackfiles))
14911491
return;

engine/src/exec-interface2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ void MCInterfaceStackFileVersionParse(MCExecContext& ctxt, MCStringRef p_input,
564564
char *t_version;
565565
/* UNCHECKED */ MCStringConvertToCString(p_input, t_version);
566566
count = sscanf(t_version, "%d.%d.%d", &major, &minor, &revision);
567-
delete t_version;
567+
MCMemoryDeleteArray(t_version);
568568

569569
version = major * 1000 + minor * 100 + revision * 10;
570570

engine/src/externalv0.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,9 @@ Exec_stat MCExternalV0::Handle(MCObject *p_context, Handler_type p_type, uint32_
353353
if (args != NULL)
354354
{
355355
while (nargs--)
356-
delete args[nargs];
356+
MCMemoryDeleteArray(args[nargs]); /* Allocated with MCStringNormalizeAndConvertToCString */
357357

358-
delete args;
358+
delete[] args; /* Allocated with new[] */
359359
}
360360

361361
if (Xerr)

0 commit comments

Comments
 (0)