mfc动态创建picture控件後,SetBitmap没几秒钟,整个主程序卡死,求解决

eradifa 2017-04-11 01:52:07
CreatPicture是创建picture控件
DrawPicture是重复绘制图片
做的项目远程监控的服务端
bool CMainDlg::CreatPicture(const int ip_array_subscript, const int ipone, const int  iptwo, const int ipthree, const int ipfour, const char *buf)
{

//using namespace std;

std::string ip_address;
char c1[20],c2[20],c3[20],c4[20];
RECT rec;
int return1, return2;
_itoa(ipone, c1, 10);
_itoa(iptwo, c2, 10);
_itoa(ipthree, c3, 10);
_itoa(ipfour, c4, 10);
ip_address += c1;
ip_address += ".";
ip_address += c2;
ip_address += ".";
ip_address += c3;
ip_address += ".";
ip_address += c4;
rec=GetPictureCoordinate(ip_array_subscript);

//GlobalAlloc是一个Windows API函数。该函数从全局堆中分配一定数目的字节数
//若函数调用成功,则返回一个新分配的内存对象的句柄。

HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, 4096);

//锁定内存中指定的内存块,并返回一个地址值,令其指向内存块的起始处
void * pData = GlobalLock(hGlobal);

//内存拷贝
memcpy(pData, buf, 4096);

GlobalUnlock(hGlobal);
IStream * pStream = NULL;
//CreateStreamOnHGlobal函数从指定内存创建流对象。
if (CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK)
{
CImage image;
if (SUCCEEDED(image.Load(pStream)))
{
//避免读到已有对象的内存
if (pPictureControl[ip_array_subscript].m_hWnd == 0)
{
//如果创建图片框失败,则返回0
return1 = pPictureControl[ip_array_subscript].Create(_T(""), WS_CHILD | WS_VISIBLE | SS_BITMAP, rec, this, ip_array_subscript);
if (return1 == 0)
{
array[ip_array_subscript] = ipone;
array[ip_array_subscript + 1] = iptwo;
array[ip_array_subscript + 2] = ipthree;
array[ip_array_subscript + 3] = ipfour;
pStream->Release();
GlobalFree(hGlobal);
image.Detach();

return 0;
}

}
else
{
return2 = (int)(pPictureControl[ip_array_subscript].SetBitmap(image));
}
//CStatic *pic = (CStatic *)GetDlgItem(IDC_PIC);
//pic->SetBitmap(image);

}
pStream->Release();
image.Detach();
}

GlobalFree(hGlobal);

if (return1 != 0 && return2 != NULL)
{
return 1;
}


}
bool CMainDlg::DrawPicture(const int ip_array_subscript, const int ipone, const int iptwo, const int ipthree, const int ipfour, const char *buf)
{
//using namespace std;
std::string ip_address;
int return1;
char c1[20], c2[20], c3[20], c4[20];
RECT rec;

_itoa(ipone, c1, 10);
_itoa(iptwo, c2, 10);
_itoa(ipthree, c3, 10);
_itoa(ipfour, c4, 10);
ip_address += c1;
ip_address += ".";
ip_address += c2;
ip_address += ".";
ip_address += c3;
ip_address += ".";
ip_address += c4;
rec = GetPictureCoordinate(ip_array_subscript);
//GlobalAlloc是一个Windows API函数。该函数从全局堆中分配一定数目的字节数
//若函数调用成功,则返回一个新分配的内存对象的句柄。

HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, 4096);

//锁定内存中指定的内存块,并返回一个地址值,令其指向内存块的起始处
void * pData = GlobalLock(hGlobal);

//内存拷贝
memcpy(pData, buf, 4096);

GlobalUnlock(hGlobal);
IStream * pStream = NULL;
//CreateStreamOnHGlobal函数从指定内存创建流对象。
if (CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK)
{
CImage image;
if (SUCCEEDED(image.Load(pStream)))
{
if (pPictureControl[ip_array_subscript].m_hWnd != 0)
{
return1 = (int)(pPictureControl[ip_array_subscript].SetBitmap(image));
}
}
pStream->Release();
image.Detach();
}

GlobalFree(hGlobal);
if (return1!=NULL)
{
return 1;
}
return 0;
}
...全文
308 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-04-13
  • 打赏
  • 举报
回复
检查是否资源泄漏的办法之一: 在任务管理器 进程 查看 选择列 里面选择:内存使用、虚拟内存大小、句柄数、线程数、USER对象、GDI对象 让你的程序(进程)不退出,循环执行主流程很多遍,越多越好,比如1000000次甚至无限循环,记录以上各数值,再隔至少一小时,越长越好,比如一个月,再记录以上各数值。如果以上两组数值的差较大或随时间流逝不断增加,则铁定有对应资源的资源泄漏! 搜“GDI泄露检测”
eradifa 2017-04-12
  • 打赏
  • 举报
回复
引用 1 楼 zgl7903 的回复:
return 0; 就可能导致资源泄漏
为何返回0会告成内存泄漏?
叶恭介叶恭介 2017-04-11
  • 打赏
  • 举报
回复
最好注释一部分代码,通过TRACE增加提示文字输出,看是哪部分卡住了。
zgl7903 2017-04-11
  • 打赏
  • 举报
回复
return 0; 就可能导致资源泄漏

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧