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

  • Home
  • SEARCH
  • 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 7687743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:46:17+00:00 2026-05-31T19:46:17+00:00

I know that i have to dispose all unmanaged resources in a class or

  • 0

I know that i have to dispose all unmanaged resources in a class or form.. but what if i passed a socket to a class then i want to dispose the class and keep the socket!

  • in form1.cs i use TCP socket to read commands from server
  • and i have a class that sends commands to server using the same socket

    so when i initialized the class .. i passed the socket i use in form1 to the class.. im wondering does the socket in form1.cs disposes if i called socket.dispose() from the class?

    do i have to dispose the socket when i dispose the class or just socket = null will be fine? (but i need the socket alive even if i wanted to dispose the class, because i want to keep receiving commands from server).

    the class:

    public class TM
    {
        Socket client;
        public TM(Socket socket)
        {
            client = socket;
        }
    
        public void Dispose()
        {
            client = null; // or client.Dispose()
    
            GC.SuppressFinalize(this);
        }
    
        ~TM()
        {
            Dispose();
        }
    
     }
    

    form1.cs

    TM tm = new TM(client);
    private void whatever() 
    {
    
    StreamReader sr = new StreamReader(new NetworkStream(client));
    while ((cmd = sr.ReadLine()) != null)   //this socket need to keep receiving commands even if i disposed the class
    {
         Console.WriteLine(cmd);
    }
    }
    
    private void Button1_Click(object sender, EventArgs e)
    {
            tm.Dispose();
    }
    

    sorry for my bad english and i hope you understood my point.. thanks in advance.

    • 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-05-31T19:46:18+00:00Added an answer on May 31, 2026 at 7:46 pm

      First, if you’re going to use Dispose, just implement IDisposable. MSDN shows the proper way of implementing the pattern you’re trying to do.

      To the actual question: Since you’re not actually disposing of the Socket in the Dispose of TM, it gets neither closed or disposed. The way it’s shown implemented, it is up to the caller to manage the lifecycle of socket. If TM is not responsible for socket creation, it should not be responsible for socket cleanup either. Doing so could lead to hard to trace down bugs and unexpected behavior.

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

    Sidebar

    Related Questions

    I'm trying to remove all of the leaves. I know that leaves have no
    I know that most people have DirectX9-capable cards but is it really necessary to
    I know that ScrollView can have only one Child but in my case I
    I have a menu bar class (MenuBarGUI) that I have put in all of
    I know that exceptions have a performance penalty, and that it's generally more efficient
    I have ten or more(i don't know) tables that have a column named foo
    As a web developer I am using PHP and I know that I have
    To start, I know that I have to have the </script> tag, and there
    I know that if I have the following group of numbers { 0, 1,
    I know that I should have schema of a table before calling NewRow method

    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.