MEF keeps reference of NonShared IDisposable parts, not allowing them to be collected by GC

前端 未结 4 1787
一整个雨季
一整个雨季 2020-12-09 05:11

I\'ve encountered somewhat of a problem in MEF\'s part lifetime which causes memory leaks in my Prism application.

My application exports views and viewmodels with t

4条回答
  •  爱一瞬间的悲伤
    2020-12-09 05:22

    Unless Prism supports some kind of lifetime for view objects, there is no solution here except to remove IDisposable from the list of interfaces exposed by the view.

    There are three MEF approaches to handling this problem, all mentioned by other responders:

    • ExportFactory
    • Child containers
    • ReleaseExport()

    All of them require some work on the part of the code that requests the original export - in this case code within Prism. This makes some sense, as it is undesirable for code consuming an object to have to be aware of how and when it was created.

    There is no ReleaseExportedObject() in MEF because multiple (e.g. property) exports can return the same value; it may be logically possible to provide but the added complexity makes it unlikely to be addressed by MEF in the foreseeable future.

    Hope this helps; I've retagged this question 'prism' as I'm sure others in the Prism community will have encountered this and be able to offer advice.

提交回复
热议问题