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!

What's with my source code

Status
Not open for further replies.

coolbass

Programmer
Nov 7, 2002
11
PH
hello guys

i need some help with my source code. i have this memory leak after the OnPaint():

void CThesisTwoDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this);
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{

COLORREF Color = RGB(255,255,255);

COLORREF ColorPen = RGB(0,0,0);
CBrush Brush(Color);
CPaintDC DrawA(this); // device context for painting


//SENSOR Array Column 1 Graph



for (int x=0; x<=m_count; x++)
{
DrawA.SelectObject(CreatePen(PS_SOLID,0,ColorPen));

DrawA.MoveTo(264+(xaxis-x)*8,118);
DrawA.LineTo(264+(xaxis-x)*8,118-m_xy1[m_count-x]*2);

DrawA.MoveTo(264+(xaxis-x)*8,218);
DrawA.LineTo(264+(xaxis-x)*8,218-m_xy2[m_count-x]*2);

DrawA.MoveTo(264+(xaxis-x)*8,318);
DrawA.LineTo(264+(xaxis-x)*8,318-m_xy3[m_count-x]*2);

DrawA.MoveTo(264+(xaxis-x)*8,418);
DrawA.LineTo(264+(xaxis-x)*8,418-m_xy4[m_count-x]*2);

DrawA.MoveTo(264+(xaxis-x)*8,518);
DrawA.LineTo(264+(xaxis-x)*8,518-m_xy5[m_count-x]*2);

//SENSOR Array Column 2 Graph

DrawA.MoveTo(590+(xaxis-x)*8,118);
DrawA.LineTo(590+(xaxis-x)*8,118-m_xy6[m_count-x]*2);

DrawA.MoveTo(590+(xaxis-x)*8,218);
DrawA.LineTo(590+(xaxis-x)*8,218-m_xy7[m_count-x]*2);

DrawA.MoveTo(590+(xaxis-x)*8,318);
DrawA.LineTo(590+(xaxis-x)*8,318-m_xy8[m_count-x]*2);

DrawA.MoveTo(590+(xaxis-x)*8,418);
DrawA.LineTo(590+(xaxis-x)*8,418-m_xy9[m_count-x]*2);

DrawA.MoveTo(590+(xaxis-x)*8,518);
DrawA.LineTo(590+(xaxis-x)*8,518-m_xy10[m_count-x]*2);

}
}
}

void CThesisTwoDlg::OnTrain()
{
// TODO: Add your control notification handler code here
CTrain Trains;

OnResetValue();
Invalidate();


if (Trains.DoModal()==IDOK)
{
m_name = Trains.m_name;
GetDlgItem(IDC_START)-> EnableWindow(TRUE);
GetDlgItem(IDC_ID) -> EnableWindow(FALSE);
GetDlgItem(IDC_END) -> EnableWindow(FALSE);
GetDlgItem(IDC_TEST) -> EnableWindow(FALSE);
GetDlgItem(IDC_CLEAN) -> EnableWindow(FALSE);
GetDlgItem(IDC_TRAIN) -> EnableWindow(FALSE);
bEN = TRUE;
m_READY =&quot;READY&quot;;
UpdateData(FALSE);
}
}

void CThesisTwoDlg::OnStart()
{
// TODO: Add your control notification handler code here
m_nTimer = SetTimer(1, 10, NULL);
a =0.00;
xaxis=1;
m_count=0;


}


void CThesisTwoDlg::OnStop()
{
// TODO: Add your control notification handler code here

KillTimer(m_nTimer);
GetDlgItem(IDC_START)-> EnableWindow(FALSE);
GetDlgItem(IDC_STOP) -> EnableWindow(FALSE);
GetDlgItem(IDC_ID) -> EnableWindow(TRUE);
GetDlgItem(IDC_TEST) -> EnableWindow(TRUE);
GetDlgItem(IDC_CLEAN) -> EnableWindow(TRUE);
GetDlgItem(IDC_TRAIN) -> EnableWindow(TRUE);
m_READY = &quot;READY&quot;;

m_S1 = 0;
m_S2 = 0;
m_S3 = 0;
m_S4 = 0;
m_S5 = 0;
m_S6 = 0;
m_S7 = 0;
m_S8 = 0;
m_S9 = 0;
m_S10 = 0;
UpdateData(FALSE);

}



void CThesisTwoDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default


GetDlgItem(IDC_STOP) -> EnableWindow(TRUE);
GetDlgItem(IDC_START) -> EnableWindow(FALSE);
BOOL bEnable = bEN;

if (bEnable == TRUE)
{
if (m_count < 30)
{
m_READY = &quot;Training&quot;;

a = a + float(0.01);
m_S1 = a;
m_S2 = a ;
m_S3 = a ;
m_S4 = a ;
m_S5 = a ;
m_S6 = a ;
m_S7 = a ;
m_S8 = a ;
m_S9 = a ;
m_S10 = a ;

m_xy1[m_count] = int(m_S1*100);
m_xy2[m_count] = int(m_S2*100);
m_xy3[m_count] = int(m_S3*100);
m_xy4[m_count] = int(m_S4*100);
m_xy5[m_count] = int(m_S5*100);
m_xy6[m_count] = int(m_S6*100);
m_xy7[m_count] = int(m_S7*100);
m_xy8[m_count] = int(m_S8*100);
m_xy9[m_count] = int(m_S9*100);
m_xy10[m_count] = int(m_S10*100);
Invalidate(FALSE);
UpdateWindow ();
m_count++;
xaxis++;
UpdateData(FALSE);
}

else
{
OnStop();
OnFileCreate();
}

CDialog::OnTimer(nIDEvent);

}

}


void CThesisTwoDlg::OnFileCreate()
{
CStdioFile file(m_name + &quot;.m&quot;, CFile::modeCreate | CFile::modeWrite );
for (int m_count=0;m_count<30;m_count++)
{
file.SeekToEnd();
CString b;
b.Format(&quot;%02.2d %02.2d %02.2d %02.2d %02.2d %02.2d %02.2d %02.2d %02.2d %02.2d\n&quot;,
m_xy1[m_count],
m_xy2[m_count],
m_xy3[m_count],
m_xy4[m_count],
m_xy5[m_count],
m_xy6[m_count],
m_xy7[m_count],
m_xy8[m_count],
m_xy9[m_count],
m_xy10[m_count]);
int length = b.GetLength();
file.Write(b,length);
}
file.SeekToEnd();
}

void CThesisTwoDlg::OnResetValue()
{
for (int count=0; count<30; count++)
{
m_xy1[count] = 0;
m_xy2[count] = 0;
m_xy3[count] = 0;
m_xy4[count] = 0;
m_xy5[count] = 0;
m_xy6[count] = 0;
m_xy7[count] = 0;
m_xy8[count] = 0;
m_xy9[count] = 0;
m_xy10[count] = 0;
}
m_count=29;

}


please have some time checking this code. i need it badly. thanks.

hope you guys help me as soon as possible.
 
Maybe the problem is here,

DrawA.SelectObject(CreatePen(PS_SOLID,0,ColorPen));
you can't release the CPen resource,

do as below:

CPen pen(PS_SOLID,0,ColorPen);
CPen* pOldPen=DrawA.SelectObject(&pen);
.
.
.
DrawA.selecObject(pOldPen);(after you end your drawing operation,restore the old pen)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top