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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:35:24+00:00 2026-06-04T11:35:24+00:00

In my connect4 style game I have an array representing a 7×6 grid, each

  • 0

In my “connect4” style game I have an array representing a 7×6 grid, each “cell” in the array contains either NSNull or a UIView subclass ‘CoinView’. Is the following the correct way to remove objects from the NSMutableArray and the primary view?

- (IBAction)debugOrigin:(id)sender {
    int x = 0;
    int y = 0;
    //get the coin object form the grid
    CoinView *coin = [[grid objectAtIndex:x] objectAtIndex:y];

    //cancel if there's no coin there
    if ([coin isKindOfClass:[NSNull class]]) { return; }

    //remove the coin from memory
    [coin removeFromSuperview];
    coin = nil;
    [[grid objectAtIndex:x] setObject:[NSNull null] atIndex:y]; //will this leak?

}

Thanks!

  • 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-04T11:35:25+00:00Added an answer on June 4, 2026 at 11:35 am

    Your code will not leak, and is in fact (almost) correct.

    You should remove this comment, as you’re not dealing with memory in your code (and it may end up confusing you as to what the code really does):

    //remove the coin from memory
    

    In the following line you’re removing the view referenced by local variable “coin” from its superview:

    [coin removeFromSuperview];
    

    And you assign nil to your local variable coin, which is good practice to make sure it’s not being used later in the code:

    coin = nil;
    

    As far as I know there is no setObject:AtIndex: for NSMutableArray. Use replaceObjectAtIndex:withObject: instead:

    [[grid objectAtIndex:x] replaceObjectAtIndex:y withObject:[NSNull null]]; //will this leak?
    

    As a last note, I recommend that you read up a bit on memory management and memory leaks (from Apple’s developers documentation). The first offers you some hints and tips that make memory management much easier to understand.

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

Sidebar

Related Questions

All, I am trying to code a Connect4 game. For this, I have created
I would like to have the menu part of my game a navigation style
I'm working on a class project to build a little Connect4 game in Java.
For example, I have style <Style TargetType=Button> ... </Style> in file Button.xaml If I
I have the following in my controller: def index @games = Game.gamsearch(params[:gamsearch]) end def
I have 50+ kiosk style computers that I want to be able to get
I have created my own mvc pattern based on the codeigniter framework style. My
I have the following button style in Themes/Generic.xaml and I want it to apply
I have a client/server style application which communicates using WCF which all works great.
We have a bunch of cli cron style scripts that are coded in php.

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.