我是C++的初學者,目前我正在爲CallBack函數編寫代碼以從CLI Wrapper中連續獲取Bytes數組。我的代碼C++中的內存泄漏回調函數
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];
}
}
}
其實我從C#通過CLI的包裝類,接收字節的數據,並傳遞給C++應用程序,以DisplayImageFrame。當我調用GetSensor1VerticesFromCSharp函數時,持續10分鐘後系統內存增加,系統被掛起。請建議我解決這個問題。
感謝, Kirubha
** sensor1camevalues **它只是一個變量,我清理它正確。其實我認爲CallBack函數需要太多內存「typedef void(* GetByteArrayCallback)(unsigned char values [],int length);」 __declspec(dllexport)void GetSensor1ColorsFromCsharp(GetByteArrayCallback cb);建議我如何清除回調函數 –