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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:11:14+00:00 2026-05-14T20:11:14+00:00

There’s a bit of code which writes data to a MemoryStream object directly into

  • 0

There’s a bit of code which writes data to a MemoryStream object directly into it’s data buffer by calling GetBuffer(). It also uses and updates the Position and SetLength() properties appropriately.

This code works properly 99.9999% of the time. Literally. Only every so many 100,000’s of iterations it will barf. The specific problem is that the Position property of MemoryStream suddenly returns zero instead of the appropriate value.

However, code was added that checks for the 0 and throws an exception which includes log of the MemoryStream properties like Position and Length in a separate method. Those return the correct value. Further addition of logging within the same method shows that when this rare condition occurs, the Position only has zero inside this particular method.

Okay. Obviously, this must be a threading issue. And most likely a compiler optimization issue.

However, the nature of this software is that it’s organized by “tasks” with a scheduler and so any one of several actual O/S thread may run this code at any give time–but never more than one at a time.

So it’s my guess that ordinarily it so happens that the same thread keeps getting used for this method and then on a rare occasion a different thread get used. (Just code the idea to test this theory by capturing and comparing the thread id.)

Then due to compiler optimizations, the different thread never gets the correct value. It gets a “stale” value.

Ordinarily in a situation like this I would apply a “volatile” keyword to the variable in question to see if that fixes it. But in this case the variables are inside the MemoryStream object.

Does anyone have any other idea? Or does this mean we have to implement our own MemoryStream object?

Sincerely,
Wayne

EDIT: Just ran a test which counts the total number of calls to this method and counts the number of times the ManagedThreadId is different than the last call. It’s almost exactly 50% of the time that it switches threads–alternating between them. So my theory above is almost certainly wrong or the error would occur far more often.

EDIT: This bug occurs so rarely that it would take nearly a week to run without the bug before feeling any confidence it’s really gone. Instead, it’s better to run experiments to confirm precisely the nature of the problem.

EDIT: Locking currently is handled via lock() statements in each of 5 methods that use the MemoryStream.

  • 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-14T20:11:14+00:00Added an answer on May 14, 2026 at 8:11 pm

    (Really need exemplar code to confirm this.)

    MemoryStream members are not documented as thread safe (e.g. Position) so you need to ensure you are only access this instance (or any reference to an object logically a part of the MemoryStream) from one thread at a time.

    But MemoryStream is not documented as having thread affinity, so you can access an instance from a different thread—as long as such an access is not concurrent.

    Threading is hard (axiomatic for this Q&A).

    I would suggest you have some concurrent access going on, with two threads both accessing the same instance concurrently and this is, occasionally, corrupting some aspect of the instance state.

    I would ensure I keep the locking as simple as possible (trying to be extra clever and limiting locking is often a cause of very hard to find bugs) and get things working. Testing on a multi-core system may also help. Only try and optimise the locking if profiling shows there is potential for significant net (application as a whole) gain.

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

Sidebar

Ask A Question

Stats

  • Questions 424k
  • Answers 424k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Never heard of FreeBasic before but looking at the help… May 15, 2026 at 11:59 am
  • Editorial Team
    Editorial Team added an answer <iframe> elements have a load event for that. How you… May 15, 2026 at 11:59 am
  • Editorial Team
    Editorial Team added an answer How about newmean <- function(X, ...){ args <- as.list(substitute(list(...)))[-1L] z<-list(X)… May 15, 2026 at 11:59 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.