CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Threaded View

  1. #1
    Join Date
    Jun 2008
    Posts
    5

    CListCtrl selection changing handling

    Hello everyone,

    I have a problem handling the selection change message for my CListCtrl. I subscribe to the LVN_ITEMCHANGING message in my dialog, perform some checks which determine whether the item should be allowed to change, and return TRUE if the selected item should not be the new selection, and FALSE if it can be.

    The problem I am having is that this introduces heap corruption. I have tested the code by calling the handler with arguments which would normally be expected by the handler from the windows message (as well as testing the code piece by piece outside of the message handler), but then I have no heap corruption, therefore I believe it is not my processing code causing the problem (hopefully). For some reason it looks like during regular message handling the heap is corrupted someway, so could anyone confirm for me that I am going about this the right way? The program crashes with a corrupted heap after several selection changes are processed. The following is some code which demonstrates what I am doing, and included comments ask some of the questions I have.

    Code:
    static bool isItemSelected(UINT state)
    {
        if((state & LVIS_SELECTED) == LVIS_SELECTED)
             return true;
        return false;
    }
    
    void MyPage::OnItemChanging(NMHDR* pNMHDR, LRESULT* pResult)
    {
        LPNMLISTVIEW item = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
    
        // Is this the right way to check that this message is actually changing the item now?
        // The reason I ask is that for every single change attempt, multiple message are sent,
        // and I assume not all of them need to be handled?
        if (!isItemSelected(item->uOldState) && isItemSelected(item->uNewState))
        {
              ... // Do my object initialization...
              // Check if objects associated with the selection initialized properly
              // which dictates if the item can be the new selection.
              if(!success)
              {
                   *pResult = TRUE; // No success, prevent the list from changing selection.
                   return;
              }
        }
    
        // For anything else set the result to FALSE.
        // Should the result be FALSE for all messages sent for each selection change,
        // even if during one call I return TRUE as in above part of the code?
        *pResult = FALSE;
    }
    I would appreciate any help I can get with this.
    Thanks!
    Last edited by mutated; December 4th, 2009 at 12:49 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured