NSThread Subclassing problem for Singleton instance
NSThread Subclassing problem for Singleton instance
- Subject: NSThread Subclassing problem for Singleton instance
- From: Abhinav Tyagi <email@hidden>
- Date: Tue, 13 Jul 2010 01:03:15 +0530
Hi,
Thanks for giving your valuable time to this post.
I have been working on Mac platform since last 5 months prior to which I have
worked on Windows platform.
I have used threads using NSThread's
detachNewThreadSelector:@selector() earlier
and they worked fine. However this time my thread is not like a simple
function that will
quit after it is finished. But my thread needs to be running as teh
data continuously comes
from the URL connection.
My aim was to implement InternetRadio Interface which can
*** connect to an internetradio station specified by stream URL
*** and can transcode the incoming data to WAVE.
As I am working on OSX 10.5 and 10.6, I have derived an interface Internetradio
from NSThread class. To avoid interference with main thread, I have derieved a
class from NSthread so that whole Internetradio functionality work in
separate thread
and doesnt interfere with main thread. As only one radio station is
required to be played
and I should be able to access the radio station details from any
thread for accessing members,
I made the class as singleton.
For Downloading of data from the URL, I am using NSURLConnection which
is a member
of the interface.
Everything works fine but I have noticed a significant problem with my app.
If the main thread calls stopDownload (instance method)
method the internetRadio thread doesnt seems to quit.
The application can be thought of a simple Cocoa App with a single window.
This window have a NATextField and a Button.
We type the URL and click the button.
On clicking the button, the existing station playback or transcoding is stopped
by calling stop download and then it is started again. The starting
and stopping is
done by following mechanism:
####Starting the thread:
InternetRadio * pIRadio = [InternetRadio GetInstance]; // Singleton
class method
if( pIRadio == nil) // this means the thread is either not been
started or was stopped earlier.
pIRadio = [Internetradio pInternetradio]; // calls alloc and init
[pIRadio start];
####Stopping the Thread:
Internetradio *pIRadio = [InternetRadio GetInstance];
[pIradio stopDownload]; // this method will release all the
memoryallocated and cancels the connection
Every time I click the button to change the radio station, the
Activity Monitor shows thread count which is 1 more than
that was before clicking button.
Although the performance of the app is fine but this behavior is
strange. The documenatation says thread causes
allocation of space due to data structures allocated in kernel as well
as application. Can this be because of similar
reason? And why is the thread count increasing?
Any sample app anybody can suggest please.
Abhi
CODE:
#import "AudioToolbox/AudioToolbox.h"
#import "InternetRadio.h" // interface definition.
details included in code where member variables are used.
BOOL g_bShouldKeepRunning = FALSE;
@implementation InternetRadio // class for implementing internet
radio functionality; the interface definition contains some data for
managing the internet radio connection, url etc.
static InternetRadio * pInternetRadio = nil; // singleton instance
-(id) init
{
if( !pInternetRadio) {
pInternetRadio = [super init];
return self;
}
}
//********************
-(id) pInternetRadio
{
@synchronized(self)
{
if( pInternetRadio == nil)
pInternetRadio = [[self alloc] init];
}
return pInternetRadio;
}
//********************
+(void) pInternetRadioSetAsNil
{
pInternetRadio = nil;
}
+(id) GetInstance
{
return pInternetRadio;
}
//********************
-(void) initDownload
{
... some initializations here
[self startDownload];
AudioFileStreamOpen(self, IRPropCallback, IRDataCallback,
kAudioFileAAC_ADTSType, &radioStream);// radiostream is a member
variable of type AudioFileStreamID
}
//********************
-(void) connection: (NSURLConnection*)connection didreceiveData:(NSData*) data
{
if( AudioFileStreamParseBytes(radioStream, [data length], [data
bytes], 0) != noErr)
NSLog(@"AudioStream Parser failed!");
[self stopDownload];
}
//********************
-(void) connection:(NSURLConnection*) connection
didReceiveresponse:(NSURLResponse*) reponse
{
// some metadata etc response code
}
-(void) connection: (NSURLConnection*) connection
didFailWithError:(NSError*) error
{
// some error handling code folowed by [self stopDownload];
}
//********************
-(void) startDownload:(id)inArgument
{
NSAutoreleasePool *pool = [[NSautoreleasePool alloc] init];
NSURLRequest * theRequest = [NSURLRequest requestWithURL:radioURL/*
NSURL type member variable*/,
cachePolicy:NSURLRequestUseProtocolcachePolicy timeoutInterval:10];
theConnection/*class member*/ = [[NSURLConnection alloc]
initWithRequest: therequest delegate:self];
// some other initialisation after succesful creation of connection
[pool release];
}
//********************
-(void) stopDownload
{
g_bShouldKeepRunning = NO;
AudioFileStreamClose(radioStream);
if( theConnection && isValidURL == TRUE)
{
[theConnection release];
theConnection = nil;
}
[self pInternetRadioSetAsNil];
}
//********************
-(void) main
{
[self initDownload];
if(g_bShouldKeepRunning)
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantFuture]];
}
@ end
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden