none
Is this byte[] and Array manipulation necessary or is it overkill? RRS feed

  • Question

  • I have a class that has a byte[] _messageBuff that's global to the class.  This data is being sent from a server to a client via TcpClient / NetworkStream.   At button click, I need to change the contents of this global _messageBuff buffer.  So, I'm wondering is it necessary to go through all this work just to change the contents or can I simply set it to null _messageBuff = null and re-populate it?  

    // If the global buffer is not empty, clear it's contents
    if( _messageBuff.Length > 0 )
    {
        Array.Clear( _messageBuff, 0, _messageBuff.Length );
        Array.Resize( ref _messageBuff, 0 );
    }
    
    // Get new message string and copy it into global buffer
    byte[] tempBuff = Encoding.ASCII.GetBytes( tbMessage.Text );
    Array.Copy( tempBuff, _messageBuff, tempBuff.Length );

    Thanks!


    Rick

    Friday, March 1, 2019 3:20 AM

All replies

  • I must be missing something. What would be wrong with...

    _messageBuff = Encoding.ASCII.GetBytes( tbMessage.Text );

    Friday, March 1, 2019 3:44 AM
  • If _messageBuff was already pointing to a location in memory that had bytes allocated, then I allocate new bytes and point buff to that, wouldn't that be a memory leak?

    I guess I'm thinking back to my OLD C++ days. 


    Rick

    Friday, March 1, 2019 3:46 AM
  • I think the garbage collector should be able to cope.

    I have a feeling someone with more knowledge in this area is going to correct me, but the only reason I can see for the first approach would be if there is another reference to the same array that you want to make sure has the same contents. Something like so...

    void Click() { byte[] tempBuff = _messageBuff; DoSomething(); // At this point, tempBuff and _messageBuff will be // different arrays. But using your original approach, // they will be the same (I think. Fairly sure, anyway.),

    //so it depends on what you require to be the case. } void DoSomething() { _messageBuff = Encoding.ASCII.getBytes(tbMessage.Text); }



    Edit : By "using your original approach", I mean using that approach in DoSomething (in case that wasn't clear).
    • Edited by Ante Meridian Friday, March 1, 2019 6:02 AM Clarification.
    Friday, March 1, 2019 5:59 AM
  • Hi m00n,

    Thank you for posting here.

    As I know, you could use Pointers to copy bytes from one to another. In C#, it does not mean memory leak. It calls unsafe code. 

    For more details about use Pointers to copy bytes from one to another, please check the exampls in the lin below.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/unsafe-code-pointers/how-to-use-pointers-to-copy-an-array-of-bytes

    In the common language runtime (CLR), unsafe code is referred to as unverifiable code. Unsafe code in C# is not necessarily dangerous; it is just code whose safety cannot be verified by the CLR. The CLR will therefore only execute unsafe code if it is in a fully trusted assembly. If you use unsafe code, maybe it would cause security risks or pointer errors.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 1, 2019 6:06 AM
    Moderator
  • Hi Wendy...

    I'm not really wanting to get into pointers and all that.  I guess I'm just remembering my old C++ days here if you created memory, you had to delete it or you would have a memory leak. 

    I know that with C# and .NET we have the garbage collector.  But, I've always been a little confused as to when you should still manually clean up unused memory manually, if at all. 


    Rick


    • Edited by m00n Friday, March 1, 2019 7:03 AM
    Friday, March 1, 2019 7:03 AM
  • Hi m00n,

    A easy to confirm. If the object provided the Dispose method, you could use Dispose or using statement to release manually. 

    When you use GC.Collect Method to force garbage collection.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 4, 2019 6:42 AM
    Moderator