现在的位置: 首页 > 综合 > 正文

延迟加载:vtk与MFC存在内存泄露问题

2013年11月07日 ⁄ 综合 ⁄ 共 2428字 ⁄ 字号 评论关闭
 从vtk上找到的,如何解决vtk莫名其面的内存泄露问题。

vtk+MFC,莫名其妙就会内存泄露。后来通过看vtk的mail list,才知道一旦vtk的dll比MFC的dll早加载,就会出错。

vtk官网上给出了两种解决办法:1 用延迟加载delayload。
                             2 从网站上用cvs下载最新vtk。

我采用第一中方法后,还是build不过去。
只好用wincvs从vtk上down下一个新版本。
当的乱七八糟的,用cmake编译,终于通过了。似乎这种方法也很有效。

Hello all,
>
> I finally got my M$ VC project working, using vtkMFCWindow. In this program
> i read some geometrical data from disk using my own readerfunction. I then
> display it using vtkMFCWindow. That all works fine, until i close the
> program and clean up the memory. At the end of program i get a message that
> memory leaks are detected. The problem is, that i really have no clue where
> they are from. When i look at the data dumped by VC, i find references to
> all kinds of objects i never have used. Can anybody tell me what i am doing
> wrong, or give me any other clues? I have build my program mostly using the
> examples given for vtkMFCWindow, however, also these examples give me the
> same problems.
>
> Thanking in advance,
>
> Han
////////////////////////////////////////////////////////////////////////////////////
The problem is that VTK dlls are loaded before the MFC dll, and at shutdown
> time, the MFC dll is unloaded before the VTK dlls... And when the MFC dll
> unloads, it does a memory leak check and incorrectly reports that all the
> VTK objects still left are leaks.
>
> You can overcome this by updating VTK to the CVS version and rebuilding VTK
> with VTK_USE_GUISUPPORT and VTK_USE_MFC turned on. I recently added flags
> to the CMakeLists to eliminate these false memory leak reports.
>
> Alternatively, you can use the /DELAYLOAD linker flag to force the VTK dlls
> to be loaded after the MFC dll and then they will also be unloaded prior to
> the MFC dll. Then any leak reports that you see will be for real... (This is
> what I added in CVS VTK to fix the problem.)
>
> HTH,
> David
//////////////////////////////////////////////////////////////////////////////

> Thank you for your response. I did what you suggested, to be precise, the
> second option. I added the linker option /DELAYLOAD:vtkMFC.dll. (I use
> vtkRendering.dll as well, should this one be delayed as well?) However, the
> result was now that there was an unresolved symbol:
>
>
>
>
>
> vtkMFC.lib(vtkMFC.dll) : error LNK2001: unresolved external symbol
> ___delayLoadHelper at 8
> Debug/VTKStart.exe : fatal error LNK1120: 1 unresolved externals
> Error executing link.exe.
>
>
> After a little googling, i noticed i should add the lib: Delayimp.lib. So i
> added the lib, and the compilation went fine again. However, now, the memory
> leaks returned. Is there something else wrong, or am i forgetting something?
>
>
>
>
>
> Secondly, if i were to go the other path, which files do I have to download?
> Only the files that are in the GUISupport/MFC/ or others as well?
>
>
>
>
>
> Thank you again for your response.
>
>
>
>
>
> Sincerely,
>
>
> Han

【上篇】
【下篇】

抱歉!评论已关闭.