Re: Implementing a queue between an application and a thread. Deeply confused...
Re: Implementing a queue between an application and a thread. Deeply confused...
- Subject: Re: Implementing a queue between an application and a thread. Deeply confused...
- From: John Stiles <email@hidden>
- Date: Tue, 1 Nov 2005 10:31:43 -0800
You are responsible for allocating a NSAutoreleasePool on your Thread
1. Look at the NSAutoreleasePool docs; it's trivial to set one up.
Also, if you are mixing and matching APIs, one gotcha to be aware of:
you need to spawn the thread at least one thread with NSThread (as
opposed to the pthread API or MPTask API); this enables Cocoa's
"threaded" mode. If you don't do it this way, the docs claim that you
will run into stability issues. (I don't think they explicitly
document what is broken in non-threaded mode.)
On Nov 1, 2005, at 9:33 AM, Brian O'Brien wrote:
I've written a function that receives images over a network.
The images are received and placed into an NSMutableArray.
There are two threads here thread 0 the main thread and thread 1
the worker thread that handled the network.
When all the images are received a method in thread 0, is called by
thread 1 to inform thread 0 that the images were received.
Thread 0, then asks how many images there are and then gets them
one at a time.
So in thread 1s init the mutable array is created.
dataSets = [[NSMutableArray alloc] initWithCapacity:256];
Although it is possible that many more than 256 images can be
received I believe I can
still add images to dataSets and it will grow and it can also
shrink to 0 elements...
Then when all the images are received (They are in a C++
std::list) they are removed
from the C++ std::list by thread 1 and placed into the
NSMutableArray...
for (im_it=Ser->begin(); im_it != Ser->end(); ++im_it)
{
img = [self buildImage:&*im_it];
[dataSets addObject:img];
[img release];
}
Thread 0 is then notified by thread 1, that a group of images has
arrived
then thread 0 gets the number of images and then each image...
- (int) getSeriesSize
{
int n;
n = [dataSets count];
return n;
}
then this call is made to get each image one at a time
- (id) getNextImage
{
id x;
x = [dataSets objectAtIndex:0]; // retain?
[dataSets removeObjectAtIndex:0];
return x; // is this going to be valid...
}
Ok so this is leaking like a siv.... I'm not sure if an
autorelease pool can be assumed to exist because I am a thread.
Its seems reasonable to assume that the creator of the the thread
has one...
My thought was that the creator of the thread would make a (DEEP?)
copy of the image and assume ownership of the
image and allow me (the thread) to release my instances when ever
I'm ready.
So thread 0s method is called by thread 1, it then copies all the
images into it memory space:
- (void) seriesReceivedFromSender:(id *)sender)
{
int n;
n = [sender getSeriesSize];
for (int i=0; i < n; ++i)
{
id *img = [sender getNextImage]; // Make a copy?
[myDataSets addObject:img]; // Release img?
}
}
Maybe I need to change my protocol so that thread 0 can tell thread
1 that is has a copy of the image and thread 1 can
then release its copy.
Any help you can offer is great... I'm sorta new to the memory
contract on Cocoa and a bit lost.
I've read the Object Ownership and Disposal
docs..._______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40blizzard.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden