几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量

几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量 (http://www.dimcax.com/hust/index.php)
-   内存泄漏 (http://www.dimcax.com/hust/forumdisplay.php?f=118)
-   -   strcore.cpp的内存泄漏问题 (http://www.dimcax.com/hust/showthread.php?t=460)

huangyhg 2007-01-24 09:28 PM

strcore.cpp的内存泄漏问题
 
strcore.cpp的内存泄漏问题
程序在DEBUG方式下运行,退出后发现内存泄漏:
output:
Loaded 'C:\WINDOWS\SYSTEM\OLEAUT32.DLL', no matching symbolic information found
.......
.......
Warning: Could not find splitter cursor - using system provided alternative.
The thread 0xFF07C045 has exited with code 0 (0x0).
The thread 0xFF07C5D9 has exited with code 0 (0x0).
The thread 0xFF07C72D has exited with code 0 (0x0).
Detected memory leaks!
Dumping objects ->
strcore.cpp(118) : {609} normal block at 0x01B04620, 43 bytes long.
Data: < > 01 00 00 00 1E 00 00 00 1E 00 00 00 B5 CD C3 DC
strcore.cpp(118) : {606} normal block at 0x01B047B0, 43 bytes long.
Data: < > 01 00 00 00 1E 00 00 00 1E 00 00 00 B5 A5 C4 A3
Object dump complete.
The thread 0xFF0793F9 has exited with code 0 (0x0).
The program 'G:\OUTPUT\Glb.exe' has exited with code 0 (0x0).

huangyhg 2007-01-24 09:29 PM

回复: strcore.cpp的内存泄漏问题
 
strcore.cpp是CString类的文件,问题应该是处在某个CString的地方.如果你确定所有new/Release都没有问题的话,再检查一下有没有用CString::GetBuffer或者LockBuffer,而没有ReleaseBuffer的情况.

huangyhg 2007-01-24 09:30 PM

回复: strcore.cpp的内存泄漏问题
 
把strcore.cpp文件拷到程序的当前目录下,就知道是那一条语句产生了内存泄漏了。

huangyhg 2007-01-24 09:32 PM

回复: strcore.cpp的内存泄漏问题
 
虽然错误的具体原因尚不能确定,但这是由三个CString对象引起的内存泄漏却是毫无疑义。我在数据处理子线程的结束处,手动删除了这三个CString对象,此处的内存泄漏就没有了。

huangyhg 2007-01-24 09:32 PM

回复: strcore.cpp的内存泄漏问题
 
还可以在程序中加入检查的代码。方法是:在需要检测的程序段的开始处建立一个CmemoryState对象,调用其成员函数Checkpoint,以取得当前内存使用情况的快照;在需要检测的程序段的末尾处再建立一个CmemoryState 对象,调用其成员函数Checkpoint ,以取得当前内存使用情况的快照;再建立第三个CmemoryState 对象,调用其成员函数Difference,把第一个CmemoryState对象和第二个CmemeoryState对象作为其参数.,如果两次内存快照不相同,则该函数返回非零,说明此程序段中有内存漏洞。下面来看一个典型的例子:

CMemoryState oldMemState, newMemState, diffMemState;
OldMemState.Checkpoint();
. . . . . .
CString s = "Test memory leak!";
. . . . . .
newMemState.Checkpoint();
if( diffMemState.Difference( oldMemState, newMemState ) )
{ TRACE( "Memory leaked!\n" ); }


所有的时间均为北京时间。 现在的时间是 10:06 AM.