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 6859367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:16:27+00:00 2026-05-27T02:16:27+00:00

My answer: After getting annoyed, I have found a solution. The problem was indeed

  • 0

My answer:

After getting annoyed, I have found a solution. The problem was indeed C# either C#’s garbage collector or C#’s multithreading, it probably thought the object was no longer needed within THAT thread, and deleted it. The solution was found as follows:

I implemented the ClientThread into the Server class, passing the Client object as a parameters, this minor change made it work. Thank you for all your responses, if anyone in the future has this problem maybe it wasn’t C#’s garbage collector. But C# mutithreading OR networking must be done within the same class. I kept my client class and just made the thread object run the function within the Server class.

If anyone can figure out what my problem was, feel free to comment so I can expand my little knowledge of C#’s memory management.

Thanks again to all the people who attempted to help me in this thread.


Original Question

I’m a C++ programmer so I’m used to managing memory myself, and I’m really not sure how to solve this problem.

For instance in C++:

while(true)
{
    void* ptr = new char[1000];
}

This would be an obvious memory leaking program, so I need to go ahead and clean it up with:

delete ptr;

But there are cases when I want to create memory for use in a different thread and I DO NOT WANT IT DELETED AFTER THE LOOP.

while(true)
{
    socket.Accept(new Client());
}
//////////Client Constructor////////////
Client()
{
    clientThread.Start();
}

This snippet is basically what I want to do in C#, but my client connects then disconnects immediately, I’m assuming this is because at the end of the while loop my new Client() is being deleted by our favorite Garbage Collector.

So my question is, how do I get around this and make it NOT delete my object.

Many have replied saying various things about having other links to it in the code. I forgot to mention that I also save the new client in a list of clients located globally

List<Client> clients;



//inside loop
clients.Add(new Client(socket.Accept()));

Ok because I’m unsure if I’m missing more information here is the ACTUAL code snippet

// Server class
    internal Socket socket { get; set; }
    internal Thread thread { get; set; }
    internal List<Client> clients { get; set; }
    internal void Init()
    {
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        thread = new Thread(AcceptThread);
    }
    internal void Start(int port,List<Client> clients)
    {
        var ipep = new IPEndPoint(IPAddress.Any, port);
        this.socket.Bind(ipep);
        this.socket.Listen(10);
        this.clients = clients;

        this.thread.Start();
    }
    internal void End()
    {
        socket.Close();
        thread.Abort();
    }
    internal void AcceptThread()
    {
        int ids = 0;

        while (true)
        {
            Client client = new Client();
            client.Init(socket.Accept());
            client.clientid = ids++;
            client.Start();
            clients.Add(client);
        }
    }

// Client class
public class Client
{
    .....

    #region Base Code

    internal void Init(Socket socket)
    {
        this.socket = socket;
        this.status = new SocketStatus();
        this.thread = new Thread(ClientThread);
        this.stream = new Stream();
    }
    internal void Start()
    {
        thread.Start();
    }
    internal void Close()
    {
        socket.Close();
        status = SocketStatus.Null;
        thread.Abort();
    }
    internal void ClientThread()
    {
        try
        {
            while (true)
            {
                byte[] data = new byte[1];
                int rec = socket.Receive(data);
                if (rec == 0)
                    throw new Exception();
                else
                    stream.write(data[0]);
            }
        }
        catch(Exception e)
        {
            Close();
        }
    }
    #endregion
}

I thank you for all your replies.

  • 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-27T02:16:27+00:00Added an answer on May 27, 2026 at 2:16 am

    That’s not how it works at all. If there exists any reference to the instance of Client you created, it is not garbage-collected. This doesn’t just apply to your own code, either. Therefore, if GCing is indeed the source of your issue, you never could have accessed it in the first place!

    If you weren’t intending to access it, you can hold on to them anyway by putting them in a List. However, I believe that once you actually use them in the other thread you’re talking about, your problems will go away.

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

Sidebar

Related Questions

After getting a helpful answer here , I have run into yet another problem:
After getting fine answer to my previous question , I came across another problem.
I have to generate charts on my Web application, and after getting the answer
After getting the right answer from this question , I am having another problem
This is probably a really simple jQuery question, but I couldn't answer it after
After getting an answer from this thread, it would save me alot of retyping
I can't figure out an SCJP questions even after getting the right answer: From
after getting my answer here: Database issue, how to store changing data structure i
Is there a way to convert from System.Windows.Forms.HtmlElement to mshtml.IHTMLElemenet3? --edit after answer accepted
After reading this answer: best way to pick a random subset from a collection?

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.