Avoid double-constructing owned_critical_section via copy constructor.

This commit is contained in:
Cervantes Yu 2016-10-11 10:44:35 +08:00 коммит произвёл Matthew Gregan
Родитель ec77543692
Коммит 22557d466e
4 изменённых файлов: 23 добавлений и 10 удалений

Просмотреть файл

@ -423,14 +423,13 @@ audiounit_init(cubeb ** context, char const * context_name)
*context = NULL;
ctx = new cubeb;
ctx = (cubeb *)calloc(1, sizeof(cubeb));
assert(ctx);
PodZero(ctx, 1);
// Placement new to call the ctors of cubeb members.
new (ctx) cubeb();
ctx->ops = &audiounit_ops;
ctx->mutex = owned_critical_section();
ctx->active_streams = 0;
ctx->limit_streams = kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber10_7;
@ -833,7 +832,8 @@ audiounit_destroy(cubeb * ctx)
audiounit_remove_device_listener(ctx);
}
delete ctx;
ctx->~cubeb();
free(ctx);
}
static void audiounit_stream_destroy(cubeb_stream * stm);
@ -1117,9 +1117,10 @@ audiounit_stream_init(cubeb * context,
}
}
stm = new cubeb_stream;
stm = (cubeb_stream *) calloc(1, sizeof(cubeb_stream));
assert(stm);
PodZero(stm, 1);
// Placement new to call the ctors of cubeb_stream members.
new (stm) cubeb_stream();
/* These could be different in the future if we have both
* full-duplex stream and different devices for input vs output. */
@ -1130,7 +1131,6 @@ audiounit_stream_init(cubeb * context,
stm->state_callback = state_callback;
stm->user_ptr = user_ptr;
stm->device_changed_callback = NULL;
stm->mutex = owned_critical_section();
/* Init data members where necessary */
stm->hw_latency_frames = UINT64_MAX;
@ -1443,7 +1443,8 @@ audiounit_stream_destroy(cubeb_stream * stm)
stm->context->active_streams -= 1;
}
delete stm;
stm->~cubeb_stream();
free(stm);
}
static int

Просмотреть файл

@ -80,6 +80,10 @@ public:
private:
pthread_mutex_t mutex;
// Disallow copy and assignment because pthread_mutex_t cannot be copied.
owned_critical_section(const owned_critical_section&);
owned_critical_section& operator=(const owned_critical_section&);
};
#endif /* CUBEB_UTILS_UNIX */

Просмотреть файл

@ -62,6 +62,10 @@ private:
#ifdef DEBUG
DWORD owner;
#endif
// Disallow copy and assignment because CRICICAL_SECTION cannot be copied.
owned_critical_section(const owned_critical_section&);
owned_critical_section& operator=(const owned_critical_section&);
};
#endif /* CUBEB_UTILS_WIN */

Просмотреть файл

@ -1637,7 +1637,8 @@ wasapi_stream_init(cubeb * context, cubeb_stream ** stream,
stm->latency = latency_frames;
stm->volume = 1.0;
stm->stream_reset_lock = owned_critical_section();
// Placement new to call ctor.
new (&stm->stream_reset_lock) owned_critical_section();
stm->reconfigure_event = CreateEvent(NULL, 0, 0, NULL);
if (!stm->reconfigure_event) {
@ -1734,6 +1735,9 @@ void wasapi_stream_destroy(cubeb_stream * stm)
close_wasapi_stream(stm);
}
// Need to call dtor to free the resource in owned_critical_section.
stm->stream_reset_lock.~owned_critical_section();
free(stm);
}