From: Lars-Peter Clausen Date: Sun, 24 Jun 2012 10:08:59 +0000 (+0200) Subject: sr: fx2lafw: Properly free transfer X-Git-Tag: dsupstream~894 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=f855de7575915a8b3a07ed32ff7ec6b431777317;p=libsigrok.git sr: fx2lafw: Properly free transfer When freeing a transfer we also have to free the transfer buffer. We also have to keep track of the number of allocated transfers and if the freed transfer was the last one stop acquisition. This patch introduces a helper function which takes care of all of this. Signed-off-by: Lars-Peter Clausen --- diff --git a/hardware/fx2lafw/fx2lafw.c b/hardware/fx2lafw/fx2lafw.c index a1a131e5..d38a1999 100644 --- a/hardware/fx2lafw/fx2lafw.c +++ b/hardware/fx2lafw/fx2lafw.c @@ -682,6 +682,20 @@ static void finish_acquisition(struct context *ctx) free(lupfd); /* NOT g_free()! */ } +static void free_transfer(struct libusb_transfer *transfer) +{ + struct context *ctx = transfer->user_data; + + g_free(transfer->buffer); + transfer->buffer = NULL; + libusb_free_transfer(transfer); + + ctx->submitted_transfers--; + if (ctx->submitted_transfers == 0) + finish_acquisition(ctx); + +} + static void receive_transfer(struct libusb_transfer *transfer) { /* TODO: These statics have to move to the ctx struct. */ @@ -697,13 +711,7 @@ static void receive_transfer(struct libusb_transfer *transfer) * transfer that come in. */ if (ctx->num_samples == -1) { - if (transfer) - libusb_free_transfer(transfer); - - ctx->submitted_transfers--; - if (ctx->submitted_transfers == 0) - finish_acquisition(ctx); - + free_transfer(transfer); return; } @@ -718,7 +726,7 @@ static void receive_transfer(struct libusb_transfer *transfer) /* Fire off a new request. */ if (!(new_buf = g_try_malloc(4096))) { sr_err("fx2lafw: %s: new_buf malloc failed.", __func__); - libusb_free_transfer(transfer); + free_transfer(transfer); return; /* TODO: SR_ERR_MALLOC */ }