Weird crash after "dragging" NotifyIcon - NullReferenceException

M

Me

I'm getting a NullReferenceException in Unknown Module when I follow
the below steps to create a simple NotifyIcon app that creates the
context menu on the fly(see a little analysis after the steps).

1. Create a new Windows Forms C# solution (I called mine DBChanger).
2. Replace Form1.cs code with this:

--------------------
using System;

namespace DBChanger
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
DBChangerIcon d = new DBChangerIcon();
d.Start();
System.Windows.Forms.Application.Run();
}
}
}
--------------------

3. Create a new class named "DBChangerIcon" and replace code with
this:

--------------------
using System;
using System.Drawing;
using System.Windows.Forms;

namespace DBChanger
{
public class DBChangerIcon
{
NotifyIcon ni;

public void Start()
{
ni = new NotifyIcon();
ni.Icon = new Icon(@"..\..\App.ico");
ni.MouseDown += new MouseEventHandler(ni_MouseDown);

ni.Visible = true;
}

private void miExit_Click(object Sender, EventArgs e)
{
ni.Visible = false;
Application.Exit();
}

private void ni_MouseDown(object Sender, MouseEventArgs e)
{
ContextMenu cm = new ContextMenu();
cm.MenuItems.Add(new MenuItem("E&xit", new
EventHandler(miExit_Click)));

ni.ContextMenu = cm;
}
}
}
--------------------

4. Build and run it.
5. Right click on the tray icon, drag the mouse off the tray icon,
then release the right click.
6. Right click on the tray icon again, release, and click Exit.

That's how I get a NullReferenceException. If I just do step 6
without step 5 then everything works fine.

It happens in the garbage collection during the Application.Exit()
call. Stack trace is:

System.GC.GetTotalMemory
System.Windows.Forms.Application.CollectAllGarbage
System.Windows.Forms.Application.Exit

And it always fails around the second time that function calls
GC.nativeGetTotalMemory();

This is with Framwork 1.1 and I made it in VS.Net 2003.

I can't figure out what I'm missing. Thanks for any help!
 
M

Me

I'm getting a NullReferenceException in Unknown Module when I follow
the below steps to create a simple NotifyIcon app that creates the
context menu on the fly(see a little analysis after the steps).

1. Create a new Windows Forms C# solution (I called mine DBChanger).
2. Replace Form1.cs code with this:

--------------------
using System;

namespace DBChanger
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
DBChangerIcon d = new DBChangerIcon();
d.Start();
System.Windows.Forms.Application.Run();
}
}
}
--------------------

3. Create a new class named "DBChangerIcon" and replace code with
this:

--------------------
using System;
using System.Drawing;
using System.Windows.Forms;

namespace DBChanger
{
public class DBChangerIcon
{
NotifyIcon ni;

public void Start()
{
ni = new NotifyIcon();
ni.Icon = new Icon(@"..\..\App.ico");
ni.MouseDown += new MouseEventHandler(ni_MouseDown);

ni.Visible = true;
}

private void miExit_Click(object Sender, EventArgs e)
{
ni.Visible = false;
Application.Exit();
}

private void ni_MouseDown(object Sender, MouseEventArgs e)
{
ContextMenu cm = new ContextMenu();
cm.MenuItems.Add(new MenuItem("E&xit", new
EventHandler(miExit_Click)));

ni.ContextMenu = cm;
}
}
}
--------------------

4. Build and run it.
5. Right click on the tray icon, drag the mouse off the tray icon,
then release the right click.
6. Right click on the tray icon again, release, and click Exit.

That's how I get a NullReferenceException. If I just do step 6
without step 5 then everything works fine.

It happens in the garbage collection during the Application.Exit()
call. Stack trace is:

System.GC.GetTotalMemory
System.Windows.Forms.Application.CollectAllGarbage
System.Windows.Forms.Application.Exit

And it always fails around the second time that function calls
GC.nativeGetTotalMemory();

This is with Framwork 1.1 and I made it in VS.Net 2003.

I can't figure out what I'm missing. Thanks for any help!

Hmm... Anybody have a better way to create a context menu for a
NotifyIcon on the fly?

Anybody confirm that this happens or doesn't happen on their machine?
I'm using Win2000 Pro with SP3.

Thanks!
 
T

TonyM

Me said:
(e-mail address removed) (Me) wrote in message
Anybody confirm that this happens or doesn't happen on their machine?
I'm using Win2000 Pro with SP3.

Thanks!


Me (You),

I built this on WinXP SP1a with VS2003Pro, .Net 1.1.

No Problems with the Exception. However the "Exit" context menu stays up
afer the first right click and until you actually close the application.
 
M

Me

TonyM said:
Me (You),

I built this on WinXP SP1a with VS2003Pro, .Net 1.1.

No Problems with the Exception. However the "Exit" context menu stays up
afer the first right click and until you actually close the application.

Thanks for trying it!

I found an XP SP1 machine to test on. That behavior (menus staying
after drag) seems to happen with all tray icons in XP. Weird.

I can reproduce my bug by left clicking instead of right clicking in
step 5. So the last steps that make it fail in XP are:

5. Left click on the tray icon, drag the mouse off the tray icon, then
release the left click.
6. Right click on the tray icon, release, and click Exit.

It's looking like I'd better just not use a tray icon. ;) Shame
really.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top