MEX crashes when called twice in succession (Same input)

2 views (last 30 days)
// This mex-file crashes when called twice. Do you see any problem?
#include <math.h>
#include <mex.h>
#include <matrix.h>
#include <stdlib.h>
void mxFree(void *ptr);
/* library of functions */
#include "convolutional.h"
/* Input Arguments */
#define INPUT prhs[0]
#define GENENCODER prhs[1]
#define CODETYPE prhs[2]
/* Output Arguments */
#define OUTPUT plhs[0]
/* main function that interfaces with MATLAB */
void mexFunction(
int nlhs,
mxArray *plhs[],
int nrhs,
const mxArray *prhs[] )
{
double *g_array;
unsigned char *input;
//double *input;
unsigned char *output_p;
mwSize DataLength, CodeLength, i, j, index;
//int subs[] = {1,1};
mwIndex *subs;
subs[0]=1; subs[1]=1;
int *g_encoder;
int nn, KK, mm, code_type, max_states;
double elm;
int *input_int, *output_int;
int *out0, *out1, *state0, *state1, *tail;
code_type = 0; /* Default:Code is RSC with terminated trellis */
/* Check if the input is of the correct type */
if ( mxIsLogical(prhs[0]) != 1)
mexErrMsgTxt("Input must be logical.");
/* Check for proper number of arguments */
if ((nrhs < 2 )||(nlhs > 1)) {
mexErrMsgTxt("Usage: [output] = ConvEncode(input, g_encoder, code_type )");
} else {
/* first input is the data word */
input =(unsigned char *) mxGetPr(INPUT);
DataLength = mxGetN(INPUT); /* number of data bits */
/* cast the input into a vector of integers */
input_int = mxCalloc( DataLength, sizeof(int) );
for (i=0;i<DataLength;i++)
input_int[i] = (int) input[i];
/* second input specifies the code polynomial */
g_array = mxGetPr(GENENCODER);
nn = mxGetM(GENENCODER);
KK = mxGetN(GENENCODER);
mm = KK - 1;
max_states = 1 << mm;
if ( nrhs == 3 ) {
/* optional third input indicates if outer is RSC, NSC or tail-biting NSC */
code_type = (int) *mxGetPr(CODETYPE);
}
/* Determine the length of the output */
if (code_type < 2)
CodeLength = nn*(DataLength+mm);
else
CodeLength = nn*DataLength;
/* Convert code polynomial to binary */
g_encoder = mxCalloc(nn, sizeof(int) );
for (i = 0;i<nn;i++) {
subs[0] = i;
for (j=0;j<KK;j++) {
subs[1] = j;
//mexPrintf("\n\nThere are %d left-hand-side argument(s).\n", subs[1]);
index = mxCalcSingleSubscript(GENENCODER, 2, subs);
elm = g_array[index];
if (elm != 0) {
g_encoder[i] = g_encoder[i] + (int) pow(2,(KK-j-1));
}
}
/* mexPrintf(" g_encoder[%d] = %o\n", i, g_encoder[i] ); */
}
}
/* create the output vector */
/* OUTPUT = mxCreateLogicalMatrix(2, CodeLength/2); */
OUTPUT = mxCreateNumericMatrix(2, CodeLength/2,mxUINT8_CLASS,mxREAL);
output_p = (unsigned char *)mxGetPr(OUTPUT);
output_int = mxCalloc( CodeLength, sizeof( int ) );
/* create appropriate transition matrices */
out0 = mxCalloc( max_states, sizeof(int) );
out1 = mxCalloc( max_states, sizeof(int) );
state0 = mxCalloc( max_states, sizeof(int) );
state1 = mxCalloc( max_states, sizeof(int) );
tail = mxCalloc( max_states, sizeof(int) );
//mexPrintf("\n\nThere are %d left-hand-side argument(s).\n", code_type);
if ( code_type ) {
nsc_transit( out0, state0, 0, g_encoder, KK, nn );
nsc_transit( out1, state1, 1, g_encoder, KK, nn );
if (code_type == 2)
tail[0] = -1;
} else {
rsc_transit( out0, state0, 0, g_encoder, KK, nn );
rsc_transit( out1, state1, 1, g_encoder, KK, nn );
rsc_tail( tail, g_encoder, max_states, mm );
}
/* Encode */
conv_encode( output_int, input_int, out0, state0, out1, state1, tail, KK, DataLength, nn );
/* cast to output */
for (i=0;i<CodeLength;i++) {
output_p[i] = (unsigned char) output_int[i];
// mexPrintf("\n\nThere are %d left-hand-side argument(s).\n", (int )output_int[i]);
}
/* To test how Matlab writes the output */
/*
output_p[0] = 1;
output_p[1] = 2;
output_p[2] = 3;
output_p[3] = 4;
output_p[4] = 5;
output_p[5] = 6;
output_p[6] = 7;
output_p[7] = 8;
output_p[8] = 9;
output_p[9] = 10;
*/
/* Clean up memory */
/*mxFree( output_int );
mxFree( input_int );
mxFree( g_encoder );
mxFree( out0 );
mxFree( out1 );
mxFree( state0 );
mxFree( state1 );
mxFree( tail );
mexErrMsgTxt("Input must be logical1."); */
return;
}
  2 Comments
Walter Roberson
Walter Roberson on 6 Jun 2019
This appears to duplicate https://www.mathworks.com/matlabcentral/answers/465756-mex-file-crashes-after-second-run-same-call
Kostas Sarrigeorgidis
Kostas Sarrigeorgidis on 6 Jun 2019
Just edited the code section to look like code.
I will remove the first one

Sign in to comment.

Answers (1)

James Tursa
James Tursa on 28 Jun 2019
Here is one cause of a crash:
mwIndex *subs;
subs[0]=1; subs[1]=1;
You are dereferencing an uninitialized pointer on that second line and attempting to write into the memory behind a garbage pointer value. Your commented out line would have been better ... just change int to mwIndex:
mwIndex subs[] = {1,1};

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!