[Mono-bugs] [Bug 386483] New: Form::MainMenuStrip prevents GC

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Sun May 4 05:19:04 EDT 2008


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


           Summary: Form::MainMenuStrip prevents GC
           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: ---


Attaching a menu to a form via Form::MainMenuStrip prevents the form from being
GCed. The form is collected if there is no menu or the menu is added via
Form::Menu (with r102443 and r102444).

I haven't spent a lot of time figuring out why the menu strip code fails, but
it looks like a ToolStripDropDownMenu isn't being removed from the static
NativeWindow::window_collection.

I've attached a test case. When testing I'd recommend first letting the test
run without user input and, once that passes, opening the file menu before the
test closes the window.

#define USE_MENUS

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

// The failed message fires if the user does nothing or if he opens the
// file menu.
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.Menu = new MainMenu();
        ms_weak = new WeakReference(ms_window);

 #if USE_MENUS
            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 += DoTransition;
        ms_timer.Interval = 3000;
        ms_timer.Start();
    }

    private static void DoTransition(Object myObject, EventArgs myEventArgs) 
    {
        if (ms_state == 0)
        {
            Console.WriteLine("0) closing window");
            ms_window.Close();
            ms_window = null;
            ms_state = 1;
        }
        else if (ms_state == 1)
        {
            Console.WriteLine("1) collecting");
            GC.Collect();                        
            ms_state = 2;
        }
        else if (ms_state == 2)
        {
            Console.WriteLine("2) checking");
            if (ms_weak.IsAlive)
                Console.WriteLine("FAILED: window hasn't been GCed");
            else
                Console.WriteLine("passed: window has been GCed");    
            ms_state = 3;
        }
        else
        {
            Console.WriteLine("3) quitting");
            ms_timer.Stop();
            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