用Visual Studio 2012 编了一个字符串显示函数。程序运行时在调用此函数上千次之后会出现错误:
线程 0x1ca0 已退出,返回值为 0 (0x0)。
线程 0x2598 已退出,返回值为 0 (0x0)。
0x0011E26C 处的第一机会异常(在 ksd.exe 中): 0xC0000005: 读取位置 0x00000000 时发生访问冲突。
调试显示问题出在 语句 CFont* pFont=pDC->SelectObject(&font); 这里。
艰苦奋斗几小时解决不了。
恳请专家指教!
CSDN 的新人第一次发帖,请多多包涵。void CksdView::ShowCString(int index,int irow, int icol, CString string, CString colour)
{
/* 显示一个已知数
index, 盘号,0,1,2.
irow,icol 行和列
string,
colour, 颜色。 可选项:blue(隐含), red.
例: colour=“blue” ; // 或者 colour=“red”
ShowNumber(index,irow,icol,str,colour);
*/CString str;
int ix,iy,k;
int ired,igreen,iblue;ired=0;
igreen=0;
iblue=255; // 隐含色是蓝色
if (colour=="red")
{ ired = 255;
igreen=0;
iblue=0; }
// strin=string;
ix = rsize*( 200+index1080+90icol-90);
iy = rsize*( 200+92*irow-92);CDC *pDC=GetDC();// device context for painting
CFont font; //创建字体对象
font.CreateFont(60,0,0,0,FW_MEDIUM,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,DEFAULT_PITCH | FF_SWISS,"Arial");
CFont* pFont=pDC->SelectObject(&font);
CFont *pOldFont=pDC->SelectObject(&font);//选入设备描述表
pDC->SelectObject(&font); //选入设备环境
pDC->SetTextColor(RGB(ired,igreen,iblue)); //设置文本颜色
pDC->TextOut(ix,iy,string); // 输出 !!!
font.DeleteObject();
// pDC->SelectStockObject(DEVICE_DEFAULT_FONT);//选择堆字体 // test add}
线程 0x1ca0 已退出,返回值为 0 (0x0)。
线程 0x2598 已退出,返回值为 0 (0x0)。
0x0011E26C 处的第一机会异常(在 ksd.exe 中): 0xC0000005: 读取位置 0x00000000 时发生访问冲突。
调试显示问题出在 语句 CFont* pFont=pDC->SelectObject(&font); 这里。
艰苦奋斗几小时解决不了。
恳请专家指教!
CSDN 的新人第一次发帖,请多多包涵。void CksdView::ShowCString(int index,int irow, int icol, CString string, CString colour)
{
/* 显示一个已知数
index, 盘号,0,1,2.
irow,icol 行和列
string,
colour, 颜色。 可选项:blue(隐含), red.
例: colour=“blue” ; // 或者 colour=“red”
ShowNumber(index,irow,icol,str,colour);
*/CString str;
int ix,iy,k;
int ired,igreen,iblue;ired=0;
igreen=0;
iblue=255; // 隐含色是蓝色
if (colour=="red")
{ ired = 255;
igreen=0;
iblue=0; }
// strin=string;
ix = rsize*( 200+index1080+90icol-90);
iy = rsize*( 200+92*irow-92);CDC *pDC=GetDC();// device context for painting
CFont font; //创建字体对象
font.CreateFont(60,0,0,0,FW_MEDIUM,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,DEFAULT_PITCH | FF_SWISS,"Arial");
CFont* pFont=pDC->SelectObject(&font);
CFont *pOldFont=pDC->SelectObject(&font);//选入设备描述表
pDC->SelectObject(&font); //选入设备环境
pDC->SetTextColor(RGB(ired,igreen,iblue)); //设置文本颜色
pDC->TextOut(ix,iy,string); // 输出 !!!
font.DeleteObject();
// pDC->SelectStockObject(DEVICE_DEFAULT_FONT);//选择堆字体 // test add}
解决方案 »
- 静态框染色问题
- 通过服务启动应用程序连接数据库失败!
- 进程使用的动态库出错导致进程崩溃退出,不知道怎么跟踪到错误的动态库代码段,请问如何调试,谢谢
- 在多文档MDI下如何实现列表的自动刷新
- 如何在界面上显示和隐藏PICTURE控件
- 当对话框全屏显示时如何让对话框和其上的各个控件的相对位置保持不变?
- 菜鸟问题:如何把数据输出到WORD文档??
- 一个基础问题 各位看看缺什么了 我老是搞不定 关于把一个非active的程序激活
- 怎么调用对话框?
- mfc读取文件夹内所有图片
- 请教:win7 64 +vs2010 +access(msadodc.ocx) 数据库连接时open报错
- MFC 界面大小变化的时候DockPane不跟着变化
CFont font; //创建字体对象
font.CreateFont(60,0,0,0,FW_MEDIUM,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,DEFAULT_PITCH | FF_SWISS,"Arial");
CFont *pOldFont=pDC->SelectObject(&font);//选入设备描述表
pDC->SelectObject(&font); //选入设备环境
pDC->SetTextColor(RGB(ired,igreen,iblue)); //设置文本颜色
pDC->TextOut(ix,iy,string); // 输出 !!!pDC->SelectObject(pOldFont); //恢复字体
ReleaseDC(); //释放DC
CFont *pOldFont=pDC->SelectObject(&font);//选入设备描述表
pDC->SelectObject(&font); //选入设备环境
红的不要 !
调试显示问题出在 语句 CFont* pFont=pDC->SelectObject(&font); 这里。
------------------------
应该是pDC的指针为NULL了。
这样,某些耗费资源的就可以设置成类成员,比如CFont* pFont,CDC *pDC
这些只在适当位置初始化一次,反复使用,不用每次都新建,程序结束的时候释放,可以加快效率,减少资源管理的难度。