Click here to Skip to main content
15,891,905 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Why do MS prefer to provide a new function, CWnd::CreateEx, rather than override CWnd::Create? Pin
Antti Keskinen4-Aug-04 22:12
Antti Keskinen4-Aug-04 22:12 
GeneralGetItemText returns bad CString & can't change subitem color in CustomDraw Pin
Mike Landis4-Aug-04 19:34
Mike Landis4-Aug-04 19:34 
GeneralRe: GetItemText returns bad CString & can't change subitem color in CustomDraw Pin
Tomasz Sowinski5-Aug-04 0:15
Tomasz Sowinski5-Aug-04 0:15 
GeneralRe: GetItemText returns bad CString & can't change subitem color in CustomDraw Pin
Mike Landis5-Aug-04 1:40
Mike Landis5-Aug-04 1:40 
GeneralRe: GetItemText returns bad CString & can't change subitem color in CustomDraw Pin
Tomasz Sowinski5-Aug-04 1:51
Tomasz Sowinski5-Aug-04 1:51 
GeneralRe: GetItemText returns bad CString & can't change subitem color in CustomDraw Pin
Mike Landis5-Aug-04 6:33
Mike Landis5-Aug-04 6:33 
GeneralRe: GetItemText returns bad CString & can't change subitem color in CustomDraw Pin
Tomasz Sowinski5-Aug-04 7:39
Tomasz Sowinski5-Aug-04 7:39 
GeneralRe: GetItemText returns bad CString & can't change subitem color in CustomDraw Pin
Mike Landis5-Aug-04 9:10
Mike Landis5-Aug-04 9:10 
There are real MFC objects behind the GetDlgItem call, so I think the CWnd*s are real. I use the (cast)ptrs to call CCtrlList, CEdit, etc. functions. I'm not wrapping anything, just subclassing, so I hope I haven't killed NM_CUSTOMDRAW passthrough. The reason I don't suspect OnGetDispInfoList() is because the control behaves quite rationally in all other cases. I can scroll the view generating update events and it handily fetches the right stuff from OnGetDispInfoList() and displays it in the control.

The only other code I know is:

const unsigned short StringPoolSize = 3;
unsigned short nextFreeSlot;
CString cstPool[StringPoolSize ];
LPTSTR pBufPool[StringPoolSize ];

from the PosDialog's .h file and GetItemText in the ListCtrl itself, excerpted here:

CString CListCtrl::GetItemText(int nItem, int nSubItem) const
{
ASSERT(::IsWindow(m_hWnd));
LVITEM lvi;
memset(&lvi, 0, sizeof(LVITEM));
lvi.iSubItem = nSubItem;
CString str;
int nLen = 128;
int nRes;
do
{
nLen *= 2;
lvi.cchTextMax = nLen;
lvi.pszText = str.GetBufferSetLength(nLen);
nRes = (int)::SendMessage(m_hWnd, LVM_GETITEMTEXT, (WPARAM)nItem,
(LPARAM)&lvi);
} while (nRes == nLen-1);
str.ReleaseBuffer();
return str;
}

The chain of events is: the CustomDraw handler calls GetItemText which sends a LVM_GETITEMTEXT message, received by GetDispInfoPosList which puts the CString in the pool (calls AddPool). AddPool plays with the reference count so the CString can't be released and sends back its pool copy. The LPTSTR retVal also looks good every time and its still okay when pDispInfo->item.pszText is assigned in OnGetDispInfoPosList(). Then you're back in _AfxDispatchCmdMsg (which can hardly be screwed up). str still shows the correct value after ReleaseBuffer in GetItemText and is good when you are at the final brace. The next thing you know, you're in OnCustomDraw and the string is gone. Apparently ReleaseBuffer decrements the reference count to 0 and the string goes away when the stack unrolls.

Thinking that Blazczak's pooling didn't anticipate CustomDraw calling GetItemText(), ultimately leading to one ReleaseBuffer() call he hadn't anticipated, I tried setting:

const unsigned short StringPoolSize = 4;

in the PosDialog's .h file and modifying the circular indexing in AddPool to use StringPoolSize (instead of 3), so it now reads:

if ( ++nextFreeSlot > StringPoolSize-1 )
nextFreeSlot = 0;

No change was evident. As soon as you return from GetItemText(), where the reference count on the string was decremented by ReleaseBuffer, its gone.

There's no other code. The answer (I think) is getting the reference count to be > 1 when GetItemText releases the buffer, because str is still okay when GetItemText is about to return (still at the top of the stack).

On the color, the evc3 help doesn't show you the structure under the m_hAttribDC. In the debugger, I can see inside dc to the m_hAttribDC, but only see 'unused' within. Not sure what to do with that.

-Mike
GeneralRe: GetItemText returns bad CString & can't change subitem color in CustomDraw Pin
Tomasz Sowinski5-Aug-04 9:27
Tomasz Sowinski5-Aug-04 9:27 
GeneralRe: GetItemText returns bad CString & can't change subitem color in CustomDraw Pin
Mike Landis5-Aug-04 9:57
Mike Landis5-Aug-04 9:57 
GeneralRe: GetItemText returns bad CString & can't change subitem color in CustomDraw Pin
Tomasz Sowinski5-Aug-04 10:14
Tomasz Sowinski5-Aug-04 10:14 
GeneralRe: GetItemText returns bad CString & can't change subitem color in CustomDraw Pin
Mike Landis5-Aug-04 10:49
Mike Landis5-Aug-04 10:49 
GeneralRe: GetItemText returns bad CString & can't change subitem color in CustomDraw Pin
Tomasz Sowinski5-Aug-04 11:03
Tomasz Sowinski5-Aug-04 11:03 
GeneralRe: GetItemText returns bad CString & can't change subitem color in CustomDraw Pin
Tomasz Sowinski6-Aug-04 1:17
Tomasz Sowinski6-Aug-04 1:17 
GeneralRe: GetItemText returns bad CString & can't change subitem color in CustomDraw Pin
Tomasz Sowinski5-Aug-04 2:18
Tomasz Sowinski5-Aug-04 2:18 
Generalthread stack overflow problem.. Pin
TssPrasad4-Aug-04 17:47
sussTssPrasad4-Aug-04 17:47 
GeneralRe: thread stack overflow problem.. Pin
palbano4-Aug-04 18:04
palbano4-Aug-04 18:04 
GeneralRe: thread stack overflow problem.. Pin
TssPrasad4-Aug-04 18:13
sussTssPrasad4-Aug-04 18:13 
GeneralRe: thread stack overflow problem.. Pin
Jijo.Raj4-Aug-04 19:01
Jijo.Raj4-Aug-04 19:01 
GeneralHelp needed!!!! Pin
pundit4-Aug-04 17:47
pundit4-Aug-04 17:47 
GeneralRe: Help needed!!!! Pin
Antti Keskinen4-Aug-04 22:15
Antti Keskinen4-Aug-04 22:15 
QuestionHow to distinguish file from directory ? Pin
Amarelia4-Aug-04 17:23
Amarelia4-Aug-04 17:23 
AnswerRe: How to distinguish file from directory ? Pin
PJ Arends4-Aug-04 17:52
professionalPJ Arends4-Aug-04 17:52 
AnswerRe: How to distinguish file from directory ? Pin
Shiva Prasad4-Aug-04 17:59
Shiva Prasad4-Aug-04 17:59 
GeneralRe: How to distinguish file from directory ? Pin
PJ Arends4-Aug-04 18:16
professionalPJ Arends4-Aug-04 18:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.