Showing tag results for Code

Jan 20, 2010
Post comments count0
Post likes count1

The wrong way to determine the size of a buffer

Raymond Chen

A colleague of mine showed me some code from a back-end program on a web server. Fortunately, the company that wrote this is out of business. Or at least I hope they're out of business!

Code
Jan 18, 2010
Post comments count0
Post likes count1

It's fine to rename a function in your DEF file, but when you do, you have to link to that function by its new name

Raymond Chen

Jeffrey Riaboy asks why, if he renames a function in his DEF file, attempts to link to the function by its old name fail. Well, um, yeah, because you renamed it. Let's take the situation apart a bit; maybe it'll make more sense. I'm going to ignore a lot of details (, calling conventions) since they are not relevant to the discussion and wou...

Code
Jan 15, 2010
Post comments count0
Post likes count1

How you might be loading a DLL during DLL_PROCESS_DETACH without even realizing it

Raymond Chen

As you are I'm sure aware, you shouldn't be doing much of anything in your function, but you have to watch out for cases where you end up doing them accidentally. Some time ago, I was investigating a failure which was traced back to loading a DLL inside . Wait, what kind of insane person loads a DLL as part of shutting down? Shouldn't you be cl...

Code
Jan 13, 2010
Post comments count0
Post likes count1

Why does GetCommandLine give me a corrupted command line?

Raymond Chen

A customer had the following problem: We're calling to retrieve the command line, and the documentation says that it returns a single null-terminated string. However, when we call it in our application, we find that it is actually a double-null-terminated string. The buffer returned consists of a series of null-terminated strings, one string per ...

Code
Jan 8, 2010
Post comments count0
Post likes count1

What is the hSection parameter to CreateDIBSection for?

Raymond Chen

The function creates a special type of bitmap known as a DIB section. We've worked with these guys before: The feature of DIB sections that is by far the most interesting is that the raw pixels in the bitmap are mapped into your process space as if they were normal memory, which you can read from and write to directly. But what is the deal with...

Code
Jan 1, 2010
Post comments count0
Post likes count1

Your program assumes that COM output pointers are initialized on failure; you just don't realize it yet

Raymond Chen

We saw last time that the COM rules for output pointers are that they must be initialized on return from a function, even if the function fails. The COM marshaller relies on this behavior, but then again, so do you; you just don't realize it yet. If you use a smart pointer library (be it ATL or boost or whatever), you are still relying on output...

Code
Dec 31, 2009
Post comments count0
Post likes count1

Why does COM require output pointers to be initialized even on failure?

Raymond Chen

One of the rules of COM is that if a parameter is marked as an output pointer, then you have to initialize the thing it points to, even if your function failed and you have nothing to return. For example, we saw the problems that can occur if you forget to set the output pointer to in the method. Why does COM have this rule? Doesn't it know that...

Code
Dec 24, 2009
Post comments count0
Post likes count1

Why don't we create a special class of programs which can break the normal rules?

Raymond Chen

In response to a discussion of why the window handle limit is 10,000, commenter Juan wondered why we don't create a special class of programs which can exceed the 10,000 handle limit and otherwise bypass the normal operation of the system. This is another case of the tragedy of special treatment: Eventually, nothing is special any more. If ther...

Code
Dec 23, 2009
Post comments count0
Post likes count1

Why is it possible to destroy a critical section while it is in use?

Raymond Chen

Some time back, Stu wondered why it is possible to destroy a critical section while it is in use. Well, there's nothing stopping you from creating a file that contains these lines: and then telling your compiler to turn it into a program. It's not like a bolt of lightning is going to come out of the sky and zap you before you hit the Enter k...

Code
Dec 18, 2009
Post comments count0
Post likes count1

I got an array with plenty of nuthin'

Raymond Chen

A customer reported a memory leak in the function : We found the following memory leak in the function . Please fix it immediately because it causes our program to run out of memory. If the 's type is , then the corresponding is leaked and not cleaned up. Right now, we are temporarily working around this in our program by inserting code b...

Code