Garbage Collector deleting Win32 window wrapped in NativeWindow Class

Hi

I have a class derived from NativeWindow which calls CreateHandle to create a WIN32 registered window.

However, the WIN32 window created suddenly gets destroyed and I've proved its the Garbage Collector thats doing it presumably because the derived NativeWindow Class gets destroyed. The NativeWindow Class is created in a UserControl derived class which in turn is hosted on a Windows Form.

I did post a link to the original post I made on the WIndows Forms forum which included sample code but a moderator deleted the post as a Cross Post but its not a WIndows Forms issue but a Garbage Collection one so I think this is the correct forum for it. I darent post the link again in case the post is deleted.

Why would the Garbage Collector destroy a NativeWIndow class and would there be a way to stop it doingso.

[1171 byte] By [Steve98796] at [2007-12-22]
# 1
Steve98796 wrote:

Why would the Garbage Collector destroy a NativeWIndow class and would there be a way to stop it doing so.

Like any other object, it should only get garbage collected when there are no references to it anymore or when the appdomain is being destroyed. Are you sure you're holding a reference to the object?

MattiasSj?gren at 2007-8-30 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 2

Mattias

Thanks for the reply.You were right. I just worked this out too. Whilst being an experienced C programmer, I am fairly new to OOP, C++, .NET and Managed C++/CLI.

My class declaration was:

ref class MyLogoControl: public UserControl

{

public:

MyLogoControl()

{

MyLogoWindow^ LogoWindow;

LogoWindow=gcnew MyLogoWindow(this);

this->BorderStyle=System::Windows::Forms::BorderStyle::Fixed3D;

}

};

where MyLogoWIndow was the derived NativeWindow class

Adding

private:

MyLogoWindow^ LogoWindow;

solved the problem

Such a basic error but years of declaring things on the stack will take some time to get away from!

Steve98796 at 2007-8-30 > top of Msdn Tech,.NET Development,Common Language Runtime...

.NET Development

Site Classified