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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:07:37+00:00 2026-05-23T14:07:37+00:00

I know this probably depends on how large of an object you are allocating,

  • 0

I know this probably depends on how large of an object you are allocating, but I just want to know if I keep on doing things this way I will run into problems later on. For right now, everything seems fine. I am using Cocos2D.

Take this method for instance:

-(void) checkCGPointDistances{

    CCSprite *tempSp;

    CCLabelTTF *tempLabel;

    for(int i=0; i<hummingBirdsMax; ++i){

        tempSp = [hummingBirds objectAtIndex:i];

        checkDistance = [[CheckDistance alloc] init];

        if([checkDistance checkDistanceWithLimit:tempSp.position withPoint2:hero.heroSprite.position withLimit:150] == YES){

            if(hero.heroSprite.position.x > tempSp.position.x){
                tempSp.flipX = YES;
            }else{
                tempSp.flipX = NO;
            }

            tempLabel = [hummingLabels.labelsArr objectAtIndex:i];


            [tempLabel setString:@"Hello!"];

            [hummingLabels.deactivateLabelToggle replaceObjectAtIndex:i withObject:[NSNumber numberWithInt:1]];
        }

        [checkDistance release];

    }

    if(abilityRushH == 0){
        checkDistance = [[CheckDistance alloc] init];

        if([checkDistance checkDistanceWithLimit:rushH.rushHSprite.position withPoint2:hero.heroSprite.position withLimit:32] == YES){
            [rushH.rushHSprite removeFromParentAndCleanup:NO];

            abilityRushH = 1;
            NSLog(@"horizontal rush activated");
        }

        [checkDistance release];
    }

    if(abilityRushV == 0){
        checkDistance = [[CheckDistance alloc] init];

        if([checkDistance checkDistanceWithLimit:rushV.rushVSprite.position withPoint2:hero.heroSprite.position withLimit:32] == YES){
            [rushV.rushVSprite removeFromParentAndCleanup:NO];

            abilityRushV = 1;
            NSLog(@"vertical rush activated");
        }

        [checkDistance release];
    }
}

Notice how I’m allocating and deallocating checkDistance for every loop in the for loop? This method is also being called on a timer very quickly. The reason I decided to do the allocation over and over inside the loop verses outside is because the number of hummingBirdsMax may be 0, so it would not need to do allocation at all. I suppose I could just check if hummingBirdsMax > 0, but is the way I’m doing it make much difference?

The class that it allocates just checks a distance between to CGPoints and returns a bool.

  • 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-23T14:07:37+00:00Added an answer on May 23, 2026 at 2:07 pm

    There’s nothing inherently wrong with what you’re doing. There’s no way to tell whether object allocation and deallocation are performance bottlenecks in your code without some work in the profiler (Shark or Instruments). As with all performance optimization decisions, there’s no reason to even speculate without some data.

    That said, if all the CheckDistance class does is perform a calculation, it’s not really an object (i.e. that encapsulates state and exposes behavior). Why not just implement the calculation as a C function? Unlike the managed Java/C# world that many folks are now used to, not everything has to be (nor should it be) a class.

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

Sidebar

Related Questions

I know this probably really simple but Im not sure what im doing wrong...
I know this is probably the canonical It depends... question but I'd appreciate any
I know this is probably a simple question but my mind just isn't working
I know this probably has been asked before but I am having issues with
I know this is probably something simple but I can't seem to find anything
I know this has probably been asked before but for my problem I cannot
I know this is probably a really amateur question, but I can't figure this
I know this is probably possible using Streams, but I wasn't sure the correct
I know this is probably a very simple question but how would I do
I know this is probably super easy, but I'm having a hard time wading

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.