您的位置:程序门 -> vc/mfc -> 图形处理/算法



内存是如何泄漏的?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


内存是如何泄漏的?[已结贴,结贴人:jiaon]
发表于:2007-06-17 16:29:04 楼主
下面是一个钟表程序的一部分,模拟秒针旋转所以要旋转位图。在ontimer里有内存泄露,ontimer每秒执行一次,能看到内存明显增长。可能是旋转位图引起的,请各位大虾看看如何改正。thanks。

//几个成员变量:
cdc   m_dcsecpointer;//the   second   pointer
cpoint   m_clockcenter;//the   clock   pointer   center
cdc   m_dcbkground;//the   back   ground

//ontimer函数,里面调用了rotatebitmap()函数
void   cclockdlg::ontimer(uint   nidevent)  
{
ctime   time=ctime::getcurrenttime();
int   nsec=time.getsecond();
float   fangle;
cpoint   points;
hdc   dcs;
int   nws=sec_width;
int   nhs=sec_height;

switch(nidevent)
{
case   main_timer:

fangle=nsec*6*pi/180;
points.y=-sec_height*cos(fangle)/2;
points.x=sec_height*sin(fangle)/2;
points=points+m_clockcenter;

dcs=rotatebitmap(m_dcsecpointer,nws,nhs,points,fangle);

getdc()-> stretchblt(0,0,m_clockcenter.x*2,2*m_clockcenter.y,&m_dcbkground,0,0,m_clockcenter.x*2,2*m_clockcenter.y,srccopy);
transparentblt(getdc()-> m_hdc,points.x,points.y,nws,nhs,dcs,0,0,nws,nhs,rgb(0,0,0));

deletedc(dcs);

break;
}

cdialog::ontimer(nidevent);
}

//旋转位图的函数
hdc   cclockdlg::rotatebitmap(
hdc   dcsrc,//source   bitmap
int   &srcwidth,
//[in]width   of   source   bitmap
//[out]width   of   the   rotated   bitmap
int   &srcheight,
//[in]height   of   source   bitmap
//[out]height   of   the   rotated   bitmap
cpoint   &cpcenter,
//[in]the   rotated   bitmap 's   center   wanted   be   shown
//[out]the   rotated   bitmap 's   left   top   corner
double   dangle//angle   of   rotation   and   y   axis
)
{//return   value:rotated   bitmap   is   temporarily   stored   here
double   x1,x2,x3,y1,y2,y3;
double   maxwidth,maxheight,minwidth,minheight;
double   srcx,srcy;
double   sina,cosa;
double   dstwidth,dstheight;
hdc   tmpdcdst;//temp   dc   for   rotate
hbitmap   newbitmap;
sina   =   sin(dangle);
cosa   =   cos(dangle);
x1   =   -srcheight   *   sina;
y1   =   srcheight   *   cosa;
x2   =   srcwidth   *   cosa   -   srcheight   *   sina;
y2   =   srcheight   *   cosa   +   srcwidth   *   sina;
x3   =   srcwidth   *   cosa;
y3   =   srcwidth   *   sina;
minwidth   =   x3> (x1> x2?x2:x1)?(x1> x2?x2:x1):x3;
minwidth   =   minwidth> 0?0:minwidth;
minheight   =   y3> (y1> y2?y2:y1)?(y1> y2?y2:y1):y3;
minheight   =   minheight> 0?0:minheight;
maxwidth   =   x3> (x1> x2?x1:x2)?x3:(x1> x2?x1:x2);
maxwidth   =   maxwidth> 0?maxwidth:0;
maxheight   =   y3> (y1> y2?y1:y2)?y3:(y1> y2?y1:y2);
maxheight   =   maxheight> 0?maxheight:0;
dstwidth   =   maxwidth   -   minwidth;
dstheight   =   maxheight   -   minheight;

//prepare   a   temp   empty   bitmap   for   drawing
tmpdcdst   =   createcompatibledc(dcsrc);
newbitmap   =   createcompatiblebitmap(dcsrc,(int)dstwidth,(int)dstheight);
selectobject(tmpdcdst,newbitmap);

//copy   to   the   temp   bitmap   from   source   bitmap
for(   int   i   =   0   ;i <dstheight;i++){
for(int   j   =   0   ;j <dstwidth;j++){
srcx   =   (j   +   minwidth)   *   cosa   +   (i   +   minheight)   *   sina;
srcy   =   (i   +   minheight)   *   cosa   -   (j   +   minwidth)   *   sina;
if(   (srcx   > =   0)   &&   (srcx   <=   srcwidth)   &&(srcy   > =   0)   &&   (srcy   <=   srcheight))
{
bitblt(tmpdcdst,   j,   i,   1,   1,   dcsrc,(int)srcx,   (int)srcy,   srcinvert);
}
}
}

deleteobject(newbitmap);

//return   values
srcwidth=dstwidth;
srcheight=dstheight;
cpcenter.x=cpcenter.x-dstwidth/2;
cpcenter.y=cpcenter.y-dstheight/2;
return   tmpdcdst;
}
发表于:2007-06-18 23:35:521楼 得分:0
你在rotatebitmap中好像只deleteobject,但是没有deletedc,那个tmpdcdst怎么办?
发表于:2007-06-19 08:14:142楼 得分:0
1、tmpdcdst   =   createcompatibledc(dcsrc);
该句产生的对象在哪删除的?

2、绘图部分应该放在ondraw里。
发表于:2007-06-19 15:58:233楼 得分:0
这个tmpdcdst是要返回的,不能在rotatebitmap里面删除。
dcs=rotatebitmap(m_dcsecpointer,nws,nhs,points,fangle);
...
deletedc(dcs);
这个在外面删了。不知行不行。

这是对话框的程序,没有ondraw,   放onpaint里行吗。


发表于:2007-06-19 16:52:274楼 得分:0
行,但创建资源的语句,最好不要放onpaint里。
发表于:2007-06-19 17:53:135楼 得分:0
那个tmpdcdst应该怎样删除呀?
发表于:2007-06-20 18:57:056楼 得分:0
使用deletedc

你用完了它肯定要删除啊,记录每个api调用的返回值,看看有没有delete不成功的情况。
发表于:2007-06-20 19:06:107楼 得分:20
而且你的程序中到处都有   getdc()-> xxx   这种语句,也很容易导致泄漏。

我没记错的话,getdc()之后都是要releasedc()的。




快速检索

最新资讯
热门点击