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 6741163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:41:32+00:00 2026-05-26T11:41:32+00:00

Have peek at the following link: GLSurfaceView code sample Specifically, look at the following

  • 0

Have peek at the following link: GLSurfaceView code sample

Specifically, look at the following function in the second code block on that page:

public boolean onTouchEvent(final MotionEvent event) {
    queueEvent(new Runnable() {
        public void run() {
            mRenderer.setColor(event.getX() / getWidth(), event.getY() / getHeight(), 1.0f);
        }
     });

     return true;
}

What confuses me here is that the anon Runnable references the very much local “event” object, but queueEvent actually ends up running the Runnable on a completely separate thread, and thus possibly long after that particular instance of “event” is long dead, which is obviously a problem. Or is it the case that sort of of local-ref-within-anon-class usage actually does a +1 on the ref count of the “event” object so that it sticks around as long as the anon Runnable is still breathing?

Also, does the fact that the MotionEven is declared final have anything to do with all of this?

P.S.
As a related question (though it’s not my main concern), what if instead of (or in addition to) “event” we wanted referenced some primitive like “int x” inside of the anon class, what happens then? (seeing as you can’t +1 the ref count of a primitive).

  • 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-26T11:41:32+00:00Added an answer on May 26, 2026 at 11:41 am

    Java doesn’t use reference counting to do its garbage collection. I don’t know a lot specifically about how the JVM implements this sort of closure, but I can only presume the anonymous class instance contains a reference to event. That being the case, as long as the anonymous Runnable is alive then everything it references is alive (and everything those things reference is alive, etc, etc).

    As for primitive types, they’re just copied directly into the fields. So there are no ‘references’ to something that needs to live as long as it is referenced and then be garbage collected; they’re just a part of the objects that contain them. Every time you “make a new reference” to the int 287, it’s not that there is a place storing 287 to which you now have another reference, you just have another memory word storing 287. So it doesn’t matter when the 287 you originally got yours from is deallocated, yours is completely independent.

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

Sidebar

Related Questions

I have the following class: class Stack { struct Link { void* data; Link*
I have a Gem that I found at RubyForge and want to peek inside
hi i have the following code: private void Textparsing() { using (StreamReader sr =
I have a problem with the following code fragment. It's intended to handle events
Hi i have the following code which works fine in my machine but when
So I have a text file that reads as the following -9 5.23 b
i have the following code #flipper #Front { z-index:81; } #flipper #Back { z-index:80;
Should be pretty simple: I have an InputStream where I want to peek at
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have you managed to get Aptana Studio debugging to work? I tried following this,

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.