Question

this is another issue I'm stuck with. First of all I'm trying to read a level-4 matlab file which contains information exported from PicoScope 6, it reads four arrays from the file, A, Tstart, Tinterval and Length. Array number one is the largest by far, it contains 1000004 values, however the other three only contains one value each. When I exectue the code below it successfully reads the file, stores it into a multidimensional array but fails when I'm trying to use the array.

#include <jni.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <matio.h>
#include "ReadMatFile.h"

//macros
#define getLength(x)  (sizeof(x) / sizeof(x[0]))

//variables
double *dataMatrix[4];
int innerSize[getLength(dataMatrix)];

//functions
jobjectArray convertToArray(JNIEnv *env, double **data, int length1D,
        int *length2D);

JNIEXPORT jdoubleArray JNICALL Java_ReadMatFile_readMatFile(JNIEnv *env,
        jobject object, jstring str) {
    const char *fileName = (*env)->GetStringUTFChars(env, str, 0);
    mat_t *matfp;
    matvar_t *matvar;
    matfp = Mat_Open(fileName, MAT_ACC_RDONLY | MAT_FT_MAT4);
    if ( NULL == matfp) {
        fprintf(stderr, "Error opening MAT file %s\n", fileName);
        return NULL;
    }
    int i = 0;
    while ( NULL != (matvar = Mat_VarReadNext(matfp))) {
        double *data = (double*) (matvar->data);
        dataMatrix[i] = data;
        innerSize[i] = (int) matvar->nbytes / matvar->data_size;
        Mat_VarFree(matvar);
        matvar = NULL;
        i++;
    }
    Mat_Close(matfp);
    (*env)->ReleaseStringUTFChars(env, str, fileName);
    int s;
    for(s = 0; s < innerSize[0]; s++)
        printf("A[%d] = %e\n", s, dataMatrix[0][s]); /* Fails here */
    return NULL;
    //return convertToArray(env, dataMatrix, getLength(dataMatrix) ,innerSize);
}

jobjectArray convertToArray(JNIEnv *env, double **data, int length1D,
        int *length2D) {
    jsize outerSize = (jsize) length1D;
    jclass class = (*env)->FindClass(env, "[D");
    jobjectArray outer = (*env)->NewObjectArray(env, outerSize, class, 0);
    jsize i;
    for (i = 0; i < outerSize; i++) {
        jsize innerSize = (jsize) length2D[i];
        jdoubleArray inner = (*env)->NewDoubleArray(env, innerSize);
        (*env)->SetDoubleArrayRegion(env, inner, 0, innerSize, data[i]);
        (*env)->SetObjectArrayElement(env, outer, i, inner);
        (*env)->DeleteLocalRef(env, inner);
    }
    return outer;
}

What is the cause of this? It creates a minidump when I run this application. Is the array too large?

A fix for this and also a explanation of what is wrong would be much appreciated!

Thanks in advance folks.

Was it helpful?

Solution

I suspect your problem is in the following code:

double *data = (double*) (matvar->data);
dataMatrix[i] = data;
innerSize[i] = (int) matvar->nbytes / matvar->data_size;
Mat_VarFree(matvar); // whoopsie

If Mat_VarFree does what I think it does, matvar->data is no longer a valid pointer, meaning dataMatrix[i] is no longer a valid pointer, hence the crash.

I think what you intend to do is something more along the lines of

innerSize[i] = matvar->nbytes / matvar->data_size;
dataMatrix[i] = malloc( sizeof *dataMatrix[i] * innerSize[i] );
if ( dataMatrix[i] )
  memcpy( dataMatrix[i], matvar->data, matvar->nbytes );
Mat_VarFree( matvar );

that is, create a local copy of the data in matvar->data and save it to your dataMatrix. In your original code, all you copied was a pointer value; you never created a separate copy of your data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top