[Mono-bugs] [Bug 601778] New: Overriden property "Text" of UserControl is accessed before user control constructor finished

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Sun May 2 10:11:04 EDT 2010


http://bugzilla.novell.com/show_bug.cgi?id=601778

http://bugzilla.novell.com/show_bug.cgi?id=601778#c0


           Summary: Overriden property "Text" of UserControl is accessed
                    before user control constructor finished
    Classification: Mono
           Product: Mono: Runtime
           Version: 2.6.x
          Platform: i386
        OS/Version: Windows 7
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: misc
        AssignedTo: mono-bugs at lists.ximian.com
        ReportedBy: ralf.kornelius at gmx.de
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---
           Blocker: No


Created an attachment (id=358457)
 --> (http://bugzilla.novell.com/attachment.cgi?id=358457)
TestProject (VisualStudio 2008 Express)

Description of Problem:
Overriden property "Text" of UserControl is accessed before user control
constructor finished. This may cause a NullReferenceException when accessing a
not yet constructed inner control.
Discovery of this bug was the creation of such a user control in an
eventhandler of the notify icon which doesn't ignore the
NullReferenceException. The program flow just quits the property without any
notice.


Steps to reproduce the problem:
1. Create a user control with a label called "labelProgressText"
2. Override property "Text" in user control. Return in this property text value
of an label from the user control, e.g.:
--------------------------------------------------
public BugUserControl()
        {
            Console.WriteLine("BugUserControl - Constructor start");
            InitializeComponent();
            Console.WriteLine("BugUserControl - Constructor finished");
        }

        public override string Text
        {
            get
            {
                Console.WriteLine("BugUserControl - Text: " +
(this.labelProgressText == null ? "null" : "not null"));
                string s = null;
                try
                {
                    s = this.labelProgressText.Text;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.WriteLine("BugUserControl: now returning!");
                return s;
            }
        }  
--------------------------------------------------


3. Create an instance of this user control in main method:
--------------------------------------------------
Console.WriteLine("BugUserControl: Constructor start");
            InitializeComponent();
            Console.WriteLine("BugUserControl: Constructor finished");
--------------------------------------------------

4. Run the app
Before the user control is finished with constructor the "Text" property is
accessed resulting in an NullRefernceException because at this time the needed
label isn't constructed!

In .NET the "Text" property is not accessed before object creation completed!


Actual Results:
Mono:
--------------------------------------------------
Main: BugUserControl creating...
BugUserControl: Enter property 'Text'!
BugUserControl: Text: null
Object reference not set to an instance of an object
BugUserControl: Return property 'Text'!
BugUserControl: Constructor start
BugUserControl: Constructor finished
Main: BugUserControl created
--------------------------------------------------

Expected Results:
NET:
--------------------------------------------------
Main: BugUserControl creating...
BugUserControl: Constructor start
BugUserControl: Constructor finished
Main: BugUserControl created
--------------------------------------------------



5. Instanciate this user control in notify icon click eventhandler
--------------------------------------------------
Addition in main():
NotifyIcon icon = new NotifyIcon();
icon.Icon = new System.Drawing.Icon("browser-chrome.ico");
icon.Visible = true;
icon.Click += new EventHandler(icon_Click);
Application.Run();


icon_Click():
Console.WriteLine("NotifyIconClick: BugUserControl creating...");
BugUserControl bu = new BugUserControl();
Console.WriteLine("NotifyIconClick: BugUserControl created");
Environment.Exit(0);
--------------------------------------------------


6. Start app and click on NotifyIcon


Actual Results:
Mono:
--------------------------------------------------
Main: BugUserControl creating...
BugUserControl: Enter property 'Text'!
BugUserControl: Text: null
Object reference not set to an instance of an object
BugUserControl: Return property 'Text'!
BugUserControl: Constructor start
BugUserControl: Constructor finished
Main: BugUserControl created

NotifyIconClick: BugUserControl creating...
BugUserControl: Enter property 'Text'!
BugUserControl: Text: null

<<<EventHandler exits here without UnhandledException/RuntimeCrash/CoreDump, so
app never quits>>>
--------------------------------------------------


Expected Results:
NET:
--------------------------------------------------
Main: BugUserControl creating...
BugUserControl: Constructor start
BugUserControl: Constructor finished
Main: BugUserControl created

NotifyIconClick: BugUserControl creating...
BugUserControl: Constructor start
BugUserControl: Constructor finished
NotifyIconClick: BugUserControl created
--------------------------------------------------





How often does this happen? 
Always

Additional Information:
TestProject is attached (VisualStudio 2008 Express)

-- 
Configure bugmail: http://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