LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

DLL with c++

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
0 Kudos
Message 1 of 4
(3,263 Views)
Hi there

Why are you using a C++ DLL? Maybe it would be easier to do this code in G and let LabVIEW do all the messy memory/array work for you? This way if there is a bug in your code it is *infinitely* easier to debug rather than trying to debug a DLL.
0 Kudos
Message 2 of 4
(3,263 Views)
The problem ist that the calculation which should be done with the given data are complicated (fluid dynamics).
Anyway, I think I found the problem by now.
It seems to be the handling of the String, that
is passed by LabView. It seems as if the use of that
kind of string is a bit tricky in C++ code.

Maybe someone in this forum has experience with
handling LabView strings in C++. Is there anything
I should consider using them?
Message 3 of 4
(3,263 Views)
Hello Kruemel,

I found this lint to discusion forum . I hope it helps you.

http://exchange.ni.com/servlet/ProcessRequest?RHIVEID=101&RNAME=ViewQuestion&HOID=5065000000080000009A2D0000&ECategory=LabVIEW.LabVIEW+General

TN
Message 4 of 4
(3,263 Views)