Does anyone know why xCode's "build and analyze" would report this line as a "possible memory leak"?
goodSound = [[SoundClass alloc] initializeSound:@"Good.wav"];
///// Here are the 4 files in question:
// Sounds.h
#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>
@interface SoundClass : NSObject
{
SystemSoundID soundHandle;
}
-(id) initializeSound:(NSString *)soundFileName;
-(void) play;
@end
/////////////
// Sounds.m
#import "Sounds.h"
@implementation SoundClass
-(id) initializeSound:(NSString *)soundFileName
{
self = [super init];
NSString *const resourceDir = [[NSBundle mainBundle] resourcePath];
NSString *const fullPath = [resourceDir stringByAppendingPathComponent:soundFileName];
NSURL *const url = [NSURL fileURLWithPath:fullPath];
OSStatus errCode = AudioServicesCreateSystemSoundID((CFURLRef) url, &soundHandle);
if(errCode == 0)
NSLog(@"Loaded sound: %@", soundFileName);
else
NSLog(@"Failed to load sound: %@", soundFileName);
return self;
}
//////////////////////////////
-(void) play
{
AudioServicesPlaySystemSound(soundHandle);
}
/////////////////////////////
-(void) dealloc
{
AudioServicesDisposeSystemSoundID(soundHandle);
[super dealloc];
}
/////////////////////////////
@end
//////////////
// MemTestViewController.h
#import <UIKit/UIKit.h>
@class SoundClass;
@interface MemTestViewController : UIViewController
{
SoundClass *goodSound;
}
-(IBAction) beepButtonClicked:(id)sender;
@end
///////////
// MemTestViewController.m
#import "MemTestViewController.h"
#import "Sounds.h"
@implementation MemTestViewController
- (void)viewDidLoad
{
NSLog(@"view did load: alloc'ing mem for sound class");
// "build and analyze" says this is possibly a memory leak:
goodSound = [[SoundClass alloc] initializeSound:@"Good.wav"];
[super viewDidLoad];
}
-(IBAction) beepButtonClicked:(id)sender
{
NSLog(@"beep button clicked");
[goodSound play];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)dealloc
{
[goodSound release];
[super dealloc];
}
@end