重写CListCtrl更改行高,但出现内存泄露,请大家帮我看看代码

zhou1xp 2010-04-09 09:23:06
我的代码如下:
void newctrl::SetItemHeight(UINT nHeight)
{
m_nItemHeight = nHeight;
CRect rcWin;
GetWindowRect(&rcWin);
// GetClientRect(rcWin);
WINDOWPOS wp;
wp.hwnd = m_hWnd;
wp.cx = rcWin.Width();
wp.cy = rcWin.Height();
wp.flags = SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER;
SendMessage(WM_WINDOWPOSCHANGED, 0, (LPARAM)&wp);
}

void newctrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
TCHAR lpBuffer[256];

LV_ITEM lvi;

lvi.mask = LVIF_TEXT | LVIF_PARAM ;
lvi.iItem = lpDrawItemStruct->itemID ;
lvi.iSubItem = 0;
lvi.pszText = lpBuffer ;
lvi.cchTextMax = sizeof(lpBuffer);
VERIFY(GetItem(&lvi));

LV_COLUMN lvc, lvcprev ;
::ZeroMemory(&lvc, sizeof(lvc));
::ZeroMemory(&lvcprev, sizeof(lvcprev));
lvc.mask = LVCF_WIDTH | LVCF_FMT;
lvcprev.mask = LVCF_WIDTH | LVCF_FMT;

for ( int nCol=0; GetColumn(nCol, &lvc); nCol++)
{
if ( nCol > 0 )
{
// Get Previous Column Width in order to move the next display item
GetColumn(nCol-1, &lvcprev) ;
lpDrawItemStruct->rcItem.left += lvcprev.cx ;
lpDrawItemStruct->rcItem.right += lpDrawItemStruct->rcItem.left ;
}

// Get the text
::ZeroMemory(&lvi, sizeof(lvi));
lvi.iItem = lpDrawItemStruct->itemID;
lvi.mask = LVIF_TEXT | LVIF_PARAM;
lvi.iSubItem = nCol;
lvi.pszText = lpBuffer;
lvi.cchTextMax = sizeof(lpBuffer);
VERIFY(GetItem(&lvi));

CDC* pDC;
pDC = CDC::FromHandle(lpDrawItemStruct->hDC);

if ( lpDrawItemStruct->itemState & ODS_SELECTED )
{
pDC->FillSolidRect(&lpDrawItemStruct->rcItem, GetSysColor(COLOR_HIGHLIGHT)) ;
pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT)) ;
}
else
{
pDC->FillSolidRect(&lpDrawItemStruct->rcItem, GetSysColor(COLOR_WINDOW)) ;
// pDC->FillSolidRect(&lpDrawItemStruct->rcItem,RGB(38,123,165)) ;
pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT)) ;
}

pDC->SelectObject(GetStockObject(SYSTEM_FONT));

UINT uFormat=DT_LEFT ;

::DrawText(lpDrawItemStruct->hDC, lpBuffer, wcslen(lpBuffer),
&lpDrawItemStruct->rcItem, uFormat) ;

pDC->SelectStockObject(SYSTEM_FONT) ;
}
}

void newctrl::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
if(m_nItemHeight>0)
lpMeasureItemStruct->itemHeight=m_nItemHeight;
}


每次我从数据库中加载数据后,会有20K的内存泄露
...全文
154 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eleven 2010-04-09
  • 打赏
  • 举报
回复
除了ls说的,还有你的GetStockObject()函数的调用,最后最好也DeleteObject()一下;MSDN原话
Use the DKGRAY_BRUSH, GRAY_BRUSH, and LTGRAY_BRUSH stock objects only in windows with the CS_HREDRAW and CS_VREDRAW styles. Using a gray stock brush in any other style of window can lead to misalignment of brush patterns after a window is moved or sized. The origins of stock brushes cannot be adjusted.

The HOLLOW_BRUSH and NULL_BRUSH stock objects are equivalent.

The font used by the DEFAULT_GUI_FONT stock object could change. Use this stock object when you want to use the font that menus, dialog boxes, and other user interface objects use.

It is not necessary (but it is not harmful) to delete stock objects by calling DeleteObject.
SullenSun 2010-04-09
  • 打赏
  • 举报
回复
没有发现。。。
Jimmy_Xia 2010-04-09
  • 打赏
  • 举报
回复
看了给出的代码,有问题的地方:
pDC->SelectObject(GetStockObject(SYSTEM_FONT));
这个需要保存返回值的,用完之后再SelectObject回去。
另外,这句
pDC->SelectStockObject(SYSTEM_FONT) ;
没用过,不知道是不是需要注意。
LZ先把这两句注释掉,看看有没有泄漏,就能确定是不是这两句代码的问题。
向立天 2010-04-09
  • 打赏
  • 举报
回复
你贴的这些代码应该不会引起内存泄露的
z8y8x 2010-04-09
  • 打赏
  • 举报
回复
不董,呵呵,用clistctrl好像没有用过zeromemory
fandh 2010-04-09
  • 打赏
  • 举报
回复
对应的是那一句new?

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧