locked
Object already loaded error in Interop User Control

    Question

  •  

    Hi,

     

    I have a VB6 MDI Project that displays it's children as maximised windows, i.e. it gives the illusion of SDI! When migrating this app to VB.NET I have created a VB.NET Interop user control that minics the child windows functionality, i.e. typically lists data from a database. I then place the User Control in a VB6 MDI Child form. I hook the MDI Child forms resize event to resize the User Control to the correct height and width.

     

    When the users chooses a different menu option a different MDI child is displayed and the currently display mdi child is Unloaded and set to nothing.

     

    The first time the mdi child with user control is displayed everything is fine. If I then choose a different mdi child that too is displayed OK. When I choose the original mdi child a second time I get an error in the VB6 mdi child form Resize event handler as I am calling the <UserControl>.Move method. The error is:

     

    Run-time error: 360

    Object already loaded.

     

    Is this because I need to dispose my Interop user control when the mdi child form is unload? If so how do I do this??

     

    Thanks for any help

     

    Graham

    Wednesday, November 21, 2007 5:00 PM

Answers

  • In the 2nd case you are actually creating a new instance of the form.  IN the first instance, you just recycling the frmNew instance. 

     

    As long as there isn't any state information being lost in creating a new instance of the form, you should otherwise be ok with this.

     

    Toddap_MS

    Sunday, December 16, 2007 3:46 AM
    Moderator

All replies

  • That could very well be the case since we did have to add specialized code in places to get the control to destroy itself and this might be one of those cases we missed.    You should be able to debug this somewhat if you follow the debugging instructions in the interopforms help (basically having vb6.exe launch from VB.NET with your project loaded)

     

    What you could try quickly is exposing a public method in your usercontrol that calls me.dispose and then calling that before you unload your usercontrol.

     

    If you want, please zip up a small repro case and mail it to me and I can investigate further.   You can send it to me here:

     

    Toddap_MS@microsoft.com (remove the _MS)

     

    Good luck!

     

    Todd

    Thursday, November 29, 2007 5:38 PM
    Moderator
  • This is strange. I tried you idea but it still failed most times. However, I have fixed it but I can't explain why, perhaps you could:

     

    I have a small VB project that contains frmMain and frmNew.

     

    frmMain is the main windows. frmNew contains the .NET User Control.

     

    when frmMain shows the frmNew is does this:

     

    Dim f as Form

    set f = frmNew

     

    f.Show

     

    This causes the error to happen most times on second showing. I actually have two forms I switch between (frmNew and frmNew2)

     

    However, if I use this:

     

    Dim f as Form

    set f = new frmNew

     

    f.Show

     

    it works ok.

     

    Can you help explain what is the difference between using f=frmNew and f = New frmNew?

     

    Thanks

     

    Graham

     

    Friday, December 07, 2007 8:19 AM
  • In the 2nd case you are actually creating a new instance of the form.  IN the first instance, you just recycling the frmNew instance. 

     

    As long as there isn't any state information being lost in creating a new instance of the form, you should otherwise be ok with this.

     

    Toddap_MS

    Sunday, December 16, 2007 3:46 AM
    Moderator
  • I am having a very similar problem to that described by the original poster. I am not using MDI - my interop control resides in a non-modal SDI VB6 form.

     

    I have noticed the following:

     

    - the first time I load the form containing the interop control, all is well.

    - I have verified that Dispose() is being called on the interop control when the form unloads

    - the second time I load the containing form, I see error 360 the first time I hit any method or property on the control.

    - at Form_Unload time, I get a different error - error 364 ("Object was unloaded") - when I am trying to clean up

    - the third time I load the containing form, all is well again

    - the 4th time, it is broken again (and so on - it works every 2nd time).

     

    I have tried putting the Dispose() method on the public interface and called it from the "Form_Unload" event handler. No difference in behavior. I have tried running the interop project in the debugger with all exceptions set to trap immediately, but no exceptions were seen.

     

    It feels like the control is not being "really" unloaded till after I unload the 2nd instantiation of the containing form.

     

    Any suggestions or ideas? I cannot easily implement the workaround used by the original poster (to dynamically create a new form each time).

     

    Thanks,

    Ron

     

    • Proposed as answer by martin gong Thursday, July 23, 2009 9:39 AM
    Wednesday, April 02, 2008 7:04 PM
  • Hi, I have the same issue, and still couldn't find any solution for that issue :( This is very annoying. I know that the problem is with reusing forms but how to make sure form is unloaded to create new one
    Wednesday, December 23, 2009 3:40 PM
  • Hi,

    I encountered the same problem and it seems to be fixed when I add this line to the WndProc method of InteropUserControl:

     

    After

       Me.Dispose()

    add

       System.GC.Collect()

     

    Hope it works for you.

    Cheers

    Tumer

    • Proposed as answer by Colizobble Saturday, October 16, 2010 8:53 AM
    Thursday, June 17, 2010 2:04 PM