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

Commit fe1f129

Browse files
Replace MCObjectHandle* with a smart pointer class
This provides RAII-semantics for the Retain/Release pairs for the object weak references.
1 parent 6b446f0 commit fe1f129

36 files changed

+541
-465
lines changed

engine/src/desktop-menu.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -531,11 +531,11 @@ bool MCButton::macmenuisopen()
531531
struct MCMainMenuInfo
532532
{
533533
MCPlatformMenuRef menu;
534-
MCObjectHandle *target;
534+
MCObjectHandle target;
535535
};
536536

537537
static MCPlatformMenuRef s_menubar = nil;
538-
static MCObjectHandle **s_menubar_targets = nil;
538+
static MCObjectHandle *s_menubar_targets = nil;
539539
static uindex_t s_menubar_target_count = 0;
540540
static uindex_t s_menubar_lock_count = 0;
541541

@@ -596,7 +596,7 @@ void MCScreenDC::updatemenubar(Boolean force)
596596
t_menu_index = 0;
597597

598598
// We construct the new menubar as we go along.
599-
MCObjectHandle **t_new_menubar_targets;
599+
MCObjectHandle *t_new_menubar_targets;
600600
uindex_t t_new_menubar_target_count;
601601
t_new_menubar_targets = nil;
602602
t_new_menubar_target_count = 0;
@@ -645,8 +645,8 @@ void MCScreenDC::updatemenubar(Boolean force)
645645
MCPlatformReleaseMenu(t_menu);
646646

647647
// Extend the new menubar targets array by one.
648-
/* UNCHECKED */ MCMemoryResizeArray(t_new_menubar_target_count + 1, t_new_menubar_targets, t_new_menubar_target_count);
649-
t_new_menubar_targets[t_menu_index] = t_menu_button -> gethandle();
648+
/* UNCHECKED */ MCMemoryResizeArrayInit(t_new_menubar_target_count + 1, t_new_menubar_targets, t_new_menubar_target_count);
649+
t_new_menubar_targets[t_menu_index] = t_menu_button->GetHandle();
650650

651651
// Increment the index into the menubar.
652652
t_menu_index++;
@@ -656,9 +656,7 @@ void MCScreenDC::updatemenubar(Boolean force)
656656
if (s_menubar != nil)
657657
{
658658
MCPlatformReleaseMenu(s_menubar);
659-
for(uindex_t i = 0; i < s_menubar_target_count; i++)
660-
s_menubar_targets[i] -> Release();
661-
MCMemoryDeleteArray(s_menubar_targets);
659+
MCMemoryDeleteArray(s_menubar_targets, s_menubar_target_count);
662660
}
663661

664662
// Update to the new menubar and targets.
@@ -923,7 +921,7 @@ void MCPlatformHandleMenuUpdate(MCPlatformMenuRef p_menu)
923921
// If the button it is 'attached' to still exists, dispatch the menu update
924922
// message (currently mouseDown("")). We do this whilst the menubar is locked
925923
// from updates as we mustn't fiddle about with it too much in this case!
926-
if (t_update_menubar || s_menubar_targets[t_parent_menu_index] -> Exists())
924+
if (t_update_menubar || s_menubar_targets[t_parent_menu_index].IsValid())
927925
{
928926
// MW-2014-06-10: [[ Bug 12590 ]] Make sure we lock screen around the menu update message.
929927
MCRedrawLockScreen();
@@ -940,10 +938,9 @@ void MCPlatformHandleMenuUpdate(MCPlatformMenuRef p_menu)
940938
// SN-2014-11-10: [[ Bug 13836 ]] Make sure that
941939
// Now we've got the menu to update, process the new menu spec, but only if the
942940
// menu button still exists!
943-
if (!t_update_menubar && s_menubar_targets[t_parent_menu_index] -> Exists())
941+
if (!t_update_menubar && s_menubar_targets[t_parent_menu_index].IsValid())
944942
{
945-
MCButton *t_button;
946-
t_button = (MCButton *)s_menubar_targets[t_parent_menu_index] -> Get();
943+
MCButton *t_button = s_menubar_targets[t_parent_menu_index].GetAs<MCButton>();
947944

948945
MCPlatformRemoveAllMenuItems(p_menu);
949946
MCstacks -> deleteaccelerator(t_button, t_button -> getstack());
@@ -995,10 +992,10 @@ void MCPlatformHandleMenuSelect(MCPlatformMenuRef p_menu, uindex_t p_item_index)
995992
// will be the current popup menu or icon menu.
996993
if (t_current_menu != nil)
997994
{
998-
if (s_menubar_targets[t_current_menu_index] -> Exists())
995+
if (s_menubar_targets[t_current_menu_index].IsValid())
999996
{
1000-
((MCButton *)s_menubar_targets[t_current_menu_index] -> Get()) -> setmenuhistoryprop(t_last_menu_index + 1);
1001-
((MCButton *)s_menubar_targets[t_current_menu_index] -> Get()) -> handlemenupick(*t_result, nil);
997+
s_menubar_targets[t_current_menu_index].GetAs<MCButton>()->setmenuhistoryprop(t_last_menu_index + 1);
998+
s_menubar_targets[t_current_menu_index].GetAs<MCButton>()->handlemenupick(*t_result, nil);
1002999
}
10031000
}
10041001
else

engine/src/eventqueue.cpp

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ struct MCEvent
9898

9999
struct
100100
{
101-
MCObjectHandle *target;
101+
// This shouldn't really be a raw MCObjectProxy* but we can't embed
102+
// the MCObjectHandle RAII class here as it cannot be placed inside
103+
// a union.
104+
MCObjectProxy* target;
102105
union
103106
{
104107
struct
@@ -293,18 +296,16 @@ static void MCEventQueueDispatchEvent(MCEvent *p_event)
293296

294297
case kMCEventTypeUpdateMenu:
295298
{
296-
MCObject *t_target;
297-
t_target = t_event -> menu . target -> Get();
298-
if (t_target != nil)
299+
MCObjectHandle t_target = t_event->menu.target;
300+
if (t_target.IsValid())
299301
t_target->message_with_valueref_args(MCM_mouse_down, kMCEmptyString);
300302
}
301303
break;
302304

303305
case kMCEventTypeMenuPick:
304306
{
305-
MCObject *t_target;
306-
t_target = t_event -> menu . target -> Get();
307-
if (t_target != nil)
307+
MCObjectHandle t_target = t_event->menu.target;
308+
if (t_target.IsValid())
308309
// SN-2014-06-23: pick updated to StringRef
309310
t_target->message_with_valueref_args(MCM_menu_pick, t_event -> menu . pick . string);
310311
}
@@ -605,12 +606,13 @@ static void MCEventQueueDestroyEvent(MCEvent *p_event)
605606
MCMemoryDeleteArray(p_event -> ime . compose . chars);
606607
else if (p_event -> type == kMCEventTypeUpdateMenu)
607608
{
608-
p_event -> menu . target -> Release();
609+
MCObjectHandle t_handle = p_event->menu.target;
610+
t_handle.ExternalRelease();
609611
}
610612
else if (p_event -> type == kMCEventTypeMenuPick)
611613
{
612-
p_event -> menu . target -> Release();
613-
// SN-2014-06-23: pick updated to StringRef
614+
MCObjectHandle t_handle = p_event->menu.target;
615+
t_handle.ExternalRelease();
614616
MCValueRelease(p_event -> menu . pick . string);
615617
}
616618
#ifdef _MOBILE
@@ -1076,23 +1078,25 @@ bool MCEventQueuePostResumeApp(void)
10761078
return MCEventQueuePost(kMCEventTypeResumeApp, t_event);
10771079
}
10781080

1079-
bool MCEventQueuePostUpdateMenu(MCObjectHandle *p_target)
1081+
bool MCEventQueuePostUpdateMenu(MCObjectHandle p_target)
10801082
{
10811083
MCEvent *t_event;
10821084
if (!MCEventQueuePost(kMCEventTypeUpdateMenu, t_event))
10831085
return false;
1084-
p_target -> Retain();
1085-
t_event -> menu . target = p_target;
1086+
1087+
t_event -> menu . target = p_target.ExternalRetain();
1088+
10861089
return true;
10871090
}
10881091

1089-
bool MCEventQueuePostMenuPick(MCObjectHandle *p_target, MCStringRef p_string)
1092+
bool MCEventQueuePostMenuPick(MCObjectHandle p_target, MCStringRef p_string)
10901093
{
10911094
MCEvent *t_event;
10921095
if (!MCEventQueuePost(kMCEventTypeMenuPick, t_event))
10931096
return false;
1094-
p_target -> Retain();
1095-
t_event -> menu . target = p_target;
1097+
1098+
t_event -> menu . target = p_target.ExternalRetain();
1099+
10961100
// SN-2014-06-23: pick updated to StringRef
10971101
return MCStringCopy(p_string, t_event -> menu . pick . string);
10981102
}
@@ -1138,7 +1142,7 @@ struct MCTouch
11381142
MCTouch *next;
11391143
uint32_t id;
11401144
int32_t x, y;
1141-
MCObjectHandle *target;
1145+
MCObjectHandle target;
11421146
};
11431147

11441148
static MCTouch *s_touches = nil;
@@ -1166,7 +1170,7 @@ static void handle_touch(MCStack *p_stack, MCEventTouchPhase p_phase, uint32_t p
11661170

11671171
if (t_touch != nil)
11681172
{
1169-
t_target = t_touch -> target -> Get();
1173+
t_target = t_touch -> target;
11701174

11711175
// MW-2011-09-05: [[ Bug 9683 ]] Make sure we remove (and delete the touch) here if
11721176
// it is 'end' or 'cancelled' so that a cleartouches inside an invoked handler
@@ -1178,9 +1182,6 @@ static void handle_touch(MCStack *p_stack, MCEventTouchPhase p_phase, uint32_t p
11781182
else
11791183
t_previous_touch -> next = t_touch -> next;
11801184

1181-
// MW-2011-01-28: Looks like a leak to me - make sure we release the object handle!
1182-
t_touch -> target -> Release();
1183-
11841185
delete t_touch;
11851186
}
11861187
}
@@ -1192,7 +1193,7 @@ static void handle_touch(MCStack *p_stack, MCEventTouchPhase p_phase, uint32_t p
11921193
t_touch -> id = p_id;
11931194

11941195
t_target = p_stack -> getcurcard() -> hittest(t_touch_loc.x, t_touch_loc.y);
1195-
t_touch -> target = t_target -> gethandle();
1196+
t_touch -> target = t_target -> GetHandle();
11961197

11971198
s_touches = t_touch;
11981199
}
@@ -1202,7 +1203,7 @@ static void handle_touch(MCStack *p_stack, MCEventTouchPhase p_phase, uint32_t p
12021203
// Touches on widgets are handled differently
12031204
if (t_target->gettype() == CT_WIDGET)
12041205
{
1205-
MCwidgeteventmanager->event_touch(reinterpret_cast<MCWidget*>(t_target),
1206+
MCwidgeteventmanager->event_touch(MCObjectCast<MCWidget>(t_target),
12061207
p_id, p_phase, t_touch_loc.x, t_touch_loc.y);
12071208
}
12081209
else
@@ -1235,8 +1236,6 @@ static void clear_touches(void)
12351236
t_touch = s_touches;
12361237
s_touches = s_touches -> next;
12371238

1238-
t_touch -> target -> Release();
1239-
12401239
delete t_touch;
12411240
}
12421241
}

engine/src/exec-context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ class MCExecContext
339339

340340

341341
// MM-2011-02-16: Added ability to get handle of current object
342-
MCObjectHandle *GetObjectHandle(void);
342+
MCObjectHandle GetObjectHandle(void);
343343
void SetTheResultToEmpty(void);
344344
void SetTheResultToValue(MCValueRef p_value);
345345
void SetTheResultToStaticCString(const char *p_cstring);

engine/src/exec-interface2.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3767,10 +3767,10 @@ void MCInterfaceDoRelayer(MCExecContext& ctxt, int p_relation, MCObjectPtr p_sou
37673767
{
37683768
// As we call handlers that might invoke messages, we need to take
37693769
// object handles here.
3770-
MCObjectHandle *t_source_handle, *t_new_owner_handle, *t_new_target_handle;
3771-
t_source_handle = p_source . object -> gethandle();
3772-
t_new_owner_handle = t_new_owner -> gethandle();
3773-
t_new_target_handle = t_new_target != nil ? t_new_target -> gethandle() : nil;
3770+
MCObjectHandle t_source_handle, t_new_owner_handle, t_new_target_handle;
3771+
t_source_handle = p_source . object -> GetHandle();
3772+
t_new_owner_handle = t_new_owner -> GetHandle();
3773+
t_new_target_handle = t_new_target != NULL ? t_new_target -> GetHandle() : MCObjectHandle(NULL);
37743774

37753775
// Make sure we remove focus from the control.
37763776
bool t_was_mfocused, t_was_kfocused;
@@ -3783,10 +3783,10 @@ void MCInterfaceDoRelayer(MCExecContext& ctxt, int p_relation, MCObjectPtr p_sou
37833783

37843784
// Check the source and new owner objects exist, and if we have a target object
37853785
// that that exists and is still a child of new owner.
3786-
if (t_source_handle -> Exists() &&
3787-
t_new_owner_handle -> Exists() &&
3786+
if (t_source_handle.IsValid() &&
3787+
t_new_owner_handle.IsValid() &&
37883788
(t_new_target == nil ||
3789-
(t_new_target_handle -> Exists() &&
3789+
(t_new_target_handle.IsValid() &&
37903790
t_new_target -> getparent() == t_new_owner)))
37913791
{
37923792
p_source . object -> getparent() -> relayercontrol_remove(static_cast<MCControl *>(p_source . object));
@@ -3806,11 +3806,6 @@ void MCInterfaceDoRelayer(MCExecContext& ctxt, int p_relation, MCObjectPtr p_sou
38063806
t_card -> kfocus();
38073807
if (t_was_mfocused)
38083808
t_card -> mfocus(MCmousex, MCmousey);
3809-
3810-
t_source_handle -> Release();
3811-
t_new_owner_handle -> Release();
3812-
if (t_new_target != nil)
3813-
t_new_target_handle -> Release();
38143809
}
38153810

38163811
if (t_success)

engine/src/exec-sound.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void MCSoundExecPlaySoundOnChannel(MCExecContext& ctxt, MCStringRef p_channel, M
102102
bool t_success;
103103
t_success = true;
104104

105-
MCObjectHandle *t_handle;
105+
MCObjectHandle t_handle;
106106
t_handle = nil;
107107
if (t_success)
108108
t_handle = ctxt.GetObjectHandle();
@@ -112,8 +112,6 @@ void MCSoundExecPlaySoundOnChannel(MCExecContext& ctxt, MCStringRef p_channel, M
112112
if (!t_success)
113113
{
114114
ctxt.SetTheResultToStaticCString("could not play sound");
115-
if (t_handle != nil)
116-
t_handle->Release();
117115
}
118116
}
119117

engine/src/exec.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,10 +1217,10 @@ void MCExecContext::UserThrow(MCStringRef p_error)
12171217
m_stat = ES_ERROR;
12181218
}
12191219

1220-
MCObjectHandle *MCExecContext::GetObjectHandle(void) const
1220+
MCObjectHandle MCExecContext::GetObjectHandle(void) const
12211221
{
12221222
extern MCExecContext *MCECptr;
1223-
return MCECptr->GetObject()->gethandle();
1223+
return MCECptr->GetObject()->GetHandle();
12241224
}
12251225

12261226
Exec_stat MCExecContext::Catch(uint2 p_line, uint2 p_pos)

engine/src/exec.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1687,7 +1687,7 @@ class MCExecContext
16871687
}
16881688

16891689
// MM-2011-02-16: Added ability to get handle of current object
1690-
MCObjectHandle *GetObjectHandle(void) const;
1690+
MCObjectHandle GetObjectHandle(void) const;
16911691
void SetTheResultToEmpty(void);
16921692
void SetTheResultToValue(MCValueRef p_value);
16931693
void SetTheResultToStaticCString(const char *p_cstring);

0 commit comments

Comments
 (0)