retain

Object retain behavior of Objective-C class methods

What's the best practice for retaining and releasing objects passed to class methods? For instance, if you have a "class variable" declared like so: static NSString *_myString = nil ...is the right thing to do this: + (void)myClassMethod:(NSString *)param { _myString = param; } ... which has the drawback that the caller needs ...

Cocoa Touch table datasource problem

I'm having some trouble with displaying results from a datasource. This code will show a different (and correct) result in the console, but results in all kinds of random crap in the simulator. ("results" is an NSMutableArray property for the class.) -(void) handleSearchForKeywords: (NSString *) keywords { [results removeAllObjects...

Wanted to share an important insigt I found out today about the .self notation in ObjC

Hi, First thanks so much to users in this site for many answers I stumble upon when investigating bugs... I am working now a few months on a Obj-C project (IPhone), today I made a big discovery (in a beginners PoV) while working with a singleton. In short if you are accessing a @property with self.someProperty it will use the "(nonato...

A question about setter in objective-c

This is an example from The Objective-C 2.0 Programming Language. I was just wondering, in the setter at the bottom, can I use value = [newValue retain] instead of value = [newValue copy] ? @interface MyClass : NSObject { NSString *value; } @property(copy, readwrite) NSString *value; @end // assume using garbage collecti...

What increases an object's retain count?

Here is code I am referring to. // Person.h @interface Person : NSObject { NSString *firstName; NSString *lastName; } @end // Person.m @implementation Person - (id)init { if (![super init]) return nil; firstName = @"John"; lastName = @"Doe"; } @end // MyClass.m @implementation MyClass ..... - (NSArray *)getP...

Multithreaded iPhone app crashes with [NSAutoreleasePool release]

Hi, I have a memory management-related question in a multithreaded iPhone application. Let's say we have this method, that is called in a separate thread than the main-UI-thread: - (BOOL)fetchAtIndex:(NSUInteger)index { NSURL *theURL = [NSURL URLWithString:[queryURLs objectAtIndex:index]]; // Pay attention to this line: NSD...

NSZombies are eating my app's brain!

I've got a retain/release problem. My View is pretty complicated so I've set NSZombieEnabled to YES and am trying to locate which, exactly, object is causing me grief. To speed this process along I'm wondering if there hints or tricks to tracking the Zombies back to the grave they dug their way out of (sorry, had to) or, back to the obje...

Objective C - help with properties

I'm starting to understand memory management better in objective-c, but there's something I don't understand. This is a property declaration: @property (nonatomic, retain)UILabel *myLabel; and this is its unseen synthesized setter (I think): - (void)setMyLabel:(UILabel *)newValue { if(myLabel != newValue) { [myLabel relea...

Property attribute "retain" doesn't seem to be working?

I've implemented a bit of code from one of the many Apple code examples, but I'm having a bit of trouble, because the retain attribute of one of the properties doesn't appear to be working. Here's the property declaration: @property (nonatomic, retain) EditingViewController *editingViewController; And here's the code: - (EditingViewC...

Objective C release, autorelease, and data types

I'm new to memory managed code but I get the idea pretty well. On taking my app through the leaks tool in XCode, I noticed I only had to clean up my custom objects, but not dynamically created arrays for example, so I figured those data types are autoreleased - makes sense since I only had to release the arrays I used as properties that...

What happens if I don't retain IBOutlet?

If I do this: @interface RegisterController : UIViewController <UITextFieldDelegate> { IBOutlet UITextField *usernameField; } instead of this: @interface RegisterController : UIViewController <UITextFieldDelegate> { UITextField *usernameField; } @property (nonatomic, retain) IBOutlet UITextField *usernameField; Will somethi...

IBOutlet instances are (null) after loading from NIB

I am working on an iPhone app and am getting (null) references to IBOutlet fields in my controller. I have a UIViewController subclass that is set as the File's Owner in my XIB. I have a set of UI elements that are wired into the controller. After loading from NIB and attempting to set properties on those UI elements, I find that they ar...

Error when retaining NS(Mutable)Array in a class

Hi, I'm having a hard time working with arrays. Coming from AS2/AS3 and garbage collection is new to me ... ;) This is not the full code, just the parts that matter. .h-file @interface HelperViewController : UIViewController { NSMutableArray *pagesNumbers; } @property (nonatomic, retain) NSMutableArray *pagesNumbers; .m-file @syn...

objective c 101 (retain vs assign) NSString

A 101 question Let's say i'm making database of cars and each car object is defined as: #import <UIKit/UIKit.h> @interface Car:NSObject{ NSString *name; } @property(nonatomic, retain) NSString *name; Why is it @property(nonatomic, retain) NSString *name; and not @property(nonatomic, assign) NSString *name; I understand that A...

Can you send retain counts to NSLog to aid learning?

Just curious if there is anyway to display an objects retain count using NSLog. I just want to print them out to console to help learn how retain/release is working in some simple code? cheers -gary- ...

objective-c object not getting dealloc:ed

I've got an issue with an object not being deallocated in objective-c. I'm pretty certain this is because it is being retained somewhere, but I don't know where (checking retainCount where it ought to be 0 returns a 1). I've gone through my code many times but fail to see what's retaining it that I don't release. Might even be a bug in t...

Checking for a valid delegate object before sending it a message.

I am trying to implement the delegate Pattern in Objective-C, however I am experiencing a Bad Access exception when invoking the delegate sometimes. It seems this is caused by the delegate being released. Apple does not recommend to retain delegates. How can I check my delegate if is still valid before trying to send it a message? ...

Release a retain UIImage property loaded via imageNamed?

In my class object i've defined a (nonatomic, retain) property for UIImage. I assigned this property with an image loaded via [UIImage imageNamed:@"file.png"]; If at some point I want to reassign this property to another image, should I have to release the prior reference? I am confused because by the retain property I know i should ...

iPhone SDK: loading UITableView from SQLite - creating array from SQLite

Hi, this is a follow up forr http://stackoverflow.com/questions/1552944/iphone-sdk-loading-uitableview-from-sqlite I am planning to use following code to load SQL data to the array. Each element of the array will be a class represening each database entry: @interface Row : NSObject { int PK; NSString *desc; } @property int PK...

NSMutabledata. Why is the retain count 1 higher that I would have expected?

I am trying to floss daily, get my exercise, and make sure I maintain a balance between my retains and releases. Here is what has me puzzled. I have a ivar: NSMutabledata *_alignmentData and a synthesized property associated with it: // .h file @property (nonatomic, retain) NSMutableData *alignmentData; // .m file @synthesize align...