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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:02:20+00:00 2026-05-26T11:02:20+00:00

I am attempting to display an NSMutableArray of an object I created to a

  • 0

I am attempting to display an NSMutableArray of an object I created to a UITableView. Everything displays, except when I try to scroll the table view and then I get an EXC_BAD_ACCESS.

Here’s my object:

.h file

@interface EmailAddressClass : NSObject {
   @public NSString * fullName;
   @public NSString * myEmailAddress;
    BOOL isSelected;
}
@property (nonatomic, retain) NSString * fullName;
@property (nonatomic, retain) NSString * myEmailAddress;


- (void) setFullname:(NSString *)pMyName;
- (void) setMyEmailAddress:(NSString *)pEmailAddress;
- (void) setIsSelected:(BOOL)pSelectedFlag;
- (BOOL) getIsSelected;
- (NSString *) getFullname;
- (NSString *)getMyEmailAddress;
@end

.m file

#import "EmailAddressClass.h"

@implementation EmailAddressClass
@synthesize fullName;
@synthesize myEmailAddress;

- (void) setFullname:(NSString *)pMyName{
    fullName = pMyName;
}

- (void) setMyEmailAddress:(NSString *)pEmailAddress{
    myEmailAddress = pEmailAddress;
}

- (NSString *) getFullname{
    return fullName;
}

- (NSString *)getMyEmailAddress{
    return myEmailAddress;
}

- (void) setIsSelected:(BOOL)pSelectedFlag{
    isSelected = pSelectedFlag;
}

- (BOOL) getIsSelected{
    return isSelected;
}

@end

Straightforward enough; just a small class that holds name and email address as NSStrings.

This is how I populate the array from address book with names and email addresses

- (NSMutableArray*)getAllEmailAddress{
    CFMutableArrayRef myMutablePeople = [self getSortedAddressBook];
    NSMutableArray *allEmails = [[NSMutableArray alloc] initWithCapacity:CFArrayGetCount(myMutablePeople)];


    for (CFIndex i = 0; i < CFArrayGetCount(myMutablePeople); i++) {
        ABRecordRef person = CFArrayGetValueAtIndex(myMutablePeople, i);
        ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
        NSString *firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
        NSString *lastName = (NSString *) ABRecordCopyValue(person, kABPersonLastNameProperty);

        for (CFIndex j=0; j < ABMultiValueGetCount(emails); j++) {
            NSString* email = (NSString*)ABMultiValueCopyValueAtIndex(emails, j);
            EmailAddressClass * anEmailPerson = [EmailAddressClass alloc];
            [anEmailPerson setIsSelected:YES];
            [anEmailPerson setMyEmailAddress:email];
            if ((firstName) && (lastName) != nil){    
                [anEmailPerson setFullname:[NSString stringWithFormat:@"%@ %@", firstName, lastName]];
            } else if ((firstName != nil) && (lastName == nil)){
                [anEmailPerson setFullname:[NSString stringWithFormat:@"%@", firstName]];
            } else if ((firstName ==nil) && (lastName != nil)){
                [anEmailPerson setFullname:[NSString stringWithFormat:@"%@", lastName]];
            } else {
                [anEmailPerson setFullname:[NSString stringWithFormat:@"%@", email]];

            }

            [allEmails addObject:anEmailPerson];
            [email release];
            [anEmailPerson release];
        }
        CFRelease(emails);
    }
    CFRelease(myMutablePeople);
   // CFRelease(people);
    return  allEmails;
}

Everything is working up to this point; the array returns the correct number of people, with their email address.

This is how I populate the table view

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    thisPerson = (EmailAddressClass *)[allEmails objectAtIndex:indexPath.row];

    cell.textLabel.text = [thisPerson getFullname];

    return cell;
}

What am I missing?

snipped of of EmailListViewController.h

#import "EmailAddressClass.h"

@interface EmailListViewController : UITableViewController {

    NSMutableArray *allEmails;
    EmailAddressClass * thisPerson;
}


@property (nonatomic, retain) NSMutableArray *allEmails;
@property (nonatomic, retain) EmailAddressClass * thisPerson;

@synthesize allEmails, thisPerson; both gets synthesized in the .m file

on Viewdidload in EmailListViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.toolbarHidden=NO;
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 self.tableView.rowHeight = ROW_HEIGHT;
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    allEmails = [[MsgController sharedMsgController] getAllEmailAddress];

    /*NSLog(@"There are : %d number of emails", [allEmails count]);

    for (EmailAddressClass *email in allEmails)
    {
        NSLog(@"%@ : %@", [email getFullname],[email getMyEmailAddress]) ;
    }
     */
}

the console output shows correct names and emails in the array

  • 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-26T11:02:20+00:00Added an answer on May 26, 2026 at 11:02 am

    When you’re looping and setting, you create the string by calling stringWithFormat. Since that’s not alloc, copy, mutableCopy, it’s autoreleased.

    Then you assign it, hold it but do not retain it:

    In the loop:

    // this is autoreleased
    [anEmailPerson setFullname:[NSString stringWithFormat:@"%@ %@", firstName, lastName]];
    

    In the class:

    // you're holding a reference to something that's about to go poof
    - (void) setFullname:(NSString *)pMyName{
        fullName = pMyName;
    }
    

    I would recommend either retaining it or declaring it as a property and synthesizing it:

    @property (retain, atomic) NSString *fullName;
    

    then in .m

    @synthesize fullName;
    

    I would also recommend that your getAllEmailAddresses function does an autorelease – the caller of the function can choose to retain it. Have your allEmails property retain that after your function autoreleases. The memory management rules say that if it doesn’t start with alloc, copy or immutableCopy, it’s autoreleased and it’s up to the caller to retain.

    Read the apple memory management guide – good read.

    http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html

    Specifically, on the second page, read the rules.

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

Sidebar

Related Questions

I'm attempting to display a LargeIcon view in a listview control, however the images
I'm attempting to display a modal dialog as a test run before I try
I want to try something different, and am attempting to display an overlay on
I'm attempting to use a side navigation to display table rows by category using
I am attempting to implement a custom view. This view should display an image
I am attempting to display literal HTML to the browser via my Razor View.
I'm attempting to display my data, which is in a UITableView, in custom cells
I'm attempting to display a table of data which looks like this on Windows
I have created a JSON object in php using json_encode and am attempting to
I'm attempting to display a logo (PNG created in Paint.NET) on my web page

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.