[Mono-bugs] [Bug 386460] New: Forms with menus aren't GCed

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Sat May 3 19:46:28 EDT 2008


https://bugzilla.novell.com/show_bug.cgi?id=386460


           Summary: Forms with menus aren't GCed
           Product: Mono: Class Libraries
           Version: 1.9.0
          Platform: Macintosh
        OS/Version: Mac OS X 10.4
            Status: NEW
          Severity: Major
          Priority: P5 - None
         Component: Windows.Forms
        AssignedTo: mono-bugs at lists.ximian.com
        ReportedBy: jesjones at mindspring.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---


Forms with a menu aren't GCed. The problem with Form::Menu seems to be that the
menu object is never removed from the static Hwnd table. One fix for this is to
add the following to Form::Close

   if (Menu != null && IsHandleCreated)
      XplatUI.SetMenu (window.Handle, null);

I'm not sure what the problem with Form::MainMenuStrip is, but the above
doesn't fix it.

Here's a test case. Note that when you run this don't open the menu (or you'll
run into a separate set of bugs).

#define OLD

// compile with: 
// gmcs -r:System.Windows.Forms.dll -r:System.Drawing.dll -out:app.exe
FormGC1.cs
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

// Don't open the file menu when you run this app.
internal class Program
{
    public static void Main(string[] args)
    {         
        DoInit();

        Application.Run();
    }

     public static void DoInit()
     {         
        ms_timer = new Timer();
        ms_window = new Form();
        ms_window.Closed += DoClosed;
        ms_window.Menu = new MainMenu();
        ms_weak = new WeakReference(ms_window);

#if OLD
            // This works if the patch is appllied to Form.Close.
            MenuItem fileMenu = new MenuItem("File");
            fileMenu.MenuItems.Add(0, new MenuItem("Close"));

            ms_window.Menu = new MainMenu();
            ms_window.Menu.MenuItems.Add(fileMenu);

#else
            // This does not work.
            MenuStrip strip = new MenuStrip();
            ToolStripMenuItem fileMenu = new ToolStripMenuItem();
            ToolStripMenuItem closeItem = new ToolStripMenuItem();

            // strip
            strip.Items.AddRange(new ToolStripItem[] {fileMenu});

            // fileMenu
            fileMenu.DropDownItems.AddRange(new ToolStripItem[] {closeItem});
            fileMenu.Text = "File";

            // closeItem
            closeItem.Name = "newToolStripMenuItem";
            closeItem.Text = "Close";

            // Form
            ms_window.Controls.Add(strip);
            ms_window.MainMenuStrip = strip;
#endif

        ms_window.Visible = true;

        ms_timer.Tick += DoClose;
        ms_timer.Interval = 3000;
        ms_timer.Start();
    }

    private static void DoClosed(object sender, EventArgs e)
    {
        ms_window = null;
    }

    // Using the close box doesn't work too well so we'll force the 
    // window to close automagically.
    private static void DoClose(Object myObject, EventArgs myEventArgs) 
    {
        if (ms_state == 0)
        {
            ms_window.Close();
            ms_state = 1;
        }
        else if (ms_state == 1)
        {
            GC.Collect();                        
            ms_state = 2;
        }
        else if (ms_state == 2)
        {
            if (ms_weak.IsAlive)
                Console.WriteLine("failed: window hasn't been GCed");
            else
                Console.WriteLine("passed: window has been GCed"); 
            ms_timer.Stop();
            ms_state = 3;
        }
        else
        {
            Application.Exit();
        }
    }

    private static Timer ms_timer;
    private static Form ms_window;
    private static WeakReference ms_weak;
    private static int ms_state;
}


-- 
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.


More information about the mono-bugs mailing list