autorelease

Autorelease NSString

Am I responsible for releasing this string, and is it autorelease by default? // command is of type NSData* char cAddress[12]; [command getBytes:cAddress range:NSMakeRange(5,12)]; NSString *someString = [NSString stringWithCharacters:(const unichar*)cAddress length:12]; ...

Objective-C appropriate use of autorelease?

I'm learning Objective-C and I'm trying to write a Vector3D class. In C++ or Java the function I want to implement would look something like this: Vector3D { Vector3D crossProduct(Vector3D in) { Vector3D result; result = ... // maths involving this classes variables and the input Vectors variables. return result; } ...

Best way to periodically drain the autorelease pool on a long-running background thread?

In the developer documentation, it says: If your application or thread is long-lived and potentially generates a lot of autoreleased objects, you should periodically drain and create autorelease pools (like the Application Kit does on the main thread); otherwise, autoreleased objects accumulate and your memory footprint grows. If, howe...

Using SDL in OS X from C++ without SDLmain

How can I call SDL functions from C++ in OS X without a main method? I'm building a dynamic library that does the initialization. The application that calls it depends only on the library. I realize that I need to somehow create an autorelease pool in Objective-C, because SDL_Init() crashes as it's now, but I don't know how. SDLmain cre...

why does it works fine without 'retain' the object?

Here i used auto-release for 'tempString' in the method 'test'. According to the rule, i should use "[temp retain]" in the main . But i didnt use it. still it works fine and prints the output. Then what is the need of "retain"? Can anyone pls tell me the reason? Thanks in advance. -(NSMutableString *) test : (NSMutableString *) aString...

EXC_BAD_ACCESS in iPhone app - memory management issue

For reference, I've already read: http://stackoverflow.com/questions/2981374/memory-management-question-releasing-an-object-which-has-to-be-returned http://stackoverflow.com/questions/3835464/iphone-return-nsmutablearray-in-method-while-still-releasing http://stackoverflow.com/questions/1250651/releasing-objects-returned-by-method-in-o...

Reducing the memory footprint of a function with a lot of autoreleased variables?

Hi, I'm still wrapping my head around some of the nuances of memory management in objective-C, and came up with the following case I'm unsure about: + (NSDecimalNumber*)factorial: (NSDecimalNumber *)l { NSDecimalNumber *index = l; NSDecimalNumber *running = [NSDecimalNumber one]; for (; [index intValue] > 1; index = [inde...

iPhone NSMutableArray and NSKeyedUnarchiver unarchiveObjectWithFile release oddity

I archive an array (NSMutableArray) of custom objects that implement the . Once i load it froma file to a retaining property @property (nonatomic, retain) NSMutableArray *buddies; the release count of the object is 2 (correct, it's 1of autorelease + 1 of retain of the property) but then noone releases it and the retain count becames 1, ...

Objective-C initialize (static method) called more that once?

I have code similar to this in Objective-C: SubclassOfNSObject *GlobalVariableThatShouldNeverChange; @implementation MyClass +(void) initialize { [super initialize]; GlobalVariableThatShouldNeverChange = [[SubclassOfNSObject alloc] init]; // Change more stuff with GlobalVariableThatShouldNeverChange } @end I have this r...

iPhone - substringToIndex / substringFromIndex / substringWithRange memory leak

Instruments leaks says that this code leaks: NSString *name = [file substringToIndex:i]; Layer *actualLayer = nil; for (Layer *lay in layers) { if ([lay.layerName isEqual:name]) { actualLayer = lay; } } name is the leaking object. There are some strange things: it only leaks sometimes, not always (this snippet of code ...

iPhone: memory leak on autoreleased object?

Dear all, I am using the XMLParser class, which contains an array with XMLElement objects. The XMLElement is being allocated using the autorelease operation. However, for some reason I'm getting a memory leak (using instruments) on this line: self.currentElement = [[[XMLElement alloc] init] autorelease]; I'm also releasing the XMLPar...

Objective C, Memory Management

1) What is the reason for the use of retain? For example, in a setter method: - (void) setCount: (int) input { [intCount autorelease]; intCount = [input retain]; } 2) the autorelease-Method: Is it deleting an old object or preparing the new one? 3) Why the retain-method is called at the input-object? Would intCount = inpu...

Autorelease object returned from NSArray?

I'm writing an NSArray category to include the -objectAtRandom message that returns an object from a random index (something similar to Python's choice). Should I autorelease this object before returning it? I believe I shouldn't, but I'm not sure... ...

Autorelease pools in appkit applications

I'm having difficulties to understand exactly WHEN autorelease pools are created and released in AppKit apps. For example, if I have an ApplicationController class that overrides init, is there an autorelease pool that gets created before it starts and gets drained after it ends? ...

When using autorelease, when is it actually released?

Sometimes I wonder when something gets autoreleased. I added an NSLog in the dealloc of various objects, but I couldn't find anything useful. When does something release when autorelease is used? Is it unpredictable, or is there some extra thread running? Thanks. ...