2
Vote

View not getting disposed in tab workspace.

description

We are facing a problem with view dispose.We are opening multiple views in tab workspace.
public TView ShowViewInTabWorkspace<TView>(TView view, string workspaceName, string tabTitle, bool allowMultipleInstance)
    {
        bool isSmartPartFound = false;
        if (workspaceName.Equals(WorkspaceNames.RightWorkspace))
        {
            if (!allowMultipleInstance)
            {
                IWorkspace wks = WorkItem.Workspaces[WorkspaceNames.RightWorkspace];
                if (wks != null && wks.SmartParts.Count > 1)
                {
                    for (int i = 0; i < wks.SmartParts.Count; i++)
                    {
                        if (view.GetType().Equals(wks.SmartParts[i].GetType()))
                        {
                            WorkItem.Workspaces[workspaceName].Activate(wks.SmartParts[i]);
                            isSmartPartFound = true;
                            break;
                        }
                    }
                }
            }
 
            if (!isSmartPartFound)
            {
                ///check the count of smart parts before showing NEW smartparts as View already created outside just return it.
                if (IsSmartPartAllowedToCreateAndShow())
                {
                    TabSmartPartInfo info = new TabSmartPartInfo();
                    info.ActivateTab = true;
                    info.Position = TabPosition.End;
                    //extra space is added to title to show close button on each tab
                    info.Title = tabTitle + Pluto.SmartClient.Interface.Constants.ShellConstants.TabTextSpace;
 
                    (view as System.Windows.Controls.UserControl).KeyUp += new System.Windows.Input.KeyEventHandler(ShellLayoutViewPresenter_KeyUp);
 
 
                    WorkItem.Workspaces[workspaceName].Show(view, info);
                }
            }
 
        }
        else
        {
            WorkItem.Workspaces[workspaceName].Show(view);
        }
        return view;
    }
 
Despite of calling Dispose() on View,it fails to dispose view object.So if I opened same view again a new view object created but old object resides in memory as it is. This accumulate memory and increase the memory size of application.
Dispose method in Presenter.cs
/// <summary>
    /// See <see cref="System.IDisposable.Dispose"/> for more information.
    /// </summary>
    public void Dispose()
    {
        if (!disposed)
        {
            disposed = true;
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
    /// <summary>
    /// Called when the object is being disposed or finalized.
    /// </summary>
    /// <param name="disposing">True when the object is being disposed (and therefore can
    /// access managed members); false when the object is being finalized without first
    /// having been disposed (and therefore can only touch unmanaged members).</param>
    /// 
   // static int counterTocheckFirstOccurrence = 0;
    protected virtual void Dispose(bool disposing)
    {
      //  if (counterTocheckFirstOccurrence == 0)
        //{
            if (disposing)
            {
                if (_workItem != null)
                {
                    if (this.View != null)
                    {
                        OnCloseView();
                        _workItem.Items.Remove(this.View);
                        if (_workItem.SmartParts.Get(this.View.GetType().Name) != null)
                        {
                            _workItem.SmartParts.Remove(this.View);
                        }
 
                        if (this.View is IDisposable)
                        {
                            //counterTocheckFirstOccurrence++;
                            ((IDisposable)this.View).Dispose();
                        }
 
                    }
                    GC.Collect();
 
                    _workItem.Items.Remove(this);
                }
            }
        //}
        disposed = true;
    }
 
Is there anything wrong in code?

comments