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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:21:30+00:00 2026-05-25T12:21:30+00:00

I have a function that takes another function as a parameter. Something like this

  • 0

I have a function that takes another function as a parameter. Something like this :

public function onHits(target : Shape, callback : Function) : void

I use it by passing a member function as a parameter that should be called whenever the passed target hits something. The function is called many times a frame. So it is used by doing :

//code...
CollisionManager.onHits(myShape, onHitCB);
//code...

The on hit function :

public function onHitCB(hitObject : *) : void 
{
    //removed all code to test this problem
}

When I do this, I have a memory leak. I’ve isolated the problem to that onHits method and have commented out everything else. onHits is an empty method with no code inside it, onHitCB is also empty. If I comment out the call to onHits, there is no memory leak and if I pass null instead of onHitCB there is no memory leak.

So it’s clearly when I pass onHitCB as a parameter that’s the problem. So I thought it might be because Flash allocates some memory to create the Function pointer and doesn’t release it but I call System.gc() every frame in debug mode and the leak is still there. Which would mean that this is either a bug in the SDK or I’m not doing something right.

I have found a weird workaround by keeping a variable that points to the function which I assign in the constructor of my object :

private var func : Function;

public function MyObject() 
{
    func = onHitCB;
}

and this will clear the memory leak even if I still pass onHitCB as the parameter. So that would mean that it’s not the “getter” function to obtain the onHitCB but something else causing the memory leak?

I’m very confused. How can this cause a memory leak :

public function MyObject() 
{
}

public function update() : void
{
    CollisionManager.onHits(myShape, onHitCB);//empty function
}

public function onHitCB(hitObject : *) : void 
{
    //removed all code to test this problem
}

but not this? :

private var func : Function;
public function MyObject() 
{
    func = onHitCB;
}

public function update() : void
{
    CollisionManager.onHits(myShape, onHitCB);//empty function
}

public function onHitCB(hitObject : *) : void 
{
    //removed all code to test this problem
}

and is there a way to not have to do this workaround?

  • 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-25T12:21:31+00:00Added an answer on May 25, 2026 at 12:21 pm

    […] bound methods are automatically created when you pass a method as a parameter. Bound methods ensure that the this keyword always references the object or class in which a method is defined. Source

    That sounds like creating a reference to a method isn’t using a simple getter. A new method closure object is generated. So your assumption is right.

    I wonder why the references aren’t cached for each instance and why they aren’t garbage collected though. Better avoid creating multiple references. Referencing a method only once is exactly what I would do when I would have to use that method in multiple places, so most of the time I wouldn’t call it a workaround but a good DRY practice. In your example it makes sense of course, assuming a method reference would be using a simple getter.

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

Sidebar

Related Questions

I have a function that takes, amongst others, a parameter declared as int privateCount
I have a function that takes pointer to pointer to struct. struct ABC; void
I have this Array i wrote a function MostFreq that takes an array of
I have a logging function that takes the calling object as a parameter. I
Say I have a function that takes a list and does something: (defun foo(aList)
I have a function that takes a struct, and I'm trying to store its
I have a Python function that takes a numeric argument that must be an
I have created a function that takes a SQL command and produces output that
If I have an f# function that takes a string containing a sentence, what
I have a function called FindSpecificRowValue that takes in a datatable and returns the

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.