大家帮忙看看,解释一下这段代码

coder0621 2014-01-23 03:42:32
CString source;
//文本内容保存在source变量中
if( OpenClipboard() )
{
HGLOBAL clipbuffer;
char * buffer;
EmptyClipboard();
clipbuffer = GlobalAlloc(GMEM_DDESHARE, source.GetLength()+1);
buffer = (char*)GlobalLock(clipbuffer);
strcpy(buffer, LPCSTR(source));
GlobalUnlock(clipbuffer);
SetClipboardData(CF_TEXT,clipbuffer);
CloseClipboard();
}


从网上看的一段代码,有点疑惑,请教一下
1、这段代码会不会有内存泄漏。没有对clipbuffer 做GlobalFree操作
2、SetClipboardData是不是完全接管了clipbuffer ,包括自动释放之类的
3、能对这几个函数详细解释一下最好了,msdn翻译的就不要了,最好是自己领悟到的语言
...全文
201 点赞 收藏 7
写回复
7 条回复
赵4老师 2014年01月23日
Clipboard Ownership The clipboard owner is the window associated with the information on the clipboard. A window becomes the clipboard owner when it places data on the clipboard — specifically, when it calls the EmptyClipboard function. The window remains the clipboard owner until it is closed or another window empties the clipboard. When the clipboard is emptied, the clipboard owner receives a WM_DESTROYCLIPBOARD message. Following are some reasons why a window might process this message: The window delayed rendering of one or more clipboard formats. In response to the WM_DESTROYCLIPBOARD message, the window might free resources it had allocated in order to render data on request. For more information about the rendering of data, see Delayed Rendering. The window placed data on the clipboard in a private clipboard format. The data for private clipboard formats is not freed by the system when the clipboard is emptied. Therefore, the clipboard owner should free the data upon receiving the WM_DESTROYCLIPBOARD message. For more information about private clipboard formats, see Clipboard Formats. The window placed data on the clipboard using the CF_OWNERDISPLAY clipboard format. In response to the WM_DESTROYCLIPBOARD message, the window might free resources it had used to display information in the clipboard viewer window. For more information about this alternative format, see Owner-Display Format. Delayed Rendering When placing a clipboard format on the clipboard, a window can delay rendering the data in that format until the data is needed. To do so, an application can specify NULL for the hData parameter of the SetClipboardData function. This is useful if the application supports several clipboard formats, some or all of which are time-consuming to render. By passing a NULL handle, a window renders complex clipboard formats only when and if they are needed. If a window delays rendering a clipboard format, it must be prepared to render the format upon request for as long as it is the clipboard owner. The system sends the clipboard owner a WM_RENDERFORMAT message when a request is received for a specific format that has not been rendered. Upon receiving this message, the window should call the SetClipboardData function to place a global memory handle on the clipboard in the requested format. If the clipboard owner is destroyed and has delayed rendering some or all clipboard formats, it receives the WM_RENDERALLFORMATS message. Upon receiving this message, the window should place valid memory handles on the clipboard for all clipboard formats that it provides. This ensures that these formats remain available after the clipboard owner is destroyed. An application should not open the clipboard before calling SetClipboardData in response to the WM_RENDERFORMAT or WM_RENDERALLFORMATS message. Any clipboard formats that are not rendered in response to the WM_RENDERALLFORMATS message cease to be available to other applications and are no longer enumerated by the clipboard functions. Memory and the Clipboard A memory object that is to be placed on the clipboard should be allocated by using the GlobalAlloc function with the GMEM_DDESHARE and GMEM_MOVEABLE flags. Once a memory object is placed on the clipboard, ownership of that memory handle is transferred to the system. When the clipboard is emptied and the memory object has one of the following clipboard formats, the system frees the memory object by calling the indicated Win32 function: Function called to free object Clipboard format DeleteMetaFile CF_DSPENHMETAFILE CF_DSPMETAFILEPICT CF_ENHMETAFILE CF_METAFILEPICT DeleteObject CF_BITMAP CF_DSPBITMAP CF_PALETTE GlobalFree CF_DIB CF_DIBV5 CF_DSPTEXT CF_OEMTEXT CF_TEXT CF_UNICODETEXT When the clipboard is emptied of a memory object whose clipboard format is not shown in the preceding list, the application itself must free the memory object.
回复 点赞
赵4老师 2014年01月23日
查MSDN是Windows程序员必须掌握的技能之一。
回复 点赞
赵4老师 2014年01月23日
SetClipboardData The SetClipboardData function places data on the clipboard in a specified clipboard format. The window must be the current clipboard owner, and the application must have called the OpenClipboard function. (When responding to the WM_RENDERFORMAT and WM_RENDERALLFORMATS messages, the clipboard owner must not call OpenClipboard before calling SetClipboardData.) HANDLE SetClipboardData( UINT uFormat, // clipboard format HANDLE hMem // data handle ); Parameters uFormat Specifies a clipboard format. This parameter can be a registered format or any of the standard clipboard formats. For more information, see Registered Clipboard Formats and Standard Clipboard Formats. hMem Handle to the data in the specified format. This parameter can be NULL, indicating that the window provides data in the specified clipboard format (renders the format) upon request. If a window delays rendering, it must process the WM_RENDERFORMAT and WM_RENDERALLFORMATS messages. After SetClipboardData is called, the system owns the object identified by the hMem parameter. The application can read the data, but must not free the handle or leave it locked. If the hMem parameter identifies a memory object, the object must have been allocated using the GlobalAlloc function with the GMEM_MOVEABLE and GMEM_DDESHARE flags. Return Values If the function succeeds, the return value is the handle of the data. If the function fails, the return value is NULL. To get extended error information, call GetLastError. Remarks The uFormat parameter can identify a registered clipboard format, or it can be one of the standard clipboard formats. For more information, see Registered Clipboard Formats and Standard Clipboard Formats. The system performs implicit data format conversions between certain clipboard formats when an application calls the GetClipboardData function. For example, if the CF_OEMTEXT format is on the clipboard, a window can retrieve data in the CF_TEXT format. The format on the clipboard is converted to the requested format on demand. For more information, see Synthesized Clipboard Formats. Windows CE: The data to be set into the clipboard should be allocated using the LocalAlloc function. Windows CE does not support the following uFormat values: CF_GDIOBJFIRST through CF_GDIOBJLAST CF_DSPBITMAP CF_DSPENHMETAFILE CF_DSPMETAFILEPICT CF_DSPTEXT CF_HDROP CF_LOCALE CF_OWNERDISPLAY CF_PRIVATEFIRST through CF_PRIVATELAST Windows CE does not perform any implicit conversions between formats. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in winuser.h. Import Library: Use user32.lib. See Also Clipboard Overview, Clipboard Functions,BITMAPINFO, GetClipboardData, GlobalAlloc, GlobalFree, METAFILEPICT, OpenClipboard,RealizePalette, RegisterClipboardFormat,SelectPalette, WM_ASKCBFORMATNAME, WM_DESTROYCLIPBOARD, WM_HSCROLLCLIPBOARD, WM_PAINTCLIPBOARD, WM_RENDERFORMAT, WM_RENDERALLFORMATS, WM_SIZECLIPBOARD, WM_VSCROLLCLIPBOARD
回复 点赞
coder0621 2014年01月23日
引用 3 楼 xiaoc1026 的回复:
1,不会有内存泄露 2,SetClipboardData 调用后,系统拥有的hMem参数确立,这个时候可以读取粘贴板数据,直到调用CloseClipboard后,自动释放资源。
也就是说clipbuffer 在CloseClipboard后,已经被释放了?
回复 点赞
见习学术士 2014年01月23日
1,不会有内存泄露 2,SetClipboardData 调用后,系统拥有的hMem参数确立,这个时候可以读取粘贴板数据,直到调用CloseClipboard后,自动释放资源。
回复 点赞
coder0621 2014年01月23日
引用 1 楼 zhao4zhong1 的回复:
等进程退出时自动释放?
复制剪切板,这种操作,可能是很频繁的,等进程退出自动释放是不合理的,这段代码也会多次执行,会不会有内存泄漏?
回复 点赞
赵4老师 2014年01月23日
等进程退出时自动释放?
回复 点赞
发动态
发帖子
VC/MFC
创建于2007-09-28

7893

社区成员

42.1w+

社区内容

VC/MFC相关问题讨论
社区公告
暂无公告