I'm working on learning Objective-C, and I'm trying to get a feel for the memory management. I'm fairly familiar with C memory management, but I'm trying to figure out how different ObjC is.
Let's say I have a class called Complex
that is used for holding complex numbers, which has a method -(Complex*)add:(Complex*)c;
that adds the passed in complex number to self
(Complex
is a mutable object, let's say).
So I can call it this way:
Complex *c = [[Complex alloc] withReal: -3.0 andImag: -2.4]; // c = -3.0-2.4i
[c add : [[Complex alloc] withReal: 1.0 andImag: 2.0]]; // now c = -2.0-0.4i
What happens to the memory used for the temporary object created in the call to add
? I assume it's a memory leak; is this the correct code?
Complex *c = [[Complex alloc] withReal: -3.0 andImag: -2.4]; // c = -3.0-2.4i
Complex *b = [[Complex alloc] withReal: 1.0 andImag: 2.0]; // b = 1+2i
[c add : b]; // now c = -2.0-0.4i
[b release];
Bonus noob question: would the Objective-C 2.0 GC deal with the situation?