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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:18:52+00:00 2026-05-19T03:18:52+00:00

I have a server, which uses a global variable to store the next available

  • 0

I have a server, which uses a global variable to store the next available unique ID.

Originally, this was defined as unsigned long g_nextUniqueId volatile; I’m changing all IDs to be 64-bit integers, so this line was changed to unsigned __int64 g_nextUniqueId volatile;

There are only two pieces of code that directly access this variable.

The first is to populate it when the server starts, it’s very simple, it just runs a SQL query, retrieves a value and stores it into a unsigned __int64 lastId, and then there’s a statement to store it in the global, g_nextUniqueId = 1 + lastId;.

The other is a function to retrieve and consume the next available ID. This is a one line function, return (unsigned __int64)InterlockedIncrement64((LONGLONG*)&g_nextUniqueId);

The problem seems to be there there are two different g_nextUniqueId variables, for lack of better terms, in the initialization function.

When g_nextUniqueId is populated, the correct value is written to the wrong address. According to the debugger, &g_nextUniqueId is not the address the value was written to. If I store &g_nextUniqueId in another variable, as a void*, the values of &g_nextUniqueId and the void* are not equivalent. The void* value is actually the correct address. This only holds true inside this one function; in any other function, the void* and &g_nextUniqueId are equivalent.

void* somePtr = (void*)&g_nextUniqueId;
Output(ToString(somePtr) + " " + ToString(&g_nextUniqueId));
// Output will be something "0x01BAF1D8 0x0012EFA4"
// 0x0012EFA4 is on or near the bottom of the stack, I believe.

Later, when I go to retrieve the next available ID, the g_nextUniqueId the InterlockedIncrement64 acts upon will be the correct one, which has a value of 0, since the initial value was written to the wrong address.

Hopefully this makes sense (the problem description, that is).

Why is the g_nextUniqueId = 1 + lastId; line writing to the wrong address? If I change the type back to unsigned long, the code works correctly.

At the moment, the only solution I can come up with is copying &g_nextUniqueId to a void* and then casting it back to a volatile unsigned __int64 and assigning.

  • 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-19T03:18:53+00:00Added an answer on May 19, 2026 at 3:18 am

    Sounds like you need to do a full recompile.

    This is often the case when you make a change to a type used in multiple translation units. Sometimes the dependency checker gets confused.

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

Sidebar

Related Questions

I have a Glassfish server in production which uses JDBC Realm for authentication. It
I have an ASP.NET MVC server app and client code which uses jquery. I'd
I have a n-tier C# ASP .Net application server which uses stored procedures to
I have a Winsock based server application which uses Windows Winsock I/O Completion Ports.
We have a SQL 2005 database server which uses high safety (synchronous, no automatic
I have a multi threaded client server application which uses Sockets. When a new
We have Hudson CI server which uses msbuild to build our C# code. Does
i have a client-server application which uses .NET Remoting communication. For authentication reasons i
I have a basic client server in erlang which uses tcp. How does one
I have a signup form on my home page which uses server side validation

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.