Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 0a8cf60

Browse files
committed
[[ Bug 21988 ]] Fix memory leaks in clipboard functions
This patch fixes several similar memory leaks that can occur when using the clipboard functions. The leaks are either caused by passing a MCDataRef with a +1 retain count to the constructor of an MCAutoDataRef, or a similar case with an MCRawClipboardItem.
1 parent b5e0e6b commit 0a8cf60

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

docs/notes/bugfix-21988.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Fix memory leaks when using clipboard functions

engine/src/clipboard.cpp

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ bool MCClipboard::AddFileList(MCStringRef p_file_names)
203203
return false;
204204

205205
// Encode it appropriately for this clipboard
206-
MCAutoDataRef t_encoded_path(m_clipboard->EncodeFileListForTransfer((MCStringRef)t_path));
206+
MCAutoDataRef t_encoded_path;
207+
t_encoded_path.Give(m_clipboard->EncodeFileListForTransfer((MCStringRef)t_path));
207208
if (*t_encoded_path == NULL)
208209
return false;
209210

@@ -222,7 +223,8 @@ bool MCClipboard::AddFileList(MCStringRef p_file_names)
222223
else
223224
{
224225
// Convert the file list into the correct format
225-
MCAutoDataRef t_encoded_list(m_clipboard->EncodeFileListForTransfer(p_file_names));
226+
MCAutoDataRef t_encoded_list;
227+
t_encoded_list.Give(m_clipboard->EncodeFileListForTransfer(p_file_names));
226228
if (*t_encoded_list == NULL)
227229
return false;
228230

@@ -370,14 +372,16 @@ bool MCClipboard::AddLiveCodeStyledText(MCDataRef p_pickled_text)
370372
if (t_success && (t_type_string = m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeRTF)) != NULL)
371373
{
372374
// This type is optional as it may not be a faithful representation
373-
MCAutoDataRef t_rtf(ConvertStyledTextToRTF(p_pickled_text));
375+
MCAutoDataRef t_rtf;
376+
t_rtf.Give(ConvertStyledTextToRTF(p_pickled_text));
374377
if (*t_rtf != NULL)
375378
t_success = t_item->AddRepresentation(t_type_string, *t_rtf);
376379
}
377380
if (t_success && (t_type_string = m_clipboard->GetKnownTypeString(kMCRawClipboardKnownTypeHTML)) != NULL)
378381
{
379382
// This type is optional as it may not be a faithful representation
380-
MCAutoStringRef t_html(ConvertStyledTextToHTML(p_pickled_text));
383+
MCAutoStringRef t_html;
384+
t_html.Give(ConvertStyledTextToHTML(p_pickled_text));
381385
if (*t_html != NULL)
382386
{
383387
// All platforms accept UTF-8 as an encoding for HTML on the clipboard
@@ -387,7 +391,7 @@ bool MCClipboard::AddLiveCodeStyledText(MCDataRef p_pickled_text)
387391
if (t_success)
388392
{
389393
MCAutoDataRef t_encoded;
390-
t_encoded = m_clipboard->EncodeHTMLFragmentForTransfer(*t_html_utf8);
394+
t_encoded.Give(m_clipboard->EncodeHTMLFragmentForTransfer(*t_html_utf8));
391395
t_success = *t_encoded != nil;
392396
if (t_success)
393397
t_success = t_item->AddRepresentation(t_type_string, *t_encoded);
@@ -399,7 +403,8 @@ bool MCClipboard::AddLiveCodeStyledText(MCDataRef p_pickled_text)
399403
if (t_success)
400404
{
401405
// This type is optional as it may not be a faithful representation
402-
MCAutoStringRef t_text(ConvertStyledTextToText(p_pickled_text));
406+
MCAutoStringRef t_text;
407+
t_text.Give(ConvertStyledTextToText(p_pickled_text));
403408
if (*t_text != NULL)
404409
t_success = AddTextToItem(t_item, *t_text);
405410
}
@@ -410,7 +415,8 @@ bool MCClipboard::AddLiveCodeStyledText(MCDataRef p_pickled_text)
410415
bool MCClipboard::AddLiveCodeStyledTextArray(MCArrayRef p_styled_text)
411416
{
412417
// Convert the styled text array to serialised styled text and add that way.
413-
MCAutoDataRef t_pickled_text(ConvertStyledTextArrayToStyledText(p_styled_text));
418+
MCAutoDataRef t_pickled_text;
419+
t_pickled_text.Give(ConvertStyledTextArrayToStyledText(p_styled_text));
414420
if (*t_pickled_text == NULL)
415421
return false;
416422

@@ -424,7 +430,8 @@ bool MCClipboard::AddHTMLText(MCStringRef p_html_string)
424430
// format.
425431
//
426432
// This is a lossy process but preserves legacy behaviour.
427-
MCAutoDataRef t_pickled_text(ConvertHTMLToStyledText(p_html_string));
433+
MCAutoDataRef t_pickled_text;
434+
t_pickled_text.Give(ConvertHTMLToStyledText(p_html_string));
428435
if (*t_pickled_text == NULL)
429436
return false;
430437

@@ -438,7 +445,8 @@ bool MCClipboard::AddRTFText(MCDataRef p_rtf_data)
438445
// format.
439446
//
440447
// This is a lossy process but preserves legacy behaviour.
441-
MCAutoDataRef t_pickled_text(ConvertRTFToStyledText(p_rtf_data));
448+
MCAutoDataRef t_pickled_text;
449+
t_pickled_text.Give(ConvertRTFToStyledText(p_rtf_data));
442450
if (*t_pickled_text == NULL)
443451
return false;
444452

@@ -486,7 +494,7 @@ bool MCClipboard::AddHTML(MCStringRef p_html)
486494

487495
// Encode the HTML in the required format for the clipboard
488496
MCAutoDataRef t_encoded;
489-
t_encoded = m_clipboard->EncodeHTMLFragmentForTransfer(*t_html_utf8);
497+
t_encoded.Give(m_clipboard->EncodeHTMLFragmentForTransfer(*t_html_utf8));
490498
if (*t_encoded == nil)
491499
return false;
492500

@@ -653,7 +661,10 @@ bool MCClipboard::AddPrivateData(MCDataRef p_private_data)
653661

654662
// Ensure that an item is always on the clipboard, even if it is empty
655663
if (m_clipboard->GetItemCount() == 0)
656-
m_clipboard->AddItem(m_clipboard->CreateNewItem());
664+
{
665+
MCAutoRefcounted<MCRawClipboardItem> t_item = m_clipboard->CreateNewItem();
666+
m_clipboard->AddItem(t_item);
667+
}
657668

658669
return true;
659670
}
@@ -887,7 +898,7 @@ bool MCClipboard::CopyAsFileList(MCStringRef& r_file_list) const
887898
t_encoded_url.Give(t_rep->CopyData());
888899
if (*t_encoded_url == NULL)
889900
return false;
890-
t_url.Reset(m_clipboard->DecodeTransferredFileList(*t_encoded_url));
901+
t_url.Give(m_clipboard->DecodeTransferredFileList(*t_encoded_url));
891902
if (*t_url == NULL)
892903
return false;
893904

@@ -1058,12 +1069,11 @@ bool MCClipboard::CopyAsHTMLText(MCStringRef& r_html) const
10581069
if (!CopyAsLiveCodeStyledText(&t_pickled_text))
10591070
return false;
10601071

1061-
MCAutoStringRef t_html;
1062-
t_html.Give(ConvertStyledTextToHTML(*t_pickled_text));
1063-
if (*t_html == nil)
1072+
MCStringRef t_html = ConvertStyledTextToHTML(*t_pickled_text);
1073+
if (t_html == nil)
10641074
return false;
10651075

1066-
r_html = t_html.Take();
1076+
r_html = t_html;
10671077
return true;
10681078
}
10691079

0 commit comments

Comments
 (0)