From 1e879aa8b614d581c3b48b3d10a8e8a45edd4eb7 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 2 Mar 2014 15:49:52 +0000 Subject: [PATCH] Don't crash in wxOSX wxDataViewChoiceRenderer if selection was cancelled. Check for the index validity and ignore the value of -1 as it is passed if the selection in the popup was cancelled. Closes #16017. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_3_0_BRANCH@76045 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + src/osx/cocoa/dataview.mm | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/docs/changes.txt b/docs/changes.txt index e352dc8c83..7753754b94 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -640,6 +640,7 @@ wxMSW: wxOSX: - Fix incorrect joystick detection in configure (Lauri Nurmi). +- Fix crash in wxDataViewCtrl when cancelling choice selection (hartwigw). - Add support for wxEVT_COMBOBOX_DROPDOWN and wxEVT_COMBOBOX_CLOSEUP events to wxOSX/Cocoa (Igor Korot). diff --git a/src/osx/cocoa/dataview.mm b/src/osx/cocoa/dataview.mm index e7af0fb007..790c54c38b 100644 --- a/src/osx/cocoa/dataview.mm +++ b/src/osx/cocoa/dataview.mm @@ -2876,8 +2876,19 @@ wxDataViewChoiceRenderer::OSXOnCellChanged(NSObject *value, { // At least under OS X 10.7 we get the index of the item selected and not // its string. + const long choiceIndex = ObjectToLong(value); + + // We can receive -1 if the selection was cancelled, just ignore it. + if ( choiceIndex == -1 ) + return; + + // If it's not -1, it must be valid, but avoid crashing in GetChoice() + // below if it isn't, for some reason. + wxCHECK_RET( choiceIndex >= 0 && (size_t)choiceIndex < GetChoices().size(), + wxS("Choice index out of range.") ); + wxDataViewModel *model = GetOwner()->GetOwner()->GetModel(); - model->ChangeValue(GetChoice(ObjectToLong(value)), item, col); + model->ChangeValue(GetChoice(choiceIndex), item, col); } bool wxDataViewChoiceRenderer::MacRender()