Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 506181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:40:28+00:00 2026-05-13T06:40:28+00:00

I am using a NativeWindow object to subclass an unmanaged window’s message pump, with

  • 0

I am using a NativeWindow object to subclass an unmanaged window’s message pump, with the purpose of intercepting its messages.

Code structure looks something like this (its psuedo C#, please excuse minor syntax problems):

class AppSubclass : Control {

    class SpecialAppWndProc : NativeWindow {

        protected override void WndProc(ref Message m) {

            switch (m.msg) {

                // do stuff
                if (SpecialEvent != null) SpecialEvent(x);
            }

            base.WndProc(ref m);

        }

        public delegate void SpecialEventHandler(int Xstart);
        public event SpecialEventHandler SpecialEvent;

        ~SpecialAppWndProc() {

            DebugTrace("Help! Save me!");

        }

    }

    private SpecialAppWndProc specialAppWndProc = new SpecialAppWndProc();

    private void StartMonitoring() {

        // do stuff


        specialAppWndProc.AssignHandle(hWndUnmanagedWindow);
        specialAppWndProc.SpecialEvent += new SpecialAppWndProc.SpecialEventHandler(specialAppWndProc_SpecialEvent);

    }

    /* ... event handler ... */

    public AppSubClass() {

        StartMonitoring();

    }

}

Now, I thought that setting an event listener would be sufficient to keep the Garbage Collector at bay, if my object is dieing because of the GC. If it isn’t, is it possible to trace how-and-why? I have never known .Net to just kill objects due to code bugs (exceptions and the occasional silent-failure seem to be the general gist of things) and I have no idea how or why the host app (my app is a COM server for unmanaged code) would have enough knowledge to kill my objects either.

Given that the object dies seemingly randomly (I haven’t been able to pinpoint a certain set of events, only that it dies anywhere from less than a second to a few minutes after StartMonitoring() is called.

It would appear that HandleRef might solve my woes, however I am unclear on how to use that in this context and I can’t think of how to fit it in my code (other than maybe declaring one at the AppSubclass level and then assigning it the SpecialAppWndProc object.

So, how do I prevent my object from dieing before I am ready for it to die?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:40:28+00:00Added an answer on May 13, 2026 at 6:40 am

    You need to store a reference to your object.

    The event works the other direction, keeping the object the event will fire towards alive, not the event source.

    If you add a few calls to GC.Collect and GC.WaitForPendingFinalizers I’m pretty sure you can provoke the problem pretty quick.

    Let me flesh out my answer a bit more.

    An event is basically just a delegate in disguise. The disguise just removes some of the capabilities associated with delegates, so that outside code cannot do whatever it wants with the underlying delegate, but at heart, it is a normal delegate.

    So what is a delegate? A delegate that refers to a single method consists of two things:

    1. A method reference
    2. An object reference (the target)

    When the event is invoked by the object that defines it, like a “Button.Click” event being fired, a specific method (for instance, bt_Click) is fired on a specific object (like an instance of Form1).

    The event thus contains a reference outwards, towards the object on which the method is defined. The event does not do anything to that other object, so that other object, like Form1 in my example above, does not in any way related to this event contain a reference back to the object.

    So in your case, let’s say you have this code:

    AppSubclass app = new AppSubclass(); // this starts monitoring
    

    If you now let that variable fall out of scope, that object is eligible for collection since nothing holds a reference to it. That there are references internally between AppSubclass and SpecialAppWndProc does not matter, there could be references both ways, but if no outside code holds a reference to it, those objects are eligible for collection.

    So you need to store a reference to your object, somewhere, for it to avoid being collected.

    To answer your original question, which was “C#: What is destroying my NativeWindow object and why?”, the answer is that it’s the Garbage Collector that destroys your NativeWindow object, and the reason is that there isn’t a rooted reference to it (by rooted reference, I mean a reference stored in a static variable, a member variable of other rooted references, or as a local variable in an active method.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 324k
  • Answers 324k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In my opinion an administrator that doesn't manage to right-click… May 14, 2026 at 1:12 am
  • Editorial Team
    Editorial Team added an answer I often use Tweeners CurveModifiers._bezier_get to create bezier curves and… May 14, 2026 at 1:12 am
  • Editorial Team
    Editorial Team added an answer $content = $_POST['content']; if ( substr($content, 0, 6) == '<br… May 14, 2026 at 1:12 am

Related Questions

I am using the class pasted below to listen for the keypress event ctrl
I would like to create pop-up windows (of a fixed size) like this: in
I'm working on a simple flex / AIR application with just a mx.TextInput control
I need to provide support for various scanners and printers to an Intranet web
Am coding an AIR 1.5 app in which I want to do a remote

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.