Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZMicrophone fixes: memory leaks and crash after de-allocation #51

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion EZAudio/EZAudioFile.m
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ -(void)_configureAudioFile {
_floatBuffers[i] = (float*)malloc(outputBufferSize);
}

[EZAudio printASBD:_fileFormat];
// [EZAudio printASBD:_fileFormat];

// There's no waveform data yet
_waveformData = NULL;
Expand Down
22 changes: 19 additions & 3 deletions EZAudio/EZAudioPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ -(void)stop {
[_output stopPlayback];
[_audioFile seekToFrame:0];
_eof = NO;

if( self.audioPlayerDelegate ){
if( [self.audioPlayerDelegate respondsToSelector:@selector(audioPlayer:didPausePlaybackOnAudioFile:)] ){
// Notify the delegate we're pausing playback
[self.audioPlayerDelegate audioPlayer:self didPausePlaybackOnAudioFile:_audioFile];
}
}
}
}

Expand Down Expand Up @@ -286,10 +293,19 @@ -(void) output:(EZOutput *)output
audioBufferList:audioBufferList
bufferSize:&bufferSize
eof:&_eof];
if( _eof && self.shouldLoop )
{
[self seekToFrame:0];
if(_eof ){

//Notifies delegate about _eof
if(self.audioPlayerDelegate)
if ([self.audioPlayerDelegate respondsToSelector:@selector(audioPlayer:reachedEndOfAudioFile:)])
[self.audioPlayerDelegate audioPlayer:self reachedEndOfAudioFile:self.audioFile];

//loops the playback if needed
if(self.shouldLoop )
[self seekToFrame:0];
}


}
}

Expand Down
7 changes: 4 additions & 3 deletions EZAudio/EZAudioPlot.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ @interface EZAudioPlot () {
@end

@implementation EZAudioPlot
@synthesize backgroundColor = _backgroundColor;
//@synthesize backgroundColor = _backgroundColor;
@synthesize color = _color;
@synthesize gain = _gain;
@synthesize plotType = _plotType;
Expand Down Expand Up @@ -96,8 +96,9 @@ -(void)initPlot {

#pragma mark - Setters
-(void)setBackgroundColor:(id)backgroundColor {
_backgroundColor = backgroundColor;
[self _refreshDisplay];

[super setBackgroundColor:backgroundColor];
[self _refreshDisplay];
}

-(void)setColor:(id)color {
Expand Down
6 changes: 4 additions & 2 deletions EZAudio/EZAudioPlotGL.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ @implementation EZAudioPlotGL
#elif TARGET_OS_MAC
@synthesize baseEffect = _baseEffect;
#endif
@synthesize backgroundColor = _backgroundColor;
//@synthesize backgroundColor = _backgroundColor;
@synthesize color = _color;
@synthesize gain = _gain;
@synthesize plotType = _plotType;
Expand Down Expand Up @@ -137,7 +137,9 @@ -(void)initializeView {

#pragma mark - Setters
-(void)setBackgroundColor:(id)backgroundColor {
_backgroundColor = backgroundColor;

[super setBackgroundColor:backgroundColor];

#if TARGET_OS_IPHONE
self.glViewController.backgroundColor = backgroundColor;
#elif TARGET_OS_MAC
Expand Down
30 changes: 30 additions & 0 deletions EZAudio/EZMicrophone.m
Original file line number Diff line number Diff line change
Expand Up @@ -596,4 +596,34 @@ -(void)_disableCallbackBufferAllocation {
operation:"Could not disable audio unit allocating its own buffers"];
}

#pragma mark De-allocation

-(void) dealloc{

if(_isConfigured){

//stops fetching if it it is ongoing
if(_isFetching)
[self stopFetchingAudio];

//inputCallback shall be unregistered before de-allocation.
// Otherwise BAD_ACCESS within inputCallback happens while attempting to access _microphone through (__bridge EZMicrophone*)inRefCon
[EZAudio checkResult:
AudioUnitRemoveRenderNotify (microphoneInput, inputCallback, (__bridge void *)self)
operation:"Couldn't unregister input callback"];

//free microphoneInputBuffer to avoid memory leaks
[EZAudio freeBufferList:microphoneInputBuffer];

//free float buffers allocated in _configureFloatConverterWithFrameSize
for ( int i=0; i<streamFormat.mChannelsPerFrame; i++ )
free(floatBuffers[i]);
free(floatBuffers);

//dicsonnects from delegate
self.microphoneDelegate=nil;

}
}

@end
2 changes: 1 addition & 1 deletion EZAudio/EZPlot.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ typedef NS_ENUM(NSInteger,EZPlotType){
/**
The default background color of the plot. For iOS the color is specified as a UIColor while for OSX the color is an NSColor. The default value on both platforms is black.
*/
@property (nonatomic,strong) id backgroundColor;
//@property (nonatomic,copy) id backgroundColor;

/**
The default color of the plot's data (i.e. waveform, y-axis values). For iOS the color is specified as a UIColor while for OSX the color is an NSColor. The default value on both platforms is red.
Expand Down