开发者

C++, .NET: Will this leak memory?

开发者 https://www.devze.com 2023-03-14 10:48 出处:网络
I am wondering if this will cause a memory leak in C++. Let开发者_高级运维 say we have a function in C++ in .NET:

I am wondering if this will cause a memory leak in C++.

Let开发者_高级运维 say we have a function in C++ in .NET:

 test()
 {
     char buffer[NUMBER_OF_BYTES];
     while(forever)
     {
         for(int i=0;i < NUMBER_OF_BYTES;i++)
         {
            buffer[i] = serial->ReadChar();
         }
         String^ serialData = gcnew String(buffer);
         sendDataOut(serialData);
     }
 }

This thing basically just waits for fixed number of serial characters which are placed in a String object to be shipped out for processing. Will the String object be reused or will a new one be created every time through -- eventually chewing through all the PC's memory? If someone can give me some insight, I would appreciate it.


A new one will be created every time. It is allocated with the garbage collector though and so will be deleted when it is no longer referenced. I don't think you need to worry about it causing a memory leak - the only way that would happen is if sendDataOut stores the strings permanently in some collection somewhere, which I'd guess is not the case.

0

精彩评论

暂无评论...
验证码 换一张
取 消