The Old New Thing
Practical development throughout the evolution of Windows.
Latest posts

How do I get the reference count of a CLR object?

A customer asked the rather enigmatic question (with no context): Is there a way to get the reference count of an object in .Net? Thanks, Bob Smith Senior Developer Contoso The CLR does not maintain reference counts, so there is no reference count to "get". The garbage collector only cares about whether an object has zero references or at least one reference. It doesn't care if there is one, two, twelve, or five hundred—from the point of view of the garbage collector, one is as good as five hundred. The customer replied, I am aware of that, yet the mechanism is somehow implemented by the GC......

Everybody thinks about CLR objects the wrong way (well not everybody)

Many people responded to Everybody thinks about garbage collection the wrong way by proposing variations on auto-disposal based on scope: "Any local variable that is IDisposable should dispose itself when it goes out of scope." "You should be able to attach an attribute to a class that says the destructor should be called immediately after leaving scope." "It should have promised to call finalizers on scope exit." What these people fail to recognize is that they are dealing with object references, not objects. (I'm restricting the discussion to reference types, naturally.) In C++,...

When does an object become available for garbage collection?

As we saw last time, garbage collection is a method for simulating an infinite amount of memory in a finite amount of memory. This simulation is performed by reclaiming memory once the environment can determine that the program wouldn't notice that the memory was reclaimed. There are a variety of mechanism for determining this. In a basic tracing collector, this determination is made by taking the objects which the program has definite references to, then tracing references from those objects, contining transitively until all accessible objects are found. But what looks like a definite reference in your code ma...

Everybody thinks about garbage collection the wrong way

Welcome to CLR Week 2010. This year, CLR Week is going to be more philosophical than usual. When you ask somebody what garbage collection is, the answer you get is probably going to be something along the lines of "Garbage collection is when the operating environment automatically reclaims memory that is no longer being used by the program. It does this by tracing memory starting from roots to identify which objects are accessible." This description confuses the mechanism with the goal. It's like saying the job of a firefighter is "driving a red truck and spraying water." That's a description of what a firefigh...

Some known folders cannot be moved, but others can, and you'll just have to accept that

Locations in the shell known folder system can be marked as , which makes them immovable. Conversely, if a file system folder is not immovable, then it can be moved. This dichotomy appears simple and unworthy of discussion, except that customers sometimes have trouble incorporating this concept into their world view. I have some code that calls , and it works for most of the folders I'm interested in, but for some values like , it fails with . Doesn't matter if I run elevated or not. What am I doing wrong? The difference is that (known under the New World Order as ) is marked as so it cannot be moved. I...

Raymond misreads flyers, episode 2: It Takes You

As part of a new phase in Microsoft's continuing recycling efforts, the recycling program got a new motto. The new motto was not announced with any fanfare. Rather, any recycling-related announcement had the new motto at the top as part of the letterhead. The new motto: It Takes You. I had trouble parsing this at first. To me, it sounded like the punch line to a Yakov Smirnoff joke. Episode 1.

How many failure reports does a bug have to get before Windows will fix it?

When a program crashes or hangs, you are given the opportunity to send an error report to Microsoft. This information is collected by the Windows Error Reporting team for analysis. Occasionally, somebody will ask, "How many failures need to be recorded before the bug is fixed? A thousand? Ten thousand? Ten million?" Each team at Microsoft takes very seriously the failures that come in via Windows Error Reporting. Since failures are not uniformly distributed, and since engineering resources are not infinite, you have to dedicate your limited resources to where they will have the greatest effect. Each failure th...

Don't forget to replace your placeholder bitmaps with real bitmaps

The story We Burned the Poop reminded me of an embarrassing story a colleague of mine related from earlier in his programming career. During the development of the product he was working on, the programmers needed an image for a comparatively rarely-used piece of the user interface. Since programmers aren't graphic designers, they inserted a placeholder bitmap which would be used until a real image arrived. And since programmers are nerds, they used a picture of a television character who was popular at the time. The testers naturally ran the program through its paces, and when that piece of the user interface...

A brief conversation while preparing to hike along the Pacific coast

Many years ago, one of my colleagues (who happens to be an avid outdoorsy person with a dry sense of humor) assembled a small group of people to take a long weekend hike along the Capa Alava Trail and then north along the Pacific coast. As we gathered in the parking lot midday Friday with our backpacking gear, another of our colleagues (whom I will call "Mary") walked past and the following conversation ensued: Mary: "Hey, where ya goin'? (joking) That way I can call Search and Rescue if I don't see you on Monday." Bob: "We're heading out to Lake Ozette." Mary: "Cool, where's Lake Ozette?" Bob: "That's okay...