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

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翻译的就不要了,最好是自己领悟到的语言
...全文
273 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
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
  • 打赏
  • 举报
回复
等进程退出时自动释放?

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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