我在程序里面设了个timer
在ontimer事件中全部代码如下,在DEBUG模式下,运行完全没有问题,在RELEASE模式下又是提示"内存不足",又是if判断部分代码不能很好的执行.请问这是为什么呢?
我把ontimer里面的这些全部注释掉就没有这些问题了.但我实在没找出问题出在哪里?
都是用的static library. POINT colorPoint;
::GetCursorPos(&colorPoint);
CWindowDC dc(GetDesktopWindow());
m_color = dc.GetPixel(colorPoint);
rValue=GetRValue(m_color);
gValue=GetGValue(m_color);
bValue=GetBValue(m_color); CString strR,strG,strB,x,y,webColor;
strR="";
strG="";
strB="";
x="";
y="";
webColor="";
char webR[2],webG[2],webB[2];
memset(webR,0,2);
memset(webG,0,2);
memset(webB,0,2); strR.Format("%d",rValue);
strG.Format("%d",gValue);
strB.Format("%d",bValue); x.Format("%d",colorPoint.x);
y.Format("%d",colorPoint.y); _itoa(rValue,webR,16);
_itoa(gValue,webG,16);
_itoa(bValue,webB,16); if(rValue<=15 && gValue>15 && bValue>15)
{
webColor.Format("#0%s%s%s",webR,webG,webB);
}
else if(gValue<=15 && rValue>15 && bValue>15)
{
webColor.Format("#%s0%s%s",webR,webG,webB);
}
else if(bValue<=15 && gValue>15 && rValue>15)
{
webColor.Format("#%s%s0%s",webR,webG,webB);
}
else if(rValue<=15 && gValue<=15 && bValue>15)
{
webColor.Format("#0%s0%s%s",webR,webG,webB);
}
else if(rValue<=15 && gValue>15 && bValue<=15)
{
webColor.Format("#0%s%s0%s",webR,webG,webB);
}
else if(gValue<=15 && rValue>15 && bValue<=15)
{
webColor.Format("#%s0%s0%s",webR,webG,webB);
}
else if(gValue<=15 && bValue>15 && rValue<=15)
{
webColor.Format("#0%s0%s%s",webR,webG,webB);
}
else if(bValue<=15 && gValue<=15 && rValue>15)
{
webColor.Format("#%s0%s0%s",webR,webG,webB);
}
else if(bValue<=15 && gValue>15 && rValue<=15)
{
webColor.Format("#0%s%s0%s",webR,webG,webB);
}
else if(bValue<=15 && gValue<=15 && rValue<=15)
{
webColor.Format("#0%s0%s0%s",webR,webG,webB);
}
else if(bValue>15 && gValue>15 && rValue>15)
{
webColor.Format("#%s%s%s",webR,webG,webB);
}//在release版本中上述判断代码无法正常执行. GetDlgItem(IDC_R)->SetWindowText(strR);
GetDlgItem(IDC_G)->SetWindowText(strG);
GetDlgItem(IDC_B)->SetWindowText(strB);
GetDlgItem(IDC_X)->SetWindowText(x);
GetDlgItem(IDC_Y)->SetWindowText(y);
GetDlgItem(IDC_WEBCOLOR)->SetWindowText(webColor); CStatic* m_pStatic=(CStatic*)GetDlgItem(IDC_SHOW);
m_pStatic->RedrawWindow();
HDC hdc;
hdc=::GetDC(m_hWnd);
::StretchBlt(hdc,14,13,84,71,dc,colorPoint.x-15,colorPoint.y-15,30,30,SRCCOPY);
CPen pen(PS_INSIDEFRAME,1,RGB(255-rValue,255-gValue,255-bValue));
CClientDC clidc(this);
clidc.SelectObject(&pen);
clidc.MoveTo(49,48);
clidc.LineTo(63,48);
clidc.MoveTo(56,41);
clidc.LineTo(56,55);
在ontimer事件中全部代码如下,在DEBUG模式下,运行完全没有问题,在RELEASE模式下又是提示"内存不足",又是if判断部分代码不能很好的执行.请问这是为什么呢?
我把ontimer里面的这些全部注释掉就没有这些问题了.但我实在没找出问题出在哪里?
都是用的static library. POINT colorPoint;
::GetCursorPos(&colorPoint);
CWindowDC dc(GetDesktopWindow());
m_color = dc.GetPixel(colorPoint);
rValue=GetRValue(m_color);
gValue=GetGValue(m_color);
bValue=GetBValue(m_color); CString strR,strG,strB,x,y,webColor;
strR="";
strG="";
strB="";
x="";
y="";
webColor="";
char webR[2],webG[2],webB[2];
memset(webR,0,2);
memset(webG,0,2);
memset(webB,0,2); strR.Format("%d",rValue);
strG.Format("%d",gValue);
strB.Format("%d",bValue); x.Format("%d",colorPoint.x);
y.Format("%d",colorPoint.y); _itoa(rValue,webR,16);
_itoa(gValue,webG,16);
_itoa(bValue,webB,16); if(rValue<=15 && gValue>15 && bValue>15)
{
webColor.Format("#0%s%s%s",webR,webG,webB);
}
else if(gValue<=15 && rValue>15 && bValue>15)
{
webColor.Format("#%s0%s%s",webR,webG,webB);
}
else if(bValue<=15 && gValue>15 && rValue>15)
{
webColor.Format("#%s%s0%s",webR,webG,webB);
}
else if(rValue<=15 && gValue<=15 && bValue>15)
{
webColor.Format("#0%s0%s%s",webR,webG,webB);
}
else if(rValue<=15 && gValue>15 && bValue<=15)
{
webColor.Format("#0%s%s0%s",webR,webG,webB);
}
else if(gValue<=15 && rValue>15 && bValue<=15)
{
webColor.Format("#%s0%s0%s",webR,webG,webB);
}
else if(gValue<=15 && bValue>15 && rValue<=15)
{
webColor.Format("#0%s0%s%s",webR,webG,webB);
}
else if(bValue<=15 && gValue<=15 && rValue>15)
{
webColor.Format("#%s0%s0%s",webR,webG,webB);
}
else if(bValue<=15 && gValue>15 && rValue<=15)
{
webColor.Format("#0%s%s0%s",webR,webG,webB);
}
else if(bValue<=15 && gValue<=15 && rValue<=15)
{
webColor.Format("#0%s0%s0%s",webR,webG,webB);
}
else if(bValue>15 && gValue>15 && rValue>15)
{
webColor.Format("#%s%s%s",webR,webG,webB);
}//在release版本中上述判断代码无法正常执行. GetDlgItem(IDC_R)->SetWindowText(strR);
GetDlgItem(IDC_G)->SetWindowText(strG);
GetDlgItem(IDC_B)->SetWindowText(strB);
GetDlgItem(IDC_X)->SetWindowText(x);
GetDlgItem(IDC_Y)->SetWindowText(y);
GetDlgItem(IDC_WEBCOLOR)->SetWindowText(webColor); CStatic* m_pStatic=(CStatic*)GetDlgItem(IDC_SHOW);
m_pStatic->RedrawWindow();
HDC hdc;
hdc=::GetDC(m_hWnd);
::StretchBlt(hdc,14,13,84,71,dc,colorPoint.x-15,colorPoint.y-15,30,30,SRCCOPY);
CPen pen(PS_INSIDEFRAME,1,RGB(255-rValue,255-gValue,255-bValue));
CClientDC clidc(this);
clidc.SelectObject(&pen);
clidc.MoveTo(49,48);
clidc.LineTo(63,48);
clidc.MoveTo(56,41);
clidc.LineTo(56,55);
另外,如果用shared library的话,release不会提示"内存不足",但是那个if判断代码完全不起作用.而且有些变量没有取到值.比如rValue,gValue,bValue.
为什么会这样呢?
定义每个字符串长度为2,因为结尾\0要占用1字节,所以字符串只能保存1个字符。
_itoa(rValue,webR,16);
_itoa(gValue,webG,16);
_itoa(bValue,webB,16);
这三行代码将可能输出2个字符到字符串里,因为字符串只能保存1个字符,所以溢出了。因为Debug版会在定义变量时多分配一些空间,所以有一点溢出也不会覆盖到有效数据,所以没有出问题;而Release版是没有多分配空间的。char webR[3],webG[3],webB[3];
这样定义就没事了。