Reputation: 4465
I read that when using ARC in Objective-C programming in Xcode the dealloc
method is called automatically by the compiler. Under what circumstances is it called?
In order to avoid having too many variable names, when I need to repeatedly use the same classes to do multiple operations (and resetting the variable each time) I often declare the variables, set them to nil
, and then assign values to them as I go. This ends up looking like this:
MyClass mc;
mc = [[MyClass alloc] init];
[mc doThis:someOption]
mc = [[MyClass alloc] init];
[mc doThis:someOtherOption];
//etc...
The method name alloc
is short for "allocate" because it is the method where memory is allocated to the variable. Does the compiler automatically release the memory for sc
every time I assign it a new value? I plan on using this method in a project of mine, and I don't want a lot of memory being allocated with all the times I call alloc
to assign a new value to mc
.
Upvotes: 0
Views: 786
Reputation: 8200
Under ARC, your variable mc
will hold a strong reference to only one instance of MyClass
at a time, so when you allocate the second one and assign it to the variable, the first one should be getting deallocated, assuming your doThis:
method doesn't do something that will create another strong reference to that instance, or that you're not doing anything else in your code that you've omitted that will keep a strong reference.
That being said, it would be a good idea for you to run your app with Instruments to see how much memory your app uses during this. Your instances shouldn't be getting autoreleased, so you shouldn't have to worry about them remaining around until the autorelease pool is drained, but I don't know what you might being doing when you init an instance of the class, or what you might be doing in your doThis:
method, so if you're concerned, it's always a good idea to profile it with Instruments for memory allocations and leaks.
Upvotes: 0
Reputation: 124997
I read that when using ARC in Objective-C programming in Xcode the dealloc method is called automatically
In Objective-C, you never call -dealloc
directly whether or not you're using ARC.
Under what circumstances is it called?
-dealloc
is called when an object's retain count drops to zero. That is, it's called when all the objects that had previously asserted "ownership" of the object (by calling +alloc
or -retain
or -copy
or +new
) have renounced that ownership (by calling -release
or -autorelease
).
Does the compiler automatically release the memory for sc every time I assign it a new value?
If you're using ARC (and you should be), the compiler will insert appropriate calls to -retain
, -release
, etc. so that memory is managed appropriately. That said, you still need to understand how memory management works, and you should be familiar with the material in Advanced Memory Management Programming Guide.
Upvotes: 0
Reputation: 162712
The compiler never calls dealloc
. The compiler inserts retain
, release
and autorelease
(more efficient equivalents, really) as necessary to follow the memory management rules.
When the compiler inserts said calls is up to the compiler and the details will change across different compiler versions and different optimization levels.
I.e. you shouldn't need to worry about it in general.
However, autorelease pressure can still be an issue, as can retain cycles. Thus, you should definitely poke about your app with the Allocations Instrument to both measure the high-water mark and make sure your app isn't leaking memory over time.
Upvotes: 5