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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:01:09+00:00 2026-06-11T02:01:09+00:00

I have a small confusion. When I print my array in tableView dataSource method

  • 0

I have a small confusion. When I print my array in tableView dataSource method numberOfRowsInSection , my App crashes.

This is my code : in .h file

@interface AddColor : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
UITableView *tblView;
NSArray *arrayColors;
}
@property(nonatomic,retain)NSArray *arrayColors;

@end

In .m file

@synthesize arrayColors;
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:NO];

     arrayColors = [NSArray arrayWithObjects:@"blackColor", @"darkGrayColor", @"lightGrayColor", @"whiteColor",  @"grayColor", @"redColor", @"greenColor", @"blueColor", @"cyanColor", @"yellowColor", @"magentaColor", @"orangeColor", @"purpleColor", @"brownColor", nil];


    tblView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];
    tblView.delegate=self;
    tblView.dataSource=self;
    [self.view addSubview:tblView];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

   NSLog(@"%d",[arrayColors count]);//App crashes here.
   return [arrayColors count];
}

My app crashes when I print [arrayColors count];

I find the solution for crash, I simply retain the array in viewDidLoad

[arrayColor retain];

and now working fine. but why my app crashes before when I print the [arrayColor count];

  • 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-11T02:01:11+00:00Added an answer on June 11, 2026 at 2:01 am
    + [NSArray arrayWithObjects:]
    

    creates an autoreleased array – it’s unsepcified when it is released, but most likely it’s released when the viewDidLoad method returns, so it’s an invalid (garbage) pointer that you access in [arrayColor count].

    By retaining the array, you get rid of the deallocation error, but now you’re leaking memory. The general approach of solving this the right way is to allocate and initialize the array in one of the initializer methods, like

    - (id)init
    {
        if ((self = [super init])) {
            arrayColors = [[NSArray alloc] initWithObjects:..., nil];
        }
        return self;
    

    }

    and then purge it in - dealloc in order not to leak memory:

    - (void)dealloc
    {
        [arrayColors release];
        [super dealloc];
    

    }

    More on the topic on Apple Developer.

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

Sidebar

Related Questions

I have a small confusion with the IDisposable interface and the using keyword in
I have small question by entity framework 4.1. There is interface of image DTO:
I have small test app: Socket socket = new Socket(jeck.ru, 80); PrintWriter pw =
I have small web app that generate PDF files as a report. I'm trying
I have a small XML file: <wddxPacket version='1.0'> <header/> <data> <struct type='coldfusion.runtime.ArgumentCollection'> <var name='HEADLINE'>
I have a small C program to calculate hashes (for hash tables). The code
I have a small problem in coding confusion 3x3 matrix by using matlab... I
I have a small confusion in my application. How can I check whether an
I have a small confusion on SSL handshake between browser and server in a
All, I Have a small confusion about Case sensitivity of ODBC DSN names. Say,

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.