Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8714461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:36:05+00:00 2026-06-13T05:36:05+00:00

I have a StateObject class which is being used to store the data from

  • 0

I have a StateObject class which is being used to store the data from client and server.

Here is the code:

 public class StateObject : IDisposable 
    {
        public StateObject()
        {

        }

        public String serviceName = ConfigurationManager.AppSettings["ServiceName"].ToString().Trim();  //Holds the service name 
        public Socket clientSocket; //socket for communication with the client

        public int id; //client id  (A running sequence to keep track of StateObjects)

        public string leaseId; //holds the leaseId that is used to communicate with the server

        public bool isLeaseIdValid = false;

        public string requestQuery = string.Empty;

        public IPEndPoint serverEP;

        public Socket serverSocket; //Socket for communication with the server

        public static int BUFFER_SIZE = Convert.ToInt32(ConfigurationManager.AppSettings["BufferSize"].ToString().Trim());  //Get the buffer size from the state object

        public byte[] clientReadBuffer = new byte[BUFFER_SIZE];   // Receive clientReadBuffer.
        public byte[] serverReadBuffer = new byte[BUFFER_SIZE];

        public int clientNumBytes;
        public byte[] clientSendBuffer = new byte[BUFFER_SIZE];
        public int serverNumBytes;
        public byte[] serverSendBuffer = new byte[BUFFER_SIZE];

        public bool isShutdown = false;

        public Socket serverSocketPort80; //Socket for communication with the server

        public bool ConnectedToPort80 = false; //initially set to false

        public ConnectionObject connectionObject;

        #region Dispose implementation

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                connectionObject.Dispose();
            }
        }

        ~StateObject()
        {
            Dispose(false);
        }

        #endregion
    }

How do I properly dispose this class and clearup the memory used by the byte array ? The byte arrays are being used to store sent/received messages in a socket communication.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:36:06+00:00Added an answer on June 13, 2026 at 5:36 am

    You only need to dispose unmanaged memory.

    In this case it looks like the only thing you need to dispose is the Socket and possibly the ConnectionObject, whatever that is.

    In other words, dispose any instances of IDisposable that this class creates.

    The garbage collector will take care of the byte arrays once this object falls out of scope.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code up on my server here (Yes I known ASMX is
I have a page which is fetching data from a webservice using async call.
have written this little class, which generates a UUID every time an object of
I am studying the MSDN example code of using asynchronous client-server sockets. I understand
I have async Server and async Client on .net Sockets. Client creates object(for example
I have an application which uses SslStream to send and receive data with its
I have written an async UDP client to talk to a server at my
I have a collection class that holds lots of different types of data in
I have created an asynchronous server that can read in messages from any number
I have the following code that saves an object to a file... -(int) saveObject:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.