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

  • SEARCH
  • Home
  • 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 8057945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:11:48+00:00 2026-06-05T09:11:48+00:00

Possible Duplicate: Do event handlers stop garbage collection from occuring? I had one wp7

  • 0

Possible Duplicate:
Do event handlers stop garbage collection from occuring?

I had one wp7 application like this:

private void button1_Click(object sender, RoutedEventArgs e)
{
    GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
    watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
    watcher.Start();
}

void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
    Debug.WriteLine(e.Position.Timestamp.ToString());
}

After I click the button twice,the Console will output the Timestamp twice.
But the watcher was a local variable!
What’s wrong with it?
And how can I distory it?

  • 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-06-05T09:11:51+00:00Added an answer on June 5, 2026 at 9:11 am

    watcher is a local variable, but that doesn’t affect the object necessarily. You’ve asked the GeoCoordinateWatcher to start – I’d expect it to maintain a reference to itself, effectively, or stash one somewhere appropriate.

    It sounds like either you ought to disable the button once it’s clicked, or you need to keep the watcher in an instance variable so that you can dispose of the old one and create a new one. (I’m not sure why that would be useful though.)

    EDIT: As there are two incorrect answers here, let me just clear something up… an event publisher (the watcher in this case) has references to the handler delegates. If those delegates refer to instance methods (as it does in this case) then there’s a reference to an instance of the type containing that method:

     Event publisher => delegate => instance of type with handler method
    

    That means that as long as the publisher isn’t garbage collected (and the event handler still exists), the instance associated with the delegate can’t be collected. It doesn’t prevent the publisher itself from being garbage collected.

    In other words, if GeoCoordinateWatcher didn’t do something “special” (probably in the Start method) it could be garbage collected. There’s no implicit reference from an event handler to the event publisher which prevents it from being garbage collected that way round.

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

Sidebar

Related Questions

Possible Duplicate: How do I Unregister 'anonymous' event handler I have code like this:
Possible Duplicate: Inspect attached event handlers for any DOM element Is there a tool
Possible Duplicate: Event handlers inside a Javascript loop - need a closure? I have
Possible Duplicate: How would that be possible to remove all event handlers of the
Possible Duplicate: Throttle event calls in jQuery I like the .change() functionality of jQuery,
Possible Duplicate: Prevent click event from affecting parent jquery I have a DOM structure
This is a possible duplicate. Couldn't find the exact one, I remember going through
Possible Duplicates: Timer, event and garbage collection : am I missing something ? If
Possible Duplicate: JavaScript Event Listeners vs Event Handlers element.onload vs element.addEventListener(“load”,callbak,false) I've read this
Possible Duplicate: event handles & visible I am new to learning WebRequest.This is my

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.