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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:32:02+00:00 2026-06-04T14:32:02+00:00

I am trying to use a system of maps to store and update data

  • 0

I am trying to use a system of maps to store and update data for a chat server. The application is mutlithreaded and uses a lock system to prevent multiple threads from accessing the data.

The problem is this: when a client is removed individually from the map, it is ok. However, when I try to call multiple closes, it leaves some in the memory. If I at any point call ::clear() on the map, it causes a debug assertion error with either “Iterator not compatible” or similar. The code will work the first time (tested using 80+ consoles connected as a test), but due to it leaving chunks behind, will not work again. I have tried researching ways, and I have written systems to stop the code execution until each process has completed. I appreciate any help so far, and I have attached the relevant code snippets.

//portion of server code that handles shutting down
DWORD WINAPI runserver(void *params) {  
    runserverPARAMS *p = (runserverPARAMS*)params;  
    /*Server stuff*/                            

    serverquit = 0; 
    //client based cleanup
    vector<int> tokill;
    map<int,int>::iterator it = clientsockets.begin();

    while(it != clientsockets.end()) {      
        tokill.push_back(it->first);
        ++it;
    }
    for(;;) {
        for each (int x in tokill) {
            clientquit[x] = 1;
            while(clientoffline[x] != 1) {
                //haulting execution until thread has terminated
            }
            destoryclient(x);
        }
    }
    //client thread based cleanup complete.
    return 0;
}


//clientioprelim
DWORD WINAPI clientioprelim(void* params) {
    CLIENTthreadparams *inparams = (CLIENTthreadparams *)params;
    /*Socket stuff*/
    for(;;) {       
        /**/
        }
        else {
            if(clientquit[inparams->clientid] == 1)
                break;
        }
    }
    clientoffline[inparams->clientid] = 1;
    return 0;
}

int LOCKED; //exported as extern via libraries.h so it's visible to other source files

void destoryclient(int clientid) {
    for(;;) {
        if(LOCKED == 0) {
            LOCKED = 1;         
            shutdown(clientsockets[clientid], 2);
            closesocket(clientsockets[clientid]);
            if((clientsockets.count(clientid) != 0) && (clientsockets.find(clientid) != clientsockets.end()))
                clientsockets.erase(clientsockets.find(clientid));                  
            if((clientname.count(clientid) != 0) && (clientname.find(clientid) != clientname.end()))
                clientname.erase(clientname.find(clientid));
            if((clientusername.count(clientid) != 0) && (clientusername.find(clientid) != clientusername.end()))
                clientusername.erase(clientusername.find(clientid));
            if((clientaddr.count(clientid) != 0) && (clientaddr.find(clientid) != clientaddr.end()))
                clientaddr.erase(clientusername.find(clientid));
            if((clientcontacts.count(clientid) != 0) && (clientcontacts.find(clientid) != clientcontacts.end())) 
                clientcontacts.erase(clientcontacts.find(clientid));
            if((clientquit.count(clientid) != 0) && (clientquit.find(clientid) != clientquit.end()))
                clientquit.erase(clientquit.find(clientid));    
            if((clientthreads.count(clientid) != 0) && (clientthreads.find(clientid) != clientthreads.end())) 
                clientthreads.erase(clientthreads.find(clientid));
            LOCKED = 0;
            break;          
        }
    }
    return;
}
  • 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-04T14:32:04+00:00Added an answer on June 4, 2026 at 2:32 pm

    Are you really using an int for locking or was it just a simplification of the code? If you really use an int: this won’t work and the critical section can be entered twice (or more) simultaneously, if both threads check the variable before one assigns to it (simplified). See mutexes in Wikipedia for reference. You could either use some sort of mutex provided by windows or boost thread instead of the int.

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

Sidebar

Related Questions

I'm trying to use System.Data.Sqlite library, and I'm following the documentation about optimizing inserts
I am trying to use System.Net.WebClient in a WinForms application to upload a file
I'm trying to use System.Windows.Forms.HTMLDocument in a console application. First, is this even possible?
I'm trying to use System.IO.File.Replace to update a file, and it's throwing System.IOException if
I am trying to use system function. The command that I execute return multiple
When trying to use a system supplied NSValueTransformer in Core Data I get the
I'm trying to learn advanced sql and how to use system queries (sql server).
I am trying to use System.Drawing.Drawing2D.GraphicsPath.AddArc to draw an arc of an ellipse starting
I am trying to use the System.Net.Mail.MailMessage class in C# to create an email
I'm trying to use the System.IO.Log features to build a recoverable transaction system. I

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.