Reputation: 21
I am beginner to C++, Currently i Writing Code for CallBack Function to Getting Bytes array continuously from CLI Wrapper. My code
C++
**Declaration:**
void ReceivedSensor1ByteArray(unsigned char values[], int length);
**Calling:**
GetSensor1ColorsFromCsharp(&ReceivedSensor1ByteArray);
**Definition:**
byte* sensor1bytevalues;
void ReceivedSensor1ByteArray(unsigned char values[], int length)
{
if(length > 0)
{
sensor1bytevalues=new byte[length];
for(int i = 0; i < length; i++)
{
sensor1bytevalues[i]=values[i];
}
}
}
**CLI Wrapper**
**Decalration:**
public ref class SampleWrapper
{
SampleWrapper(void)
{
kinectSocketwrapperObj->ReadBytesValues+=gcnew CLIWrapperClass::ByteValuesReady(this,&Wrapper::SampleWrapper::ByteArrayReadyMethod);
}
public:
CLIWrapperClass ^ kinectSocketwrapperObj;
static SampleWrapper ^ Instance = gcnew SampleWrapper();
void ByteArrayReadyMethod(array<Byte> ^ values);
**Definition:**
GetByteArrayCallback byteArrayCallback;
__declspec(dllexport) void GetSensor1ColorsFromCsharp(GetByteArrayCallback cb)
{
byteArrayCallback = cb;
CLIWrapperClass ^KinectServerWrapper = SampleWrapper::Instance->kinectSocketwrapperObj;
KinectServerWrapper->ReceiveSensor1colors();
}
void SampleWrapper::ByteArrayReadyMethod(array<Byte> ^ values)
{
Byte *nativeValues = new Byte[values->Length];
copyManagedByteToUnfloatArray(nativeValues, values);
byteArrayCallback(nativeValues, values->Length);
}
void copyManagedByteToUnfloatArray(Byte target[], array<Byte> ^ values)
{
int maxSize = values->Length;
if ( maxSize > 0)
{
for (int index = 0; index < maxSize; index++ )
{
target[index] = (float)values[index];
}
}
}
Actually I Receiving Bytes Data from C# through CLI Wrapper Class and Passed to C++ Application in order to DisplayImageFrame. When i call GetSensor1VerticesFromCSharp function continuesly the system Memory increased after 10 minutes the system gets hanged. please suggest me to solve this issue.
Thanks, Kirubha
Upvotes: 0
Views: 566
Reputation: 1856
What's the lifetime of sensor1camevalues? It's not clear if it's part of a class or if you're copying into a global array, which can make managing it's lifetime difficult at best.
Either switch to using a container like std::vector
, which will handle allocating memory for the sensor values for you, or use shared pointers like std::unique_ptr<float []>
to initialise the dynamic array. It's important to read up on RAII / memory management and how this works, as this is a key concept of C++.
Upvotes: 1
Reputation: 419
You should free with "delete" each chunk of memory allocated with "new", if you are about "true" c++, not .Net one.
I.e.:
sensor1camevalues=new float[length]; // memory allocation
delete[] sensor1camevalues; // deallocation
Upvotes: 0