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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:33:18+00:00 2026-05-18T01:33:18+00:00

i’ve been dancing with a tambourine for a while, but still don’t know what’s

  • 0

i’ve been dancing with a tambourine for a while, but still don’t know what’s the reason for that error.
I’ve got a tableView with history of user queries data from sqlite base. I’m new to iPhone developing, so my code may be a bit excessive. The hierarchy is:

  • HistoryModel
    model-object with some init methods

  • HistoryDataController
    gets data from database and presents an array of HistoryModel objects

  • HistoryViewController
    subclass of UITableView, displays data

  • AppDelegate
    there i initially store an array of HistoryModel objects (by getting it from HistoryDataController) for HistoryViewController to access it.

The problem is, when i scroll the table or open the tab with it for the second time – it crashes with -[CFString retain]: message sent to deallocated instance

Code:

HistoryModel.h
pretty unnecessary class for that case, but i want that worked to repeat in several identical cases, but a bit more complicated

@interface HistoryModel : NSObject {
    int entry_id;
    NSString *word;
}

- (id)initWithWord:(NSString *)word;
- (id)initWithWord:(NSString *)word andId:(int)entry_id;

@property int entry_id;
@property (retain) NSString *word;

@end

HistoryModel.m

@implementation HistoryModel

@synthesize entry_id, word;

- (id)initWithWord:(NSString *)_word {
    [super init];
    word = _word;
    return self;
}

- (id)initWithWord:(NSString *)_word andId:(int)_entry_id {
    entry_id = _entry_id;
    return [self initWithWord:_word];

@end

HistoryDataController.h
i use the entity of that class as getter of data and a storage for HistoryModel objects (in historyEntries property)

@interface HistoryDataController : NSObject {
    NSMutableArray *historyEntries;
    int limit;
}

@property (nonatomic, retain) NSMutableArray *historyEntries;
@property int limit;

- (id)initWithHistoryData;
- (id)initWithHistoryDataLimitedBy:(int)limit;

HistoryDataController.m

@implementation HistoryDataController
@synthesize historyEntries, limit;

- (id)initWithHistoryDataLimitedBy:(int)_limit {
    [super init];

    // Getting data from database
    {some DB stuff}

    NSMutableArray *tmp_historyEntries = [[NSMutableArray alloc] init];
    while(result == SQLITE_ROW)
    {
        HistoryModel *currentHistoryEntry = [[HistoryModel alloc] initWithWord:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)] ];
        [tmp_historyEntries addObject:currentHistoryEntry];
        result = sqlite3_step(statement);
    }
    historyEntries = tmp_historyEntries;

    {some DB stuff}
    return self;
}
@end

HistoryViewController.h
subclass of UITableViewController, gets data stored in AppDelegate’s property and displays in the table

@interface HistoryViewController : UITableViewController {
    IBOutlet UITableView *historyTable;
    SynonymsAppDelegate *appDelegate;
}

@property (retain) UITableView *historyTable;

@end

HistoryViewController.m

@implementation HistoryViewController
@synthesize historyTable, historyEntriesToShow;

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    appDelegate = (SynonymsAppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate initHistoryList];
    [self.tableView reloadData];
}

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

    {standart cell stuff}

    HistoryModel *historyEntry = [appDelegate.historyList objectAtIndex:indexPath.row];
    cell.textLabel.text = historyEntry.word;
    return cell;
}

@end

SynonymsAppDelegate.h
when history tab opens, it gets data of historyList property, that was formed by HistoryDataController 🙂

@interface SynonymsAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
    ...
    NSMutableArray *historyList;
}
...
@property (retain) NSMutableArray *historyList;

- (void)initHistoryList;

@end

SynonymsAppDelegate.m

@implementation SynonymsAppDelegate
@synthesize window, tabBarController, historyList;

- (void)initHistoryList {
    HistoryDataController *historyDataController = [[HistoryDataController alloc] initWithHistoryData];
    historyList = historyDataController.historyEntries;
}

@end

Fuf. Sorry for so much code, but i believe that’s all necessary.
As a result of half the day spent on this question, i may guess, that problem is somehow connected with HistoryModel entity, because when i delete “retain” for word @property, the error switches for -[CFString isEqualToString:]: message sent to deallocated instance

I’m not really experienced in memory management, but i guess this HistoryModel objects inside historyEntry in HistoryViewController or in historyList in AppDelegate releases somehow, when scrolling the table or opening the tab for the second time.
But this’s just my guessing.
Really appreciate the help.

  • 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-18T01:33:19+00:00Added an answer on May 18, 2026 at 1:33 am

    You definitely have an issue in your -[HistoryModel initWithWord] You should retain (or better yet copy) the string that is being passed.

    I would write it like this:

    - (id)initWithWord:(NSString *)_word {
        [super init];
        self.word = _word; // this is same as [self setWord:_word]
        return self;
    }
    

    There are some who would say using the setter in your init is not a good practice. I’m not of that camp. But in any case, you need to be retaining or copying that string.

    Then you have a similar issue in your app delegate where you are leaking each HistoryDataController as you create a new one. (and that happens every time that tableview appears). And you really should be retaining that array as well (although that hasn’t caused a problem yet because you’re leaking the HistoryDataControllers and therefore masking that issue so far.)

    My general advice to you would be don’t put off memory management. To come back later and try to get it right is complicated and error-prone even for an experienced developer. It is much, much easier to build the correct memory management techniques into the code as you write it. This means it’s well worth your time to read the memory management guide first before you start coding something like this. You’ll save yourself a lot of time and frustration.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a

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.