How to properly organize a call to Dispose and is it possible?
There is C ++ code that in CWinFormsControl uses the C # class inherited from UserControl.
CWinFormsControl<MyControl> myControl; UserControl is in turn inherited from the IDisposable interface.
I try to do in C ++ class destructor
delete myControl; which would seem to call Dispose for MyControl. However, only the finalizer is called, but not Dispose.
It turns out that Dispose for MyControl objects is called with the flag disposing = false. And how, in this case, according to the pattern, delete managed resources?
I will add code snippets. The project itself is hefty. Something like this. Maybe something is missing.
C # object
public partial class DP_ViewFormWrapper : UserControl { bool m_Disposed = false; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } if (!this.m_Disposed) { foreach (UserControl paramDiagView in m_ParamDiagViews.Values) { ParamDiag_Page paramDiagPage = paramDiagView as ParamDiag_Page; if (paramDiagPage != null) { paramDiagPage.Close(); } } this.m_Disposed = true; } base.Dispose(disposing); } } C ++ object
class CDVMachineElementsView : public CRoot3DWnd { public: ~CDVMachineElementsView() { // Удаляем объекты закладок for (MapTabItr_t Itr = m_mapBodyTabs.begin() ; Itr != m_mapBodyTabs.end() ; Itr++) { delete Itr->second; } } int CDVMachineElementsView::OnCreate(LPCREATESTRUCT lpCreateStruct) { m_mapBodyTabs[0] = new CWinFormsControl<DP_ViewFormWrapper>(); CWinFormsControl<DP_ViewFormWrapper>* viewFormWrapper = (CWinFormsControl<DP_ViewFormWrapper>*)m_mapBodyTabs[0]; if (!viewFormWrapper->CreateManagedControl(dwWinFlags, CRect(0,0,0,0), this, 0)) { return -1; } return 0; } };