MarekKowalski / LiveScan3D

LiveScan3D is a system designed for real time 3D reconstruction using multiple Azure Kinect or Kinect v2 depth sensors simultaneously at real time speed.
MIT License
749 stars 202 forks source link

Memory Leak in C++ CallBack Function #7

Closed kirubhakinect closed 8 years ago

kirubhakinect commented 8 years ago

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

MarekKowalski commented 8 years ago

Hi Kirubha,

I cannot really tell what is going on based on the code you gave me. One potential place where a memory leak could occur is where you allocate new memory for sensor1bytevalues, remember that you need to free this memory using delete (since it is c++).

If you need any more help, I suggest you write to me by email instead of posting new issues here.

Best regards,

Marek