link: fix memleak

Don't leak the buffers, handle ownership of them just like we do with
the buffer memory.
This commit is contained in:
Wim Taymans 2017-08-23 09:42:09 +02:00
parent 80cf20a255
commit cc95f975ce
2 changed files with 15 additions and 21 deletions

View file

@ -590,7 +590,8 @@ static int do_allocation(struct pw_link *this, uint32_t in_state, uint32_t out_s
n_params, n_params,
params, params,
1, 1,
data_sizes, data_strides, &impl->buffer_mem); data_sizes, data_strides,
&impl->buffer_mem);
pw_log_debug("allocating %d input buffers %p %zd %zd", impl->n_buffers, pw_log_debug("allocating %d input buffers %p %zd %zd", impl->n_buffers,
impl->buffers, minsize, stride); impl->buffers, minsize, stride);
@ -1175,9 +1176,10 @@ void pw_link_destroy(struct pw_link *link)
if (link->info.format) if (link->info.format)
free(link->info.format); free(link->info.format);
if (impl->buffer_owner == link) if (impl->buffer_owner == link) {
free(impl->buffers);
pw_memblock_free(&impl->buffer_mem); pw_memblock_free(&impl->buffer_mem);
}
free(impl); free(impl);
} }

View file

@ -341,10 +341,10 @@ void pw_port_destroy(struct pw_port *port)
pw_log_debug("port %p: free", port); pw_log_debug("port %p: free", port);
spa_hook_list_call(&port->listener_list, struct pw_port_events, free); spa_hook_list_call(&port->listener_list, struct pw_port_events, free);
if (port->buffers) if (port->allocated) {
free(port->buffers); free(port->buffers);
if (port->allocated)
pw_memblock_free(&port->buffer_mem); pw_memblock_free(&port->buffer_mem);
}
if (port->properties) if (port->properties)
pw_properties_free(port->properties); pw_properties_free(port->properties);
@ -393,12 +393,12 @@ int pw_port_set_format(struct pw_port *port, uint32_t flags, const struct spa_fo
if (!SPA_RESULT_IS_ASYNC(res)) { if (!SPA_RESULT_IS_ASYNC(res)) {
if (format == NULL) { if (format == NULL) {
if (port->buffers) if (port->allocated) {
free(port->buffers); free(port->buffers);
pw_memblock_free(&port->buffer_mem);
}
port->buffers = NULL; port->buffers = NULL;
port->n_buffers = 0; port->n_buffers = 0;
if (port->allocated)
pw_memblock_free(&port->buffer_mem);
port->allocated = false; port->allocated = false;
port_update_state (port, PW_PORT_STATE_CONFIGURE); port_update_state (port, PW_PORT_STATE_CONFIGURE);
} }
@ -452,7 +452,6 @@ int pw_port_set_param(struct pw_port *port, struct spa_param *param)
int pw_port_use_buffers(struct pw_port *port, struct spa_buffer **buffers, uint32_t n_buffers) int pw_port_use_buffers(struct pw_port *port, struct spa_buffer **buffers, uint32_t n_buffers)
{ {
int res; int res;
size_t size;
if (n_buffers == 0 && port->state <= PW_PORT_STATE_READY) if (n_buffers == 0 && port->state <= PW_PORT_STATE_READY)
return SPA_RESULT_OK; return SPA_RESULT_OK;
@ -473,14 +472,12 @@ int pw_port_use_buffers(struct pw_port *port, struct spa_buffer **buffers, uint3
else else
res = SPA_RESULT_NOT_IMPLEMENTED; res = SPA_RESULT_NOT_IMPLEMENTED;
size = sizeof(struct spa_buffer *) * n_buffers; if (port->allocated) {
if (port->buffers)
free(port->buffers); free(port->buffers);
port->buffers = size ? memcpy(malloc(size), buffers, size) : NULL;
port->n_buffers = n_buffers;
if (port->allocated)
pw_memblock_free(&port->buffer_mem); pw_memblock_free(&port->buffer_mem);
}
port->buffers = buffers;
port->n_buffers = n_buffers;
port->allocated = false; port->allocated = false;
if (port->n_buffers == 0) if (port->n_buffers == 0)
@ -496,7 +493,6 @@ int pw_port_alloc_buffers(struct pw_port *port,
struct spa_buffer **buffers, uint32_t *n_buffers) struct spa_buffer **buffers, uint32_t *n_buffers)
{ {
int res; int res;
size_t size;
if (port->state < PW_PORT_STATE_READY) if (port->state < PW_PORT_STATE_READY)
return SPA_RESULT_NO_FORMAT; return SPA_RESULT_NO_FORMAT;
@ -516,11 +512,7 @@ int pw_port_alloc_buffers(struct pw_port *port,
else else
res = SPA_RESULT_NOT_IMPLEMENTED; res = SPA_RESULT_NOT_IMPLEMENTED;
size = sizeof(struct spa_buffer *) * *n_buffers; port->buffers = buffers;
if (port->buffers)
free(port->buffers);
port->buffers = size ? memcpy(malloc(size), buffers, size) : NULL;
port->n_buffers = *n_buffers; port->n_buffers = *n_buffers;
port->allocated = true; port->allocated = true;