关于DLL里定位内存泄露

j20j10b 2014-05-14 02:34:25
1, char * lpszText = T2A(bCode.GetBuffer(bCode.GetLength()));

这样的指针赋值会造成内存泄露吗? 用完如何删除?






2, 另外在DLL里定位内存泄露语句, 用下面代码没找到, 如何在DLL里定位内存泄露位置?


#define _CRTDBG_MAP_ALLOC
// 用于将malloc和free函数重定向至DEBUG版本,使之能输出对应的源文件及行号。

#include <crtdbg.h>

#ifdef _DEBUG
#define new new(_CLIENT_BLOCK, __FILE__, __LINE__)
#endif



InitInstance()里

#ifdef _DEBUG
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF| _CRTDBG_LEAK_CHECK_DF);
_CrtDumpMemoryLeaks();
_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG );

#endif
...全文
76 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-05-14
  • 打赏
  • 举报
回复
CString::GetBuffer LPTSTR GetBuffer( int nMinBufLength ); throw( CMemoryException ); Return Value An LPTSTR pointer to the object’s (null-terminated) character buffer. Parameters nMinBufLength The minimum size of the character buffer in characters. This value does not include space for a null terminator. Remarks Returns a pointer to the internal character buffer for the CString object. The returned LPTSTR is not const and thus allows direct modification of CString contents. If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions. The address returned by GetBuffer may not be valid after the call to ReleaseBuffer since additional CString operations may cause the CString buffer to be reallocated. The buffer will not be reallocated if you do not change the length of the CString. The buffer memory will be freed automatically when the CString object is destroyed. Note that if you keep track of the string length yourself, you should not append the terminating null character. You must, however, specify the final string length when you release the buffer with ReleaseBuffer. If you do append a terminating null character, you should pass –1 for the length to ReleaseBuffer and ReleaseBuffer will perform a strlen on the buffer to determine its length. Example The following example demonstrates the use of CString::GetBuffer. // example for CString::GetBuffer CString s( "abcd" ); #ifdef _DEBUG afxDump << "CString s " << s << "\n"; #endif LPTSTR p = s.GetBuffer( 10 ); strcpy( p, "Hello" ); // directly access CString buffer s.ReleaseBuffer( ); #ifdef _DEBUG afxDump << "CString s " << s << "\n"; #endif CString Overview | Class Members | Hierarchy Chart See Also CString::GetBufferSetLength, CString::ReleaseBuffer

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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