debugging MSVC++ CRTL memory leaks

It seems like none of the docs on this subject are quite complete.

One of the most missed issues is that there can be multiple heaps in a single process. DLLs that use the CRTL, for example, can get a module-local heap. Each heap will get a separate run of the dump activity, so the allocation number shown in the dump report is local to that heap. You might not break in the right place if you don’t set up the allocation break number in the right module.

When I see the “Detected memory leaks!” message, I put a breakpoint in the _CrtDumpMemoryLeaks() function. This function is in dbgheap.c, which installs to C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\crt\src\dbgheap.c in Visual C++ 2013. It’s the same module that implements _CrtSetDbgFlag() and _CrtSetBreakAlloc(), so stepping into those functions can help open the file.

Breaking on that function’s output of the leaked blocks will reveal the module that’s actually making the call; just look at the module name information in the stack when the breakpoint is hit.

After finding the right module, adding these two lines of code to the application’s InitInstance() method should get the allocator to break on the appropriate allocation number:


The break call might be placed in main() or in DllLoadLibrary(); whatever is the earliest available point. Static constructors run before these method are called, so it’s conceivable that another static constructor needs to be written to get the all in place early enough.

Some sources show multiple calls to _CrtSetBreakAlloc() implying that the library can check for multiple break numbers — but it can’t.

Asking about Questions

Over at The Ladders, career coach Lee E. Miller advises about a question lots of interviewees get: Tell me about yourself.

Mr Miller is right about one thing: this question shouldn’t take an interviewee by surprise. An interviewee should be poised and confident, and shouldn’t hesitate to describe themselves or their experiences. After all, providing compelling descriptions about yourself — compelling enough to decide that you’re a hire — is what interviewing is all about for the candidate.

But I think he blows it in one critical regard. He says that the wrong answer is to ask “what would you like to know?” and explains that such a response indicates the candidate hasn’t prepared.

I couldn’t disagree more. A candidate who asks such a question is showing respect for the interviwer’s time. He can assume he knows what the interviewer wants, but wouldn’t it be better to make sure his information and conclusions are correct? If he can confirm that he’s going to attack the right targets, then he’s in much better shape than someone who’s got the wrong idea and starts off in the wrong direction.

Mr Miller asserts that a candidate asking this question isn’t prepared, and wouldn’t be prepared on the job. That seems like an illogical conclusion; one’s instantaneous performance for a single, out-of-domain question probably doesn’t forecast their performance for their at-work tasks. Further, if we supposed Mr Miller’s  brand of conclusion were correct, we could also assume a candidate who doesn’t ask this question is someone who would assume they were right and commit to a direction which they haven’t previously verified. How many experienced managers have worked with an employee — a new hire, in particular — who went off in a certain direction without verifying that direction was in the best interests of the company and all the stakeholders in the matters touched by that work?

After all, that’s what Mr Miller is insisting the candidate should do.

Outlook and Meeting Attendance

If I schedule a meeting in Microsoft Outlook, I can optionally get responses to that meeting request. The responses are just messages from each invitee with a subject indicating their attendance.

Someone might attend, so they’ll cause the Calendar to send a message with the subject “Accepted: FY2012 Budget Review”. Someone else might be tentative about attending, and send a subject of “Tentative: FY2012 Budget Review”. Or, they might decline: “Declined: FY2012 Budget Review”.

Let’s say I’ve booked a meeting for a very large audience. I invite 300 people; it might be most of a department, or even my whole company. I’d like to know if I should book the medium-sized conference room or the large-sized conference room. Will 150 people attend, or will it be closer to 300?

Remarkably, I can’t find a way in Outlook to count the number of attendees.

If I save all the messages I got in response, I could put them in a folder in my inbox and sort them by subject. But they don’t sort correctly by subject, and there’s no way to count them anyway unless I make a folder for each one, or each meeting. I’d need a folder for the FY2012 Budget Review Attendees separately from the FY2012 Budget Review Tentative Attendees. Then, when viewing on of the folders, Outlook displays a note in the status bar saying the folder has “64 items” in the folder, so I know there are 64 attendees. This isn’t very convenient, though.

If I click on the “Tracking” button in the invite, I see a list of people who were invited and their response. I could sort list by responses, but there’s no way to see a count of any of the groups.

If I use the scheduling assistant, I can get a look at the attendees to see if they’re attending or not, and even what the rest of each of their days looks like (though I don’t find that too useful). But no way to count attendees.

Am I missing a feature? How did Microsoft intend us to track meeting invites?

Recovering SQL Server databases without a log file

I use two drives in RAID0 for my database server at home. This is pretty bad, for sure. The RAID0 log array has failed twice in six years.

I probably should just drop the extra $80 to add a third drive and make RAID5, or buy a couple more and make RAID10. When the failure happens, I don’t lose any data; all the data could be reloaded, but not from a backup — from the original data source. That’s quite time consuming.

I’ve found that I can have SQL Server create a log file if the volume is lost, and that allows me to again mount the related database. This still might cause trouble; I might lose data, I might end up with uncommitted or (half-committed, really) transactions, and so on. Since the server is usually quiescent, it just doesn’t matter to me.

Since I’m often doing this, and since the script is a bit elusive, here’s a note to myself that explains what to do.

First, I need to put the database into EMERGENCY recovery mode:


Then, I’ll issue a CHECKDB statement to make sure the database is OK.

DBCC CHECKDB('database');

I can then issue an ALTER DATABASE command to rebuild the log:


ALTER DATABASE DatabaseName REBUILD LOG ON (NAME=Database_Log,FILENAME='F:\SQLLogs\DatabaseName_log.Ldf');


Then reset the database to Multi-User:


before doing another CHECKDB to make sure everything is OK.

This isn’t right, and isn’t something I’d do in production, but it is quite fine for my lame little test server.

Where can I get good news?

I turned on CNN looking for coverage of the Tohoku earthquake. I’m not sure I’m allowed to watch CNN anymore.

The first story I saw was about a YouTube video made by a New York senator that “teaches parents to spy on their kids”. The idea is that the parents learn how kids hide thing and where to look for drugs, weapons, or other dangerous items. I’m not sure this is news, but it seems okay if CNN spends a bit of time on human interest stories.

Thing was, though, CNN made this the “Your Views Question of the Day”. They solicited input on the Internet, then read comments users made on the air. Who care what Ricky from Indiana thinks about this story? Why is spending 90 seconds on reading reader comments, without analyzing them, at all useful?

Coverage moved to Hawaii, where some effects of the tsunami were felt. At 20 seconds, the segment was incredibly short and conveyed zero information. Why not show a map of Hawaii, drawing lines on the coast where green, yellow, and red indicate the severity of the damage? No damage reported? No line, then.

The description of the damage was hopelessly vague and un-insightful. Why dumb down such a simple report so severely?

The next story was about Rhode Island requiring high school students “to score at least partially proficient” on a standardized test in order to graduate. The coverage involved an interview with a representative with the ACLU, an organization with amiable goals but — as far as I can tell — zero influence or experience in the area of education or child development. What about the other side of the coin? Why not interview students who are bored because their classes cover material at the pace of the slowest student? Why not interview colleges or employers who are tired of teaching recent graduates things that they should have learned earlier in school?

The shallow depth of coverage, one-sided reporting, and shameless self-promotion leave me cold. Is there a news outlet that is acceptable?

Crystal Disk Mark and SQL Server recommended some benchmarking tools for SQL Server users via Glenn Berry’s SQL Server Performance Blog. Problem is, one of the tools is pretty badly flawed.

Berry recommended Crystal Disk Mark, which is a cute tool that ostensibly measures disk performance. Thing is, it does a pretty shoddy job. If you run the test, you’ll notice that the disk queue for the drive its testing is never deeper than one. That is, it doesn’t do multi-threaded or overlapped I/O requests. Instead, it issues a single blocking requests. It doesn’t take long to download the source code and confirm this fact.

For desktop machines, doing only one I/O at a time isn’t so bad because that matches the workload desktop machines typically show their drives during normal use.

It seems a bit irresponsible to recommend such a tool for servers, though–particularly machines which will be running SQL Server, where multiple outstanding I/O operations are key to achieving any sensible level of performance. I like to have at least a couple outstanding I/O operations per logical spindle on the servers I run. This lets the controller and the drive have a shot at elevator seeking across multiple requests, combining redundant requests in cache, and batching as best as possible.

Since none of Crystal Disk Mark’s test modes provide any I/O request depth, it won’t provide an accurate measure of performance for the disk arrays and storage subsystems that are typically used with SQL Server installations and it isn’t an appropriate tool for capacity planning or evaluating hardware for such installations.

Troubling Education Stats

A Right Denied is a slide deck from the Organization for Economic Cooperation and Development. It’s full of stats that paint a pretty bleak picture about education in the United States. Some of the slides explain that there are internal inequities, where students in certain areas (or of certain races!) get worse educations than others. There are other sides that point out how the countries’ students do terribly in mathematics and science education compared to their international counterparts.

Test Post #4

What else could be wrong?

Understanding RSA Chiphers

I found a great article on understanding RSA public key ciphers. There’s just enough background information and good description of the mechanics.

Thank an Engineer: The Chilean Miner Rescue

The media’s treatment of the Chilean miner rescue is really getting under my skin.

It’s not luck (well, maybe a little.) It’s not a miracle. The heroes of this story aren’t vague spirits or chancy dice rolls.

The heroes of this story are tangible, identifiable people. Will we ever seen an interview with the foreman of the crew that drilled the rescue chute, the engineer who designed the drop basket or the cutting heads, or the geologist who read sonograms to find the safest and most stable path to the chamber? They measured many times and cut only once with precision that rivals the moon landing because once was exactly how many chances they had at success.

This kind of courage, determination, and execution is rare in our world, and must be celebrated when used for good.