Skip to content

Commit

Permalink
More memory leak and crash fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmiw committed Mar 28, 2024
1 parent ee4b204 commit 9879bea
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/cohpsk_put_test_bits.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ int main(int argc, char *argv[]) {
}

coh = cohpsk_create();
assert(coh != NULL);

foct = NULL;
logframes = 0;
if (argc == 3) {
if ((foct = fopen(argv[2], "wt")) == NULL) {
cohpsk_destroy(coh);
fprintf(stderr, "Error opening output Octave file: %s: %s.\n", argv[2],
strerror(errno));
exit(1);
Expand Down Expand Up @@ -101,6 +103,7 @@ int main(int argc, char *argv[]) {
}

fclose(fin);
cohpsk_destroy(coh);
float ber = (float)nerrors / nbits;
fprintf(stderr, "BER: %4.3f Nbits: %d Nerrors: %d\n", ber, nbits, nerrors);

Expand Down
1 change: 1 addition & 0 deletions src/fmfsk.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ struct FMFSK *fmfsk_create(int Fs, int Rb) {
* Destroys an fmfsk modem and deallocates memory
*/
void fmfsk_destroy(struct FMFSK *fmfsk) {
free(fmfsk->stats);
free(fmfsk->oldsamps);
free(fmfsk);
}
Expand Down
2 changes: 2 additions & 0 deletions src/freedv_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,13 @@ void freedv_close(struct freedv *freedv) {

if (FDV_MODE_ACTIVE(FREEDV_MODE_2400A, freedv->mode) ||
FDV_MODE_ACTIVE(FREEDV_MODE_800XA, freedv->mode)) {
FREE(freedv->tx_bits);
fsk_destroy(freedv->fsk);
fvhff_destroy_deframer(freedv->deframer);
}

if (FDV_MODE_ACTIVE(FREEDV_MODE_2400B, freedv->mode)) {
FREE(freedv->tx_bits);
fmfsk_destroy(freedv->fmfsk);
fvhff_destroy_deframer(freedv->deframer);
}
Expand Down
4 changes: 2 additions & 2 deletions src/freedv_fsk.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void freedv_2400b_open(struct freedv *f) {
f->n_codec_frames = 1;
f->bits_per_codec_frame = codec2_bits_per_frame(f->codec2);
f->bits_per_modem_frame = f->bits_per_codec_frame;
int n_packed_bytes = (f->bits_per_modem_frame + 7) / 8;
int n_packed_bytes = (f->bits_per_modem_frame + 7) / 8 + 1;
f->tx_payload_bits = MALLOC(n_packed_bytes);
assert(f->tx_payload_bits != NULL);
f->rx_payload_bits = MALLOC(n_packed_bytes);
Expand Down Expand Up @@ -123,7 +123,7 @@ void freedv_800xa_open(struct freedv *f) {

f->bits_per_codec_frame = codec2_bits_per_frame(f->codec2);
f->bits_per_modem_frame = f->n_codec_frames * f->bits_per_codec_frame;
int n_packed_bytes = (f->bits_per_modem_frame + 7.0) / 8.0 + 0.5;
int n_packed_bytes = (f->bits_per_modem_frame + 7) / 8;
f->tx_payload_bits = MALLOC(n_packed_bytes);
assert(f->tx_payload_bits != NULL);
f->rx_payload_bits = MALLOC(n_packed_bytes);
Expand Down
1 change: 1 addition & 0 deletions src/freedv_vhf_framing.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ int fvhff_get_varicode_size(struct freedv_vhf_deframer *def) {
void fvhff_destroy_deframer(struct freedv_vhf_deframer *def) {
freedv_data_channel_destroy(def->fdc);
free(def->bits);
free(def->invbits);
free(def);
}

Expand Down
9 changes: 7 additions & 2 deletions unittest/tcohpsk.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ int main(int argc, char *argv[]) {
int tx_bits_log[COHPSK_BITS_PER_FRAME * FRAMES];
COMP tx_symb_log[NSYMROWPILOT * FRAMES][COHPSK_NC * COHPSK_ND];
COMP tx_fdm_frame_log[COHPSK_M * NSYMROWPILOT * FRAMES];
COMP ch_fdm_frame_log[COHPSK_M * NSYMROWPILOT * FRAMES];
COMP ch_fdm_frame_log_out[(COHPSK_M * NSYMROWPILOT + 1) * FRAMES];
COMP ch_fdm_frame_log[COHPSK_M * NSYMROWPILOT * FRAMESL];
COMP ch_fdm_frame_log_out[(COHPSK_M * NSYMROWPILOT + 1) * FRAMESL];
// COMP rx_fdm_frame_bb_log[M*NSYMROWPILOT*FRAMES];
// COMP ch_symb_log[NSYMROWPILOT*FRAMES][COHPSK_NC*COHPSK_ND];
COMP ct_symb_ff_log[NSYMROWPILOT * FRAMES][COHPSK_NC * COHPSK_ND];
Expand Down Expand Up @@ -318,6 +318,11 @@ int main(int argc, char *argv[]) {
#endif
fclose(fout);

free(coh->rx_baseband_log);
free(coh->rx_filt_log);
free(coh->ch_symb_log);
free(coh->rx_timing_log);

cohpsk_destroy(coh);

return 0;
Expand Down
3 changes: 3 additions & 0 deletions unittest/tfreedv_2400B_rawdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ int main(int argc, char **argv) {
}
}
}

freedv_close(f);

if (!frames) {
printf("Did not decode any frames successfully\n");
goto fail;
Expand Down
4 changes: 2 additions & 2 deletions unittest/tfreedv_800XA_rawdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ int main(int argc, char **argv) {
unsigned char payload_tx[7] = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde};

printf("freedv_codec_frames_from_rawdata() ");
unsigned char codec_frames[8] = {0};
unsigned char codec_frames[9] = {0};
freedv_codec_frames_from_rawdata(f, codec_frames, payload_tx);
int fails = 0;
for (i = 0; i < 8; i++) {
Expand All @@ -93,7 +93,7 @@ int main(int argc, char **argv) {
printf("Passed\n");

printf("freedv_rawdata_from_codec_frames() ");
unsigned char rawdata[7] = {0};
unsigned char rawdata[8] = {0};
freedv_rawdata_from_codec_frames(f, rawdata, payload);
fails = 0;
for (i = 0; i < 7; i++) {
Expand Down

0 comments on commit 9879bea

Please sign in to comment.