Hello!
I'm rather new to LabView. Currently I try to write a
DLL which receives a Array Handle from LabView.
My Problem is, that the DLL crashes after it has worked properly for some minutes.
The DLL is called from LabView in a loop about once a second. Sometimes it runs up to 15min, sometimes it doesn't crash at all, sometimes it crashes after 5 seconds.
It tried to comment almost all parts of the code to find out which part causes the crash, but without success.
Maybe some can see where I have made a basic mistake.
I used MS Visual C++ 6.0.
I'm using classes in the code. Might this be a problem.
Actually I don't think so because if I don't do so, the same problem occurs.
Or might there be a problem with the writing to a file. I found this to be a good way to Debug the DLL (cause printf() can't be used within the DLL.
This is the code:
#include "extcode.h"
#include
#include
#include
#include
/* Typedefs */
#pragma pack(1)
typedef struct {
LStrHandle Value1;
LStrHandle Value2;
LStrHandle Value3;
LStrHandle Value4;
LStrHandle Value5;
LStrHandle Value6;
float64 value;
LVBoolean elt8;
LVBoolean elt9;
LVBoolean elt10;
LVBoolean elt11;
LStrHandle agard;
LStrHandle elt13;
LStrHandle elt14;
} TD2;
typedef struct {
int32 dimSize;
TD2 elt[1];
} TD1;
typedef TD1 **TD1Hdl;
#pragma pack()
#define MAX_AGARD_LENGTH 8
extern "C"
{
void _declspec (dllexport) mainAverage(TD1Hdl hValues, long nCalcValues);
}
class MessureValue
{
public:
double _value;
//+1 to have sufficient space to add terminating 0-character
char _agard[10];
int _flag;
void setContents(double value, LStrHandle agard, int flag);
double getValue();
char *getAgard();
int getFlag();
};
class MessureData
{
public:
MessureValue *_data;
int _nData;
MessureData(int nValues);
~MessureData();
int getSize();
void setContents(TD1Hdl hValues);
MessureValue getValue(int index);
};
int MessureData::getSize()
{
return _nData;
}
MessureValue MessureData::getValue(int index)
{
if(index < _nData)
return _data[index];
else
return _data[0];
}
MessureData::MessureData(int nValues)
{
_data = (MessureValue *)malloc(nValues * sizeof(MessureValue));
if(_data == NULL)
{
MessageBox(0, "geht nicht", "test", 1);
exit(0);
}
_nData = nValues;
}
MessureData::~MessureData()
{
//if(_data != NULL)
free(_data);
}
void MessureData::setContents(TD1Hdl hValues)
{
int counter;
int givenSize = (**hValues).dimSize;
if(givenSize >= _nData)
{
for(counter=0; counter < _nData; counter++)
{
_data[counter].setContents((**hValues).elt[counter].value,
(**hValues).elt[counter].agard,
(**hValues).elt[counter].elt8 );
}
}
}
/*
* method to fill a MessureValue object with the required content
* only the attributes are set which are needed for the calculation
*/
void MessureValue::setContents(double value, LStrHandle agard, int flag)
{
int counter;
//set the actual value
_value = value;
//set the agard number cahracter by character
for(counter=0; (counter<(**agard).cnt) && (counter < 9); counter++)
{
_agard[counter] = (**agard).str[counter];
}
//append a 0-string to mark the end of the agard-string
_agard[counter] = '\0';
//set the flag
_flag = flag;
}
double MessureValue::getValue()
{
return _value;
}
char *MessureValue::getAgard()
{
return _agard;
}
int MessureValue::getFlag()
{
return _flag;
}
void _declspec (dllexport) mainAverage(TD1Hdl hValues, long nCalcValues)
{
FILE *fp;
MessureData *data = new MessureData((**hValues).dimSize-nCalcValues);
int counter;
double a;
int n;
//fflush(fp);
//MessageBox(0, "test", "hallo", 1);
fp=fopen("F:\\test1.txt", "w");
fprintf(fp, "allo");
n = (**hValues).dimSize;
for(counter=0; counter {
fprintf(fp, "%d %f %s\n", counter, (**hValues).elt[counter].value);//(**(**hValues).elt[counter].agard).str );
//a = (*hValues)->elt[counter].value;
//fprintf(fp, "\n%f %s", (*hValues)->elt[counter].value ,(*(*hValues)->elt[counter].agard)->str);
}
//fprintf(fp, "\ntest");
// data->setContents(hValues);
// fprintf(fp, "\n\n**********************");
//fprintf(fp, "\n%d", data->getSize());
//fflush(fp);
//for(counter=0; counter < data->getSize(); counter++)
//{
// fprintf(fp, "\n %d - %f", counter, data->getValue(counter).getValue() );
// fflush(fp);
//}
delete data;
fclose(fp);
}
The problem stills occurs if I comment all the dynamic memory allocations.
I hope someone can help me.
Michael