Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with OnPaint

Status
Not open for further replies.

agrund

Programmer
Oct 27, 2003
4
DE
Hi.

I've got a problem with painting into a window. I draw a background image from a bitmap, then overlay it with various graphs. This works fine, but when the last graph finishes drawing, everything disappears, leaving behind a blank white window. The application is an MDI application, created using Microsoft's Visual C++ 6.0.

Does anybody have any idea why this happens, and how I can change this?

CU,
André
 
I think you have memory leaks, but there could be many reasons.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Hi.

Could you give me some advice on how to find those memory leaks, or perhaps where else to look for problems?

CU,
André
 
Show us your OnPaint code.

/Per
[sub]
"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."[/sub]
 
Hi.

I'm not sure. It's quite a bit, and I haven't had the leisure to comment it yet. In addition, most of the actual painting is done by other functions. But we'll see.

void CChildFrame::OnPaint()
{
CPaintDC dc(this); // device context for painting
int start=0;
int end=0;
if (s_reg_on)
{
for (int i=0;i<=XLoader.regions.size();i++)
{
if (XLoader.regions.size())
{

for (int in=0;in<=DLoader.DataStreams[0]->GetLength();in+=50)
{
int currentX=DLoader.DataStreams[0]->GetValue(in,3);
int currentY=DLoader.DataStreams[0]->GetValue(in,4);
bool inside=false;
for (int r=0;r<XLoader.regions.size();r++)
{
if (currentX<=XLoader.regions[r].coordinates.right && currentX>=XLoader.regions[r].coordinates.left && currentY<=XLoader.regions[r].coordinates.bottom && currentY>=XLoader.regions[r].coordinates.top )
inside=true;
}
if (inside==true) eyeinside.push_back(true);
else eyeinside.push_back(false);
}
for (in=0;in<=DLoader.DataStreams[0]->GetLength();in+=50)
{
int currentX=DLoader.DataStreams[0]->GetValue(in,1);
int currentY=DLoader.DataStreams[0]->GetValue(in,2);
bool inside=false;
for (int r=0;r<XLoader.regions.size();r++)
{
if (currentX<=XLoader.regions[r].coordinates.bottom && currentX>=XLoader.regions[r].coordinates.top && currentY<=XLoader.regions[r].coordinates.right && currentY>=XLoader.regions[r].coordinates.left)
inside=true;
}
if (inside==true) mouseinside.push_back(true);
else mouseinside.push_back(false);
}
CPen pen1;
pen1.CreatePen(PS_SOLID,3,RGB(0,0,0));
dc.SelectObject(pen1);
dc.MoveTo(XLoader.regions.coordinates.left,XLoader.regions.coordinates.top);
dc.LineTo(XLoader.regions.coordinates.right,XLoader.regions.coordinates.top);
dc.MoveTo(XLoader.regions.coordinates.right,XLoader.regions.coordinates.top);
dc.LineTo(XLoader.regions.coordinates.right,XLoader.regions.coordinates.bottom);
dc.MoveTo(XLoader.regions.coordinates.right,XLoader.regions.coordinates.bottom);
dc.LineTo(XLoader.regions.coordinates.left,XLoader.regions.coordinates.bottom);
dc.MoveTo(XLoader.regions.coordinates.left,XLoader.regions.coordinates.bottom);
dc.LineTo(XLoader.regions.coordinates.left,XLoader.regions.coordinates.top);
}
}
}
if (s_seq_on)
{
for (int j=0;j<XLoader.Tasks.size();j++)
{
if (XLoader.Tasks.size())
{

CString StartS;
CString EndS;
Task t=XLoader.Tasks.at(j);
StartS=t.start;
EndS=t.end;
char currentchar;
bool min=true;
bool sec=false;
bool mil=false;
CString hs;
int h;
int c = 0;
int dpos[4]; hs.Format(&quot;%s&quot;, StartS);
for (int i = 1; i <= 2; i++)
{
if (h != -1)
{
h = hs.Find(&quot;:&quot;, c);
c = h + 1;
dpos = h;
}
}
CString s_m, s_s, s_ms;
if (dpos[1] != 0) s_m = hs.Mid(0, dpos[1]); else s_m = &quot;&quot;;
if ( dpos[2] != (dpos[1]+1) )
{
s_s = hs.Mid( dpos[1] + 1, (dpos[2] - dpos[1] - 1) );
}
else s_s = &quot;&quot;;
if ( dpos[3] != (dpos[2]+1) )
{
s_ms = hs.Mid( dpos[2] + 1, (dpos[3] - dpos[2] - 1) );
}
else s_ms = &quot;&quot;;

long start = atoi(s_m)*60000 + atoi(s_s)*1000 + atoi(s_ms);

hs.Format(&quot;%s&quot;, EndS);
for (int j = 1; j <= 2; j++)
{
if (h != -1)
{
h = hs.Find(&quot;:&quot;, c);
c = h + 1;
dpos = h;
}
}
if (dpos[1] != 0) s_m = hs.Mid(0, dpos[1]); else s_m = &quot;&quot;;
if ( dpos[2] != (dpos[1]+1) )
{
s_s = hs.Mid( dpos[1] + 1, (dpos[2] - dpos[1] - 1) );
}
else s_s = &quot;&quot;;
if ( dpos[3] != (dpos[2]+1) )
{
s_ms = hs.Mid( dpos[2] + 1, (dpos[3] - dpos[2] - 1) );
}
else s_ms = &quot;&quot;;


long end = atoi(s_m)*60000 + atoi(s_s)*1000 + atoi(s_ms);

}
}
}
if (bm.bmWidth>0)
{
if (end==0) end=DLoader.DataStreams[0]->GetLength();
if (s_repro)
{
dc.DrawState(CPoint(0,0),CSize(bm.bmHeight,bm.bmWidth),&m_bmpBitmap,DST_BITMAP|DSS_NORMAL,NULL);
MethodRepro mrepro;
mrepro.Calculate(s_eyedata,s_mousedata,s_mouseevents,s_keyevents,s_browserevents,&dc,&DLoader, bm, start, end, eyeinside, mouseinside);
}
if (s_auf_matrix)
{
dc.DrawState(CPoint(0,0),CSize(bm.bmHeight,bm.bmWidth),&m_bmpBitmap,DST_BITMAP|DSS_NORMAL,NULL);
MethodAttMatrix matrix;
matrix.Calculate(s_eyedata,s_mousedata, s_mouseevents, s_keyevents, s_browserevents,&dc,&DLoader, bm, start, end);
}
if (s_fixation)
{
dc.DrawState(CPoint(0,0),CSize(bm.bmHeight,bm.bmWidth),&m_bmpBitmap,DST_BITMAP|DSS_NORMAL,NULL);
MethodFixation fix;
fix.Calculate(s_eyedata,s_mousedata, s_mouseevents, s_keyevents, s_browserevents,&dc,&DLoader, bm, start, end, eyeinside, mouseinside);
}
}
}

Hope you can make something of it.

CU,
André
 
1.
&quot;One possible cause of the memory leak is that MFC creates temporary objects that are used inside message handler functions. In regular DLLs, MFC does not automatically release memory allocated for these objects.&quot;
As Ion said could be a memory leak.
Call _CrtCheckMemory() with _CRTDBG_LEAK_CHECK_DF flag set just before you finish the paint.
2.
Look om how you are using the worker threads if your application uses CWinThread instances.
-obislavu-

 
Hi.

I tried what you said, obislavu, and _CrtCheckMemory() executes properly. According to the MSDN Library it should create a debug report. How can I view that report? Sorry if the question is trivial, but I couldn't find it.

CU,
André
 
Add the code

ReleaseDC(&dc);

to the end to release the dc you created. Even though it goes out of scope, I believe that MFC has done some behind the seens operations that has some memory still allocated.
 
Also when you use the SelectObject function it returns a pointer to the old object. Reselect this object before the one you created goes out of scope. For example:

CPen pen1, *penOld;
penOld = pen1.CreatePen(PS_SOLID,3,RGB(0,0,0));
dc.SelectObject(&pen1);

// ....processes using the pen1 object

dc.SelectObject(penOld);

If you do not do this you loose memory/gdi resources.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top