Skip to content

Commit

Permalink
pulseaudio: fix PaPulseAudio_Initialize unlocking
Browse files Browse the repository at this point in the history
Make sure that if PaPulseAudio_Initialize fails that
a) Mainloop is unlocked if HostAPI is initialized
b) Unlocking is done before freeing
c) It's done only once

Also move allocations group freeing code to correct place
now there only one place where it's called
  • Loading branch information
illuusio committed Oct 24, 2023
1 parent 24c8d57 commit 96fa25d
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/hostapi/pulseaudio/pa_linux_pulseaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,19 @@ void PaPulseAudio_Free( PaPulseAudio_HostApiRepresentation * ptr )
ptr->timeEvent = NULL;
}


if( ptr->mainloop )
{
pa_threaded_mainloop_free( ptr->mainloop );
ptr->mainloop = NULL;
}

if( ptr->allocations )
{
PaUtil_FreeAllAllocations( ptr->allocations );
PaUtil_DestroyAllocationGroup( ptr->allocations );
ptr->allocations = NULL;
}

PaUtil_FreeMemory( ptr );
}

Expand Down Expand Up @@ -529,6 +535,7 @@ PaError PaPulseAudio_Initialize( PaUtilHostApiRepresentation ** hostApi,
int i;
int deviceCount;
int ret = 0;
int lockTaken = 0;
PaPulseAudio_HostApiRepresentation *pulseaudioHostApi = NULL;
PaDeviceInfo *deviceInfoArray = NULL;

Expand Down Expand Up @@ -561,6 +568,8 @@ PaError PaPulseAudio_Initialize( PaUtilHostApiRepresentation ** hostApi,

/* Connect to server */
PaPulseAudio_Lock( pulseaudioHostApi->mainloop );
lockTaken = 1;

ret = pa_context_connect( pulseaudioHostApi->context,
NULL,
0,
Expand All @@ -570,8 +579,7 @@ PaError PaPulseAudio_Initialize( PaUtilHostApiRepresentation ** hostApi,
{
PA_PULSEAUDIO_SET_LAST_HOST_ERROR( 0,
"PulseAudio_Initialize: Can't connect to server");
result = paNotInitialized;
PaPulseAudio_UnLock( pulseaudioHostApi->mainloop );
result = paUnanticipatedHostError;
goto error;
}

Expand All @@ -589,6 +597,7 @@ PaError PaPulseAudio_Initialize( PaUtilHostApiRepresentation ** hostApi,
break;
case PA_CONTEXT_TERMINATED:
case PA_CONTEXT_FAILED:
result = paUnanticipatedHostError;
goto error;
break;
case PA_CONTEXT_UNCONNECTED:
Expand Down Expand Up @@ -742,18 +751,18 @@ PaError PaPulseAudio_Initialize( PaUtilHostApiRepresentation ** hostApi,
PaUtil_DummyGetWriteAvailable );

PaPulseAudio_UnLock( pulseaudioHostApi->mainloop );
lockTaken = 0;
return result;

error:

if( pulseaudioHostApi )
{
if( pulseaudioHostApi->allocations )
if( lockTaken )
{
PaUtil_FreeAllAllocations( pulseaudioHostApi->allocations );
PaUtil_DestroyAllocationGroup( pulseaudioHostApi->allocations );
PaPulseAudio_UnLock( pulseaudioHostApi->mainloop );
lockTaken = 0;
}

PaPulseAudio_Free( pulseaudioHostApi );
pulseaudioHostApi = NULL;
}
Expand All @@ -767,12 +776,6 @@ void Terminate( struct PaUtilHostApiRepresentation *hostApi )
PaPulseAudio_HostApiRepresentation *pulseaudioHostApi =
(PaPulseAudio_HostApiRepresentation *) hostApi;

if( pulseaudioHostApi->allocations )
{
PaUtil_FreeAllAllocations( pulseaudioHostApi->allocations );
PaUtil_DestroyAllocationGroup( pulseaudioHostApi->allocations );
}

PaPulseAudio_Lock( pulseaudioHostApi->mainloop );
pa_context_disconnect( pulseaudioHostApi->context );
PaPulseAudio_UnLock( pulseaudioHostApi->mainloop );
Expand Down

0 comments on commit 96fa25d

Please sign in to comment.