Re: CListCtrl, custom item data and allocation policy

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"Mosfet" <anonymous@xxxxxxx> wrote in message
news:478e6385$0$2739$426a74cc@xxxxxxxxxxxxxxx
Hi,

When using a CListCtrl you can associate some custom data via the
SetItemData method.
The problem is it takes a DWORD argumentr that can reference a value or a
pointer to an allocated structure.
When destroying this CListCtrl is there any ways of determining if it
needs to be deallocated.

For instance, let's say I have a CMyListCtrl inheriting from CListCtrl
used in two different dialogs.
In one dialog I call SetItemData passing a DWORD value representing a
numeric value and in the second one I pass a pointer to an allocated chunk
of memory.

In the destructor how doI know if I need to call delete ?


I would use the item data to point always to the same kind of structure,
say, MyStruct, which includes both the numeric item as well as a pointer to
the allocated structure. If the allocated structure does not exist, the
pointer should be set to NULL. Then you can always call delete on the
pointer, since deletion of a NULL pointer is always allowed.

Of course, with this method, you would always need to allocate a MyStruct
for each item, and always delete it too. However, the MyStruct structure
would be small (maybe only 12 bytes in this example, 4 for the integer and 8
for the pointer) and presumably would be so much smaller than the ultimate
structure, that this technique would make sense.


.



Relevant Pages

  • Re: CListCtrl, custom item data and allocation policy
    ... get it in memory instead of waiting for a LVM_GETITEM message to complete.. ... a pointer to an allocated structure. ... When destroying this CListCtrl is there any ways of determining if it ...
    (microsoft.public.vc.mfc)
  • CListCtrl, custom item data and allocation policy
    ... When using a CListCtrl you can associate some custom data via the SetItemData method. ... The problem is it takes a DWORD argumentr that can reference a value or a pointer to an allocated structure. ... When destroying this CListCtrl is there any ways of determining if it needs to be deallocated. ...
    (microsoft.public.vc.mfc)
  • How to Identify the Item and Subitem that has been Clicked in a CListCtrl
    ... I am trying to identify the row and column in a CListCtrl when the user ... signature will have an LPNMITEMACTIVATE struct pointer that will contain the ... item and subitem that has been clicked on. ...
    (microsoft.public.vc.mfc)
  • Re: Adding the ability to add functions into structures?
    ... language like C++ that support OO directly, is that you have to manually build and initialize the function table yourself; the compiler won't do it for you. ... to an allocated structure. ... pointer to another function. ...
    (comp.lang.c)
  • Re: Debugging corrupted memoy
    ... allocated structure which must be buggy since after its execution the value ... programm I get a SIGSEGV when deferencing the pointer and valgrind shows no ... And then the '*' in front of the calloc() call is definitely ... Please report the exact source code you were using plus the ...
    (comp.lang.c)