Detect when the notification callback has destroyed the ListView to

avoid attempts to access data that is no longer valid.
This commit is contained in:
Troy Rollo 2005-10-21 15:49:36 +00:00 committed by Alexandre Julliard
parent 364c3aa449
commit c0b79e0b05
1 changed files with 58 additions and 24 deletions

View File

@ -768,10 +768,12 @@ static LRESULT notify_hdr(LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
return result; return result;
} }
static inline LRESULT notify(LISTVIEW_INFO *infoPtr, INT code) static inline BOOL notify(LISTVIEW_INFO *infoPtr, INT code)
{ {
NMHDR nmh; NMHDR nmh;
return notify_hdr(infoPtr, code, &nmh); HWND hwnd = infoPtr->hwndSelf;
notify_hdr(infoPtr, code, &nmh);
return IsWindow(hwnd);
} }
static inline void notify_itemactivate(LISTVIEW_INFO *infoPtr, LVHITTESTINFO *htInfo) static inline void notify_itemactivate(LISTVIEW_INFO *infoPtr, LVHITTESTINFO *htInfo)
@ -812,10 +814,11 @@ static inline LRESULT notify_listview(LISTVIEW_INFO *infoPtr, INT code, LPNMLIST
return notify_hdr(infoPtr, code, (LPNMHDR)plvnm); return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
} }
static LRESULT notify_click(LISTVIEW_INFO *infoPtr, INT code, LVHITTESTINFO *lvht) static BOOL notify_click(LISTVIEW_INFO *infoPtr, INT code, LVHITTESTINFO *lvht)
{ {
NMLISTVIEW nmlv; NMLISTVIEW nmlv;
LVITEMW item; LVITEMW item;
HWND hwnd = infoPtr->hwndSelf;
TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht)); TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
ZeroMemory(&nmlv, sizeof(nmlv)); ZeroMemory(&nmlv, sizeof(nmlv));
@ -826,13 +829,15 @@ static LRESULT notify_click(LISTVIEW_INFO *infoPtr, INT code, LVHITTESTINFO *lv
item.iItem = lvht->iItem; item.iItem = lvht->iItem;
item.iSubItem = 0; item.iSubItem = 0;
if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam; if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
return notify_listview(infoPtr, code, &nmlv); notify_listview(infoPtr, code, &nmlv);
return IsWindow(hwnd);
} }
static void notify_deleteitem(LISTVIEW_INFO *infoPtr, INT nItem) static BOOL notify_deleteitem(LISTVIEW_INFO *infoPtr, INT nItem)
{ {
NMLISTVIEW nmlv; NMLISTVIEW nmlv;
LVITEMW item; LVITEMW item;
HWND hwnd = infoPtr->hwndSelf;
ZeroMemory(&nmlv, sizeof (NMLISTVIEW)); ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
nmlv.iItem = nItem; nmlv.iItem = nItem;
@ -841,6 +846,7 @@ static void notify_deleteitem(LISTVIEW_INFO *infoPtr, INT nItem)
item.iSubItem = 0; item.iSubItem = 0;
if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam; if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
notify_listview(infoPtr, LVN_DELETEITEM, &nmlv); notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
return IsWindow(hwnd);
} }
static int get_ansi_notification(INT unicodeNotificationCode) static int get_ansi_notification(INT unicodeNotificationCode)
@ -3012,12 +3018,13 @@ static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT directi
* [I] nItem : item index * [I] nItem : item index
* *
* RETURN: * RETURN:
* None * Whether the window is still valid.
*/ */
static void LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem) static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
{ {
INT nFirst = min(infoPtr->nSelectionMark, nItem); INT nFirst = min(infoPtr->nSelectionMark, nItem);
INT nLast = max(infoPtr->nSelectionMark, nItem); INT nLast = max(infoPtr->nSelectionMark, nItem);
HWND hwndSelf = infoPtr->hwndSelf;
NMLVODSTATECHANGE nmlv; NMLVODSTATECHANGE nmlv;
LVITEMW item; LVITEMW item;
BOOL bOldChange; BOOL bOldChange;
@ -3046,7 +3053,10 @@ static void LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
nmlv.uOldState = item.state; nmlv.uOldState = item.state;
notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv); notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
if (!IsWindow(hwndSelf))
return FALSE;
infoPtr->bDoChangeNotify = bOldChange; infoPtr->bDoChangeNotify = bOldChange;
return TRUE;
} }
@ -3400,9 +3410,15 @@ static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL
/* send LVN_ITEMCHANGING notification, if the item is not being inserted */ /* send LVN_ITEMCHANGING notification, if the item is not being inserted */
/* and we are _NOT_ virtual (LVS_OWERNDATA), and change notifications */ /* and we are _NOT_ virtual (LVS_OWERNDATA), and change notifications */
/* are enabled */ /* are enabled */
if(lpItem && !isNew && infoPtr->bDoChangeNotify && if(lpItem && !isNew && infoPtr->bDoChangeNotify)
notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv)) {
HWND hwndSelf = infoPtr->hwndSelf;
if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
return FALSE; return FALSE;
if (!IsWindow(hwndSelf))
return FALSE;
}
/* copy information */ /* copy information */
if (lpLVItem->mask & LVIF_TEXT) if (lpLVItem->mask & LVIF_TEXT)
@ -3546,6 +3562,7 @@ static BOOL set_sub_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL i
static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW) static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
{ {
UINT uView = infoPtr->dwStyle & LVS_TYPEMASK; UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
HWND hwndSelf = infoPtr->hwndSelf;
LPWSTR pszText = NULL; LPWSTR pszText = NULL;
BOOL bResult, bChanged = FALSE; BOOL bResult, bChanged = FALSE;
@ -3568,6 +3585,8 @@ static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, B
bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged); bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
else else
bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged); bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
if (!IsWindow(hwndSelf))
return FALSE;
/* redraw item, if necessary */ /* redraw item, if necessary */
if (bChanged && !infoPtr->bIsDrawing) if (bChanged && !infoPtr->bIsDrawing)
@ -4516,7 +4535,7 @@ static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
LISTVIEW_SetItemState(infoPtr, nItem, &item); LISTVIEW_SetItemState(infoPtr, nItem, &item);
/* send LVN_DELETEITEM notification. */ /* send LVN_DELETEITEM notification. */
notify_deleteitem(infoPtr, nItem); if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
/* we need to do this here, because we'll be deleting stuff */ /* we need to do this here, because we'll be deleting stuff */
if (uView == LVS_SMALLICON || uView == LVS_ICON) if (uView == LVS_SMALLICON || uView == LVS_ICON)
@ -4568,6 +4587,7 @@ static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
*/ */
static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, LPWSTR pszText, BOOL isW) static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, LPWSTR pszText, BOOL isW)
{ {
HWND hwndSelf = infoPtr->hwndSelf;
NMLVDISPINFOW dispInfo; NMLVDISPINFOW dispInfo;
TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW); TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
@ -4585,6 +4605,8 @@ static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, LPWSTR pszText, BOOL
/* Do we need to update the Item Text */ /* Do we need to update the Item Text */
if (!notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW)) return FALSE; if (!notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW)) return FALSE;
if (!IsWindow(hwndSelf))
return FALSE;
if (!pszText) return TRUE; if (!pszText) return TRUE;
if (!(infoPtr->dwStyle & LVS_OWNERDATA)) if (!(infoPtr->dwStyle & LVS_OWNERDATA))
@ -4625,6 +4647,7 @@ static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
WCHAR szDispText[DISP_TEXT_SIZE] = { 0 }; WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
NMLVDISPINFOW dispInfo; NMLVDISPINFOW dispInfo;
RECT rect; RECT rect;
HWND hwndSelf = infoPtr->hwndSelf;
TRACE("(nItem=%d, isW=%d)\n", nItem, isW); TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
@ -4662,6 +4685,8 @@ static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW)) if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
{ {
if (!IsWindow(hwndSelf))
return 0;
SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0); SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
infoPtr->hwndEdit = 0; infoPtr->hwndEdit = 0;
return 0; return 0;
@ -6191,6 +6216,7 @@ static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
ITEM_INFO *lpItem; ITEM_INFO *lpItem;
BOOL is_sorted, has_changed; BOOL is_sorted, has_changed;
LVITEMW item; LVITEMW item;
HWND hwndSelf = infoPtr->hwndSelf;
TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW); TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
@ -6264,6 +6290,8 @@ static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
nmlv.iItem = nItem; nmlv.iItem = nItem;
nmlv.lParam = lpItem->lParam; nmlv.lParam = lpItem->lParam;
notify_listview(infoPtr, LVN_INSERTITEM, &nmlv); notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
if (!IsWindow(hwndSelf))
return -1;
/* align items (set position of each item) */ /* align items (set position of each item) */
if ((uView == LVS_SMALLICON || uView == LVS_ICON)) if ((uView == LVS_SMALLICON || uView == LVS_ICON))
@ -7967,6 +7995,7 @@ static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData) static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
{ {
UINT uView = infoPtr->dwStyle & LVS_TYPEMASK; UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
HWND hwndSelf = infoPtr->hwndSelf;
INT nItem = -1; INT nItem = -1;
NMLVKEYDOWN nmKeyDown; NMLVKEYDOWN nmKeyDown;
@ -7976,14 +8005,16 @@ static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lK
nmKeyDown.wVKey = nVirtualKey; nmKeyDown.wVKey = nVirtualKey;
nmKeyDown.flags = 0; nmKeyDown.flags = 0;
notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr); notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
if (!IsWindow(hwndSelf))
return 0;
switch (nVirtualKey) switch (nVirtualKey)
{ {
case VK_RETURN: case VK_RETURN:
if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1)) if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
{ {
notify(infoPtr, NM_RETURN); if (!notify(infoPtr, NM_RETURN)) return 0;
notify(infoPtr, LVN_ITEMACTIVATE); if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
} }
break; break;
@ -8069,7 +8100,7 @@ static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
if (!infoPtr->bFocus) return 0; if (!infoPtr->bFocus) return 0;
/* send NM_KILLFOCUS notification */ /* send NM_KILLFOCUS notification */
notify(infoPtr, NM_KILLFOCUS); if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
/* if we have a focus rectagle, get rid of it */ /* if we have a focus rectagle, get rid of it */
LISTVIEW_ShowFocusRect(infoPtr, FALSE); LISTVIEW_ShowFocusRect(infoPtr, FALSE);
@ -8102,14 +8133,14 @@ static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x,
TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y); TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
/* send NM_RELEASEDCAPTURE notification */ /* send NM_RELEASEDCAPTURE notification */
notify(infoPtr, NM_RELEASEDCAPTURE); if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
htInfo.pt.x = x; htInfo.pt.x = x;
htInfo.pt.y = y; htInfo.pt.y = y;
/* send NM_DBLCLK notification */ /* send NM_DBLCLK notification */
LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE); LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
notify_click(infoPtr, NM_DBLCLK, &htInfo); if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
/* To send the LVN_ITEMACTIVATE, it must be on an Item */ /* To send the LVN_ITEMACTIVATE, it must be on an Item */
if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo); if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
@ -8139,7 +8170,7 @@ static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, IN
TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y); TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
/* send NM_RELEASEDCAPTURE notification */ /* send NM_RELEASEDCAPTURE notification */
notify(infoPtr, NM_RELEASEDCAPTURE); if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf); if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
@ -8182,7 +8213,7 @@ static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, IN
{ {
if (bGroupSelect) if (bGroupSelect)
{ {
LISTVIEW_AddGroupSelection(infoPtr, nItem); if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
LISTVIEW_SetItemFocus(infoPtr, nItem); LISTVIEW_SetItemFocus(infoPtr, nItem);
infoPtr->nSelectionMark = nItem; infoPtr->nSelectionMark = nItem;
} }
@ -8257,7 +8288,7 @@ static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT
/* send NM_CLICK notification */ /* send NM_CLICK notification */
LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE); LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
notify_click(infoPtr, NM_CLICK, &lvHitTestInfo); if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
/* set left button flag */ /* set left button flag */
infoPtr->bLButtonDown = FALSE; infoPtr->bLButtonDown = FALSE;
@ -8332,6 +8363,7 @@ static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
static LRESULT LISTVIEW_HeaderNotification(LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh) static LRESULT LISTVIEW_HeaderNotification(LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
{ {
UINT uView = infoPtr->dwStyle & LVS_TYPEMASK; UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
HWND hwndSelf = infoPtr->hwndSelf;
TRACE("(lpnmh=%p)\n", lpnmh); TRACE("(lpnmh=%p)\n", lpnmh);
@ -8345,6 +8377,8 @@ static LRESULT LISTVIEW_HeaderNotification(LISTVIEW_INFO *infoPtr, const NMHEADE
case HDN_ITEMCHANGEDW: case HDN_ITEMCHANGEDW:
case HDN_ITEMCHANGEDA: case HDN_ITEMCHANGEDA:
notify_forward_header(infoPtr, lpnmh); notify_forward_header(infoPtr, lpnmh);
if (!IsWindow(hwndSelf))
break;
/* Fall through */ /* Fall through */
case HDN_TRACKW: case HDN_TRACKW:
case HDN_TRACKA: case HDN_TRACKA:
@ -8575,7 +8609,7 @@ static LRESULT LISTVIEW_RButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x,
TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y); TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
/* send NM_RELEASEDCAPTURE notification */ /* send NM_RELEASEDCAPTURE notification */
notify(infoPtr, NM_RELEASEDCAPTURE); if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
/* send NM_RDBLCLK notification */ /* send NM_RDBLCLK notification */
lvHitTestInfo.pt.x = x; lvHitTestInfo.pt.x = x;
@ -8606,7 +8640,7 @@ static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, IN
TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y); TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
/* send NM_RELEASEDCAPTURE notification */ /* send NM_RELEASEDCAPTURE notification */
notify(infoPtr, NM_RELEASEDCAPTURE); if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
/* make sure the listview control window has the focus */ /* make sure the listview control window has the focus */
if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf); if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
@ -8662,7 +8696,7 @@ static LRESULT LISTVIEW_RButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT
lvHitTestInfo.pt.x = x; lvHitTestInfo.pt.x = x;
lvHitTestInfo.pt.y = y; lvHitTestInfo.pt.y = y;
LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE); LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
notify_click(infoPtr, NM_RCLICK, &lvHitTestInfo); if (!notify_click(infoPtr, NM_RCLICK, &lvHitTestInfo)) return 0;
/* Change to screen coordinate for WM_CONTEXTMENU */ /* Change to screen coordinate for WM_CONTEXTMENU */
pt = lvHitTestInfo.pt; pt = lvHitTestInfo.pt;
@ -8725,7 +8759,7 @@ static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
if (infoPtr->bFocus) return 0; if (infoPtr->bFocus) return 0;
/* send NM_SETFOCUS notification */ /* send NM_SETFOCUS notification */
notify(infoPtr, NM_SETFOCUS); if (!notify(infoPtr, NM_SETFOCUS)) return 0;
/* set window focus flag */ /* set window focus flag */
infoPtr->bFocus = TRUE; infoPtr->bFocus = TRUE;