Joshua Hensley
Joshua Hensley

Reputation: 23

iOS ivar alloc/init assigns memory address to another ivar

I've declared two NSMutableArrays in the interface of one of my view controllers, as you can see in the code below. However, after synthesizing the properties, I attempt to initialize my class' ivars in viewDidLoad. If I step over this process in debugger, I see that it is offsetting memory allocation by one ivar, if that makes any sense. So, for example, as I am initializing the first NSMutableArray, I can watch the memory address get assigned to the other NSMutableArray ivar. The NSMutableArray that the code should be initializing remains a null memory address. This odd "offset" in memory allocations is try for initializing the second NSMutableArray too...it is initialized in my DataHelper ivar's memory location. This is causing errors to be thrown later in the code like:

-[DataHelper count]: unrecognized selector sent to instance

Where it is expecting a reference to an NSMutableArray in memory, it is instead pointing to the DataHelper memory address. Has anyone ever experienced anything like this? I'm using ARC, the latest version of Xcode, and iOS 5.1.

MyViewController.h

#import <UIKit/UIKit.h>
#import "DataHelper.h"

@interface MyViewController : UIViewController
{
    DataHelper *dataHelper;
    NSMutableArray *recentPosts;
    NSMutableArray *popularPosts;
}

@property (nonatomic, strong) DataHelper *dataHelper;
@property (nonatomic, strong) NSMutableArray *recentPosts;
@property (nonatomic, strong) NSMutableArray *popularPosts;

@end

viewDidLoad in MyViewController.m

@implementation MyViewController

@synthesize dataHelper, recentPosts, popularPosts;

- (void)viewDidLoad
{
    [super viewDidLoad];

    recentPosts = [[NSMutableArray alloc] init];
    popularPosts = [[NSMutableArray alloc] init];
    dataHelper = [[DataHelper alloc] init];

    // I've also tried initializing the ivars as above, with "self." prefix

    recentPosts = [dataHelper getSuggestionsByMostRecent:1];
    popularPosts = [dataHelper getSuggestionsByPopular:1];
}

Upvotes: 1

Views: 783

Answers (3)

Martin Ullrich
Martin Ullrich

Reputation: 100681

There are memory readout issues with LLDB in XCode (4.3.1 seems to be affected too) so i suggest you use GDB for debugging.

Upvotes: 0

ott--
ott--

Reputation: 5722

I would synthesize like this:

@synthesize dataHelper = _dataHelper;
// same for the other two here

and use _dataHelper only.

And for adding the posts, didn't you want to do this instead:

[_recentPosts addObjectsFromArray:[_dataHelper getSuggestionsByMostRecent:1]];

Upvotes: 0

Matthias Bauch
Matthias Bauch

Reputation: 90117

ARC expects your method names to follow the cocoa(-touch) naming conventions. and getSuggestionsByMostRecent violates that convention. Especially this one:

Use “get” only for methods that return objects and values indirectly. You should use this form for methods only when multiple items need to be returned.

that would be a valid get-method (not a good one though): - (void)getString:(NSString **)str

Without using correct naming scheme ARC does not know where to add retain or releases.

Try to rename your method to something like suggestionsSortedByMostRecent:. Without the get.

Upvotes: 1

Related Questions