Home > Software engineering >  After the MFC dynamic create picture control, SetBitmap few seconds, the entire main program jammed,
After the MFC dynamic create picture control, SetBitmap few seconds, the entire main program jammed,

Time:10-01

Controls CreatPicture is to create the picture
DrawPicture is drawing picture
Do the project of remote monitoring of the service side
 bool CMainDlg: : CreatPicture (const int ip_array_subscript, const int iphone, const int iptwo, const int ipthree, const int ipfour, const char * buf) 
{

//using namespace STD.

STD: : string ip_address;
Char c1 [20], [20] serie c2 and c3 [20], c4 [20].
The RECT rec.
Int return1 return2;
_itoa (iphone, 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 is a Windows API function, the function from the global heap allocated in a certain number of bytes
//if the function call is successful, it returns a handle to the newly allocated memory object,

HGLOBAL HGLOBAL=GlobalAlloc (GMEM_MOVEABLE, 4096);

//lock memory specified memory block, and return a address values, make it points to the memory block at beginning
Void * pData=https://bbs.csdn.net/topics/GlobalLock (hGlobal);

//memory copy
Memcpy (pData, buf, 4096);

GlobalUnlock (hGlobal);
Cost * pStream=NULL;
//CreateStreamOnHGlobal function from the specified memory to create stream objects,
If (CreateStreamOnHGlobal (hGlobal, TRUE, & amp; PStream)==S_OK)
{
CImage image;
If (SUCCEEDED (image. The Load (pStream)))
{
//to avoid to read for the object's memory
If (pPictureControl [ip_array_subscript] m_hWnd==0)
{
//if failed to create a picture box, it returns 0
Return1=pPictureControl [ip_array_subscript]. The Create (_T (" "), WS_CHILD | WS_VISIBLE | SS_BITMAP, rec, this, ip_array_subscript);
If (return1==0)
{
Array [ip_array_subscript]=iphone;
Array [ip_array_subscript + 1]=iptwo;
Array [ip_array_subscript + 2]=ipthree;
Array [ip_array_subscript + 3]=ipfour;
PStream - & gt; Release ();
GlobalFree (hGlobal);
Image. Detach ();

return 0;
}

}
The else
{
Return2=(int) (pPictureControl [ip_array_subscript] SetBitmap (image));
}
//CStatic * PIC=(CStatic *) GetDlgItem (IDC_PIC);
//PIC - & gt; SetBitmap (image);

}
PStream - & gt; Release ();
Image. Detach ();
}

GlobalFree (hGlobal);

If (return1!=0 & amp; & Return2!=NULL)
{
return 1;
}


}
Bool CMainDlg: : DrawPicture (const int ip_array_subscript, const int iphone, const int iptwo, const int ipthree, const int ipfour, const char * buf)
{
//using namespace STD.
STD: : string ip_address;
Int return1;
Char c1 [20], [20] serie c2 and c3 [20], c4 [20].
The RECT rec.

_itoa (iphone, 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 is a Windows API function, the function from the global heap allocated in a certain number of bytes
//if the function call is successful, it returns a handle to the newly allocated memory object,

HGLOBAL HGLOBAL=GlobalAlloc (GMEM_MOVEABLE, 4096);

//lock memory specified memory block, and return a address values, make it points to the memory block at beginning
Void * pData=https://bbs.csdn.net/topics/GlobalLock (hGlobal);

//memory copy
Memcpy (pData, buf, 4096);

GlobalUnlock (hGlobal);
Cost * pStream=NULL;
//CreateStreamOnHGlobal function from the specified memory to create stream objects,
If (CreateStreamOnHGlobal (hGlobal, TRUE, & amp; PStream)==S_OK)
{
CImage image;
If (SUCCEEDED (image. The Load (pStream)))
{
If (pPictureControl [ip_array_subscript] m_hWnd!=0)
{
Return1=(int) (pPictureControl [ip_array_subscript] SetBitmap (image));
}
}
PStream - & gt; Release ();
Image. Detach ();
}

GlobalFree (hGlobal);
If (return1!=NULL)
{
return 1;
}
return 0;
}

CodePudding user response:

return 0; Can lead to resource leaks

CodePudding user response:

Comments part of the code, the best way to increase tip text output TRACE, see which part is stuck,

CodePudding user response:




reference 1st floor zgl7903 response:
return 0; Resource leaks can lead to
why returns 0 and dry the memory leak?

CodePudding user response:

One way to check whether resource leaks:
In the process of the task manager to see inside the column selection: memory, virtual memory size, handle number, number of threads, the USER object, GDI objects
Make your program (process) don't quit, cycle through main process many times, the more the better, like 1000000 times or even an infinite loop, record the above numerical, it through at least one hour, as long as possible, such as a month, to record more than the value, if the above two groups of numerical differential is bigger or increasing over time, will certainly have a corresponding resources resource leaks!

Search "GDI leak detection"
  • Related