Skip to content

Commit

Permalink
Fix problem with releasing from non-existent inputQueue[] arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
h4yn0nnym0u5e committed Feb 6, 2022
1 parent 27b4040 commit 7f24cff
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
17 changes: 13 additions & 4 deletions teensy3/AudioStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,11 @@ int AudioConnection::connect(AudioStream &source, unsigned char sourceOutput,
}


int AudioConnection::disconnect(void)
// Discconect an AudioConnection from its soource and destination(s).
// This can happen on-demand from the program, or as a result of e.g. one of
// src and dst being deleted. If the latter then the inputQueue is no longer valid,
// as it's part of the derived class and has already disappeared.
int AudioConnection::disconnect(bool inputQueueValid /* = true */)
{
AudioConnection *p;

Expand Down Expand Up @@ -696,7 +700,7 @@ int AudioConnection::disconnect(void)
//Remove possible pending src block from destination
if (NULL != dst)
{
if (NULL != dst->inputQueue)
if (inputQueueValid && NULL != dst->inputQueue)
{
AudioStream::release(dst->inputQueue[dest_index],false); // release() does NULL pointer check
dst->inputQueue[dest_index] = NULL;
Expand Down Expand Up @@ -951,8 +955,8 @@ SFSH();

if (pC->dst == this)
{
pC->disconnect(); // disconnect this connection
pC->dst = NULL; // can never re-connect, source will no longer exist
pC->disconnect(false); // disconnect this connection: inputQueue is NOT valid
pC->dst = NULL; // can never re-connect, destination will no longer exist
SPTF("%08X disconnect+NULL (%08X, %08X) ",(uint32_t) pC,(uint32_t) pC->src,(uint32_t) pC->dst);
//SPRT("=dst ");
SFSH();
Expand All @@ -979,6 +983,11 @@ SFSH();


// Destructor: quite a lot of housekeeping to do
// Note that at this point our derived object is invalid, so we have to be very careful
// not to refer to anything it provided. In particular, the inputQueue[] array has
// gone, so when we disconnect any AudioConnection objects they MUST NOT try to
// release audio blocks. This should have been done already using a SAFE_RELEASE()
// macro, in any case.
AudioStream::~AudioStream()
{
AudioStream** ppS; // iterating pointer
Expand Down
2 changes: 1 addition & 1 deletion teensy3/AudioStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class AudioConnection
AudioStream &destination, unsigned char destinationInput);
friend class AudioStream;
~AudioConnection();
int disconnect(void);
int disconnect(bool inputQueueValid = true);
int connect(void);
int connect(AudioStream &source, AudioStream &destination) {return connect(source,0,destination,0);};
int connect(AudioStream &source, unsigned char sourceOutput,
Expand Down
17 changes: 13 additions & 4 deletions teensy4/AudioStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,11 @@ int AudioConnection::connect(AudioStream &source, unsigned char sourceOutput,
}


int AudioConnection::disconnect(void)
// Discconect an AudioConnection from its soource and destination(s).
// This can happen on-demand from the program, or as a result of e.g. one of
// src and dst being deleted. If the latter then the inputQueue is no longer valid,
// as it's part of the derived class and has already disappeared.
int AudioConnection::disconnect(bool inputQueueValid /* = true */)
{
AudioConnection *p;

Expand Down Expand Up @@ -689,7 +693,7 @@ int AudioConnection::disconnect(void)
//Remove possible pending src block from destination
if (NULL != dst)
{
if (NULL != dst->inputQueue)
if (inputQueueValid && NULL != dst->inputQueue)
{
AudioStream::release(dst->inputQueue[dest_index],false); // release() does NULL pointer check
dst->inputQueue[dest_index] = NULL;
Expand Down Expand Up @@ -944,8 +948,8 @@ SFSH();

if (pC->dst == this)
{
pC->disconnect(); // disconnect this connection
pC->dst = NULL; // can never re-connect, source will no longer exist
pC->disconnect(false); // disconnect this connection: inputQueue is NOT valid
pC->dst = NULL; // can never re-connect, destination will no longer exist
SPTF("%08X disconnect+NULL (%08X, %08X) ",(uint32_t) pC,(uint32_t) pC->src,(uint32_t) pC->dst);
//SPRT("=dst ");
SFSH();
Expand All @@ -972,6 +976,11 @@ SFSH();


// Destructor: quite a lot of housekeeping to do
// Note that at this point our derived object is invalid, so we have to be very careful
// not to refer to anything it provided. In particular, the inputQueue[] array has
// gone, so when we disconnect any AudioConnection objects they MUST NOT try to
// release audio blocks. This should have been done already using a SAFE_RELEASE()
// macro, in any case.
AudioStream::~AudioStream()
{
AudioStream** ppS; // iterating pointer
Expand Down
2 changes: 1 addition & 1 deletion teensy4/AudioStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class AudioConnection
AudioStream &destination, unsigned char destinationInput);
friend class AudioStream;
~AudioConnection();
int disconnect(void);
int disconnect(bool inputQueueValid = true);
int connect(void);
int connect(AudioStream &source, AudioStream &destination) {return connect(source,0,destination,0);};
int connect(AudioStream &source, unsigned char sourceOutput,
Expand Down

0 comments on commit 7f24cff

Please sign in to comment.