Issue with casting CString to LPARAM and recasting it to CString...
Hi,
I have developed a custom control with some customizations of the
List Control class of MFC (CListCtrl). I have another custom class
(MyItem) which is inherited from CItem. Each and every entry in the
list control is a MyItem.
Every time a new item is added into the LIst control we use the logic
of maintaining a unique sequence number which is a CString and member
of MyItem class. Here goes the implementation of the 2 main members of
MyItem class
void MyItem::SetListItemSequence(const CString& seq)
{
m_sequence = seq;
}
This member sets the newly added entries sequence value into the
member.
///////////////////////////////////////////////////////////////////////////////////////
CString* MyItem::GetListItemSequence()
{
return &m_sequence;
}
This member gets the added Sequence numbers Address. The caller of
this function is a member from my customized list control class, we
call this while inserting the item into the list control and we call
it like this - we first call the GetListItemSequence() from the insert
method lieke this-
SetItemSequence(nRow,(LPARAM)(LPCTSTR)item.GetListItemSequence());
here row is the row number of the newly added list entry. and item is
the object of MyItem class. So calling the MyItem's
GetListItemSequence() which actually returns the address of the
sequence number. Converting this into LPARAM like shown above. Is this
a issue?????? That SetItemSequence() is a member of MyListControl
class which takes the address and sets the row sequence with the type
casted LPARAM value.
void MyListControl::SetItemSequence(int item,LPARAM lParam)
{
LVITEM pItem;
InitLVITEM(item,0,&pItem);
LS_item *lpLS_row = (LS_item*) pItem.lParam;
if(lpLS_row)
{
lpLS_row->pSequence = lParam;
}
}
Now my isssue here is - when ever the user double clicks on any
particualar item i will have to show him the contents of the item. So
I need to know the sequnce number so i implemented another member like
this-
CString CListCtrlStyled::GetItemSequence(int item)
{
CString retVal;
LPARAM rc = 0;
LVITEM pItem;
InitLVITEM(item,0,&pItem);
LS_item *lpLS_row = (LS_item*) pItem.lParam;
if(lpLS_row)
{
if (lpLS_row->pSequence)
retVal = *(CString*)(lpLS_row->pSequence);
}
return retVal;
}
Now this function works fine almost all cases. But it does crash
sometime and it crashes right at the place where the deferencing is
happening. I see that the adress value i.e. lpLS_row->pSequence is
fine. (checked this using log files) but the content may b is bad or
somthing. CString conversion code crashes as it finds the srcString is
"".(empty ??? may b?? not sure). Can any one please help me out. I am
blown off with the ideas. If it fails at every instance I can fix this
may b :) but it crashes once in a while. I feel there is some type
casting issue with CString to LPARAM and later dereferencing it. Any
help will b more than thankful.