autorelease

Dealing with objects returned from cocoa convenience methods

I'm having a lot of issues with NSDate objects being prematurely deallocated. I suspect that the issues may be related to the way that I deal with the objects returned from NSDate convenience methods. I think that my showDate property declaration in the JKShow class should be "retain", but changing it to assign or copy seems to have no e...

Autorelease iPhone

Coming up towards the end of developing an iPhone application and I'm wondering just how bad is it to use autorelease when developing for the iphone. I'm faced with some fairly random crashes and, so far, I can't pinpoint it to anything other than sloppy memory usage. As a Cocoa newbie I remember initially reading a guideline document ...

Autorelease scope

Hi, I was wondering how the autorelese works on the iPhone. I though that once you send an autorelease to an object it is guaranteed to be retained in till the end of the scope of the block the autorelease was sent. Is that correct? I was initializing a view from a NIB in the applicationDidFinishLaunching like below: (void)applica...

Why is autorelease especially dangerous/expensive for iPhone applications?

I'm looking for a primary source (or a really good explanation) to back up the claim that the use of autorelease is dangerous or overly expensive when writing software for the iPhone. Several developers make this claim, and I have even heard that Apple does not recommend it, but I have not been able to turn up any concrete sources to ba...

Autorelease and "assign" properties in Objective-C? (On iPhone)

I have an instance of a UITableView, and a separate class that adheres to the delegate and datasource protocols. I'm doing this like so: SubjectTableViewHandler *handler = [[[SubjectTableViewHandler alloc] init] retain]; tv.delegate = handler; tv.dataSource = handler; [handler autorelease]; I don't want to maintain the handler as an i...

How are objects in an autorelease pool referenced?

I am wondering if the autorelease pool holds strong or weak references to the objects it holds. I would guess they are weak. When I add an object to an autorelease pool, it's just not immediately released but will be released when the pool is drained, right? So the references should be weak, i.e. the reference count (or retain count) kee...

What's the difference between sending -release or -drain to an Autorelease Pool?

In many Books and on many Sites I see -drain. Well, for an Autorelease Pool that sounds cool. But does it do anything other than an release? I would guess -drain just makes the Pool to -release all it's objects, without releasing the Pool itself. Just a guess. ...

is the main.m really the place, where the autorelease pool of the main run loop is created by every event?

#import <UIKit/UIKit.h> int main(int argc, char *argv[]) { NSLog(@"new event..."); NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int retVal = UIApplicationMain(argc, argv, nil, nil); [pool release]; return retVal; } If that's the case, then the main() function would have to be called on every event, ...

Should I -drain or -release an autorelease pool in iPhone OS?

The doc says: In a garbage-collected environment, sending a drain message to a pool triggers garbage collection if necessary; release, however, is a no-op. In a reference-counted environment, drain has the same effect as release. Typically, therefore, you should use drain instead of release. If I get that right, they ...

Is it possible to add an object to a specific autorelease pool?

In the docs there is an addObject: method of NSAutoreleasePool. I thought about this: NSString *myString = [[NSString alloc] initWithCString:"Does this work?"]; [thePool addObject:myString]; [anotherPool addObject:myString]; Is that possible? I always read that I can only add objects to the topmost one on the autorelease pool stack. ...

Why does a Convencience Constructor or Object Factory have to care about releasing the Object?

Actually, if you use a method with "new", "create", "alloc" or "copy" in it's name, then you are responsible for releasing the object that is returned to you. BUT: Why do these methods make an call to -autorelease? Wouldn't this result in "over-releasing" the object? Lets say I get that object from such a method, and then I call -releas...

Why is there no autorelease pool when I do performSelectorInBackground: ?

I am calling a method that goes in a background thread: [self performSelectorInBackground:@selector(loadViewControllerWithIndex:) withObject:[NSNumber numberWithInt:viewControllerIndex]]; then, I have this method implementation that gets called by the selector: - (void) loadViewControllerWithIndex:(NSNumber *)indexNumberObj { NSAu...

How to find the cause of a malloc "double free" error?

Hello I'm programming an application in Objective-C and I'm getting this error: MyApp(2121,0xb0185000) malloc: *** error for object 0x1068310: double free *** set a breakpoint in malloc_error_break to debug It is happening when I release an NSAutoreleasePool and I can't figure out what object I'm releasing twice. How do I set his br...

NSAutoreleasePool carrying across methods?

I'm building an iPhone application where I detach some threads to do long-running work in the background so as not to hang the UI. I understand that threads need NSAutoreleasePool instances for memory management. What I'm not sure about is if the threaded method calls another method - does that method also need an NSAutoreleasePool? Exa...

How do I handle memory management in this situation?

I have two classes, a class that handles db connectivity and an entity class. The db class has an instance method called GetEntityByID:(int)entity_id. This method does a simple select statement, and creates an Entity class instance using an init method. This works fine, however whoever calls GetEntityByID must remember to release it...

Use autorelease before adding objects to a collection?

I have been looking through the questions asked on StackOverflow, but there are so many about memory management in Objective-C that I couldn't find the answer I was looking for. The question is if it is ok (and recommnded) to call autorelease before adding a newly created object to a collection (like NSMutableArray)? Or should I release...

where are autorelease pools required in a secondary NSThread runloop?

The run loop for the secondary thread of my application is below. It has a nested control loops. The outer loop runs for the duration of the application The inner loop runs while one view is open, then the thread waits while the view is not open. Passes through the inner loop are short, a fraction of a second. My code does not knowin...

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...

iPhone Development - Release an autoreleased object

What happens if I release an autoreleased object? Its an autoreleased UIButton I want to release and the only way to create a UIButton is to use the convinience method buttonWithType:. Will it be released from memory like a normal object? Or should I just let the autoreleasepool take care of it? I wouldn't have made it autoreleased in th...

Objective C : Releasing after removal from an array & reference pointers.

So some where i have a leak which is related to deleting an object under certain circumstances. Premise: - I have an NSMutableArray of Tree objects (a Tree object knows how to draw itself). - I have a reference pointer (Tree *selected) which basically points to whatever tree i last touched. - Note that the *selected pointer is a weak r...