Re: Passing strings to threads and memory leaks
Re: Passing strings to threads and memory leaks
- Subject: Re: Passing strings to threads and memory leaks
- From: Pontus Ilbring <email@hidden>
- Date: Fri, 7 May 2004 21:04:09 +0200
On 2004-05-07, at 20.05, Chuck Rice wrote:
Thanks! But I guess I still do not understand. If I release theData at
the end of the method, when the other thread executes, that storage
will be gone, won't it?
That is the advanced reading, which I might as well try to explain.
When you call release, that doesn't necessarily mean that an object
disappears from memory.
Each object has a retain count, a number describing how many others are
interested in it. When you call retain, you signal that you are
interested in the object, and its retain count is increased by one.
When you call release, you signal that you are no longer interested in
the object, and its retain count is decreased. Should the objects
retain count reach zero that means no one is interested in it anymore,
and it will be deallocated.
When you create an object using alloc or copy its retain count starts
out at 1.
In the modified code I showed you, theData starts out with a retain
count of 1, because you created it with alloc. When you call retain
before storing it away the retain count becomes 2, and when you call
release at the end of the method its retain count becomes 1 again. In
the next iteration, when you release temp, its retain count falls to 0
and it'll be deallocated. In this case, you could avoid calling retain
and release for theData altogether -- its retain count starts out at 1
and would just stay at 1 until you release temp it in the next
iteration.
Coding the way I suggested merely saves you from even having to think
about the retain count.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.