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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:40:22+00:00 2026-05-15T07:40:22+00:00

I am learning Obj-C but still occasionally have a difficult time wrapping my head

  • 0

I am learning Obj-C but still occasionally have a difficult time wrapping my head around some of the mem management stuff. I am using custom cells with a UITableView, and implemented the cellForRowAtIndexPath method where I accidentally released the cell at the end. This obviously caused problems as the cell was also getting released when the tableView was popped. This led to a crash due to releasing the cell twice – no prob, understand.

However, as I kept working, I intermingled standard and custom cells, so my method got a bit more complex. My first try was the below, which caused the same problem as the above scenario. This is where I am a bit confused – since I am not releasing “cell”, why can’t I release “customCell” after I set cell to its value?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     UITableViewCell *cell;

     //CATEGORY_SECTION is a constant defined elsewhere
     if (indexPath.section == CATEGORY_SECTION) {
          cell = [tableView dequeueReusableCellWithIdentifier:@"StandardCellIdentifier"];
          if (cell == nil) {
               cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"StandardCellIdentifier"] autorelease];
               cell.accessoryType = UITableViewCellAccessoryCheckmark;
          }
          cell.labelText.text = myModelObject.name;

     } else {

          cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCellIdentifier"];
          MyCustomCellClass *customCell;
          if (cell == nil) {
               UIViewController *helperController = [[UIViewController alloc] initWithNibName:@"MyCustomCell" bundle:nil];
               customCell = (MyCustomCellClass *)[helperController view];


               [helperController release];
          }

     customCell.myCustomLabel.text = myModelObject.description;
     cell = customCell;
     [customCell release];
     }

     return cell;
}

I understand that when I set cell = customCell, I am assigning customCell’s memory address, not the actual object, to cell…so when I release customCell it is also in effect releasing cell? How would I actually copy customCell so I could release it? Or do I not have to release it (even though I alloc’d it) – it seems like a memory leak waiting to happen, how would you approach it?

For the record, here is the revised code I used to avoid this issue. This question isn’t so much as finding a solution (since I have, below) but understanding what is happening behind the scenes. Thanks for any guidance!

My working method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     //CATEGORY_SECTION is a constant defined elsewhere
     if (indexPath.section == CATEGORY_SECTION) {
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StandardCellIdentifier"];
          if (cell == nil) {
               cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"StandardCellIdentifier"] autorelease];
               cell.accessoryType = UITableViewCellAccessoryCheckmark;
          }
          cell.labelText.text = myModelObject.name;

          return cell;

     } else {

          MyCustomCell *customCell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCellIdentifier"];

          if (customCell == nil) {
               UIViewController *helperController = [[UIViewController alloc] initWithNibName:@"MyCustomCell" bundle:nil];
               customCell = (MyCustomCell *)[helperController view];
               [helperController release];
          }

          customCell.myCustomLabel.text = myModelObject.description;

          return customCell;
     }
}
  • 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-15T07:40:22+00:00Added an answer on May 15, 2026 at 7:40 am

    Right, you’re releasing the same memory, which both variables point to. If you were setting a class property, for example, the property would probably be marked with retain. If a property has retain it means it wants to “own” the object. You could then release it.

    In your case, your cell variable doesn’t own the memory, so you can’t release it.

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

Sidebar

Related Questions

To preface, I'm very new to Obj-C but I do have some OOP experience
I'm learning obj-c and I have a question about memory management: Say I have
I'm still learning some of this c# stuff, and I couldn't find an answer
I'm still in the beginning stage of learning c#, so i have a question
Hi i am new to iphone and starts to learning Obj c. i have
I have created some C++ classes to model a Solitaire game as a learning
Its been quite some time since i have started developing web pages using JSF
Still learning my ways around C# generics... is it possible to achieve this functionality?
I'm still learing VB.NET and usually I just google my questions, but this time
Just a quick question if I may, I have always (since learning obj-c) written

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.