Calvin Hsia's WebLog

thoughts from a professional developer

Browse by Tags

Tagged Content List
  • Blog Post: Use status events to log and analyze an application

    Applications can get quite complex, with multiple components, assemblies, subsystems, etc. Understanding this complexity can be daunting. An old but still very effective way of analyzing code is to modify the code to output a string whenever that section of code gets executed. Remember the old Printf...
  • Blog Post: Examine the layout of managed strings in memory

    Suppose you wrote some C# code like this: var str1 = "ThisIsAString" ; var str2 = "ThisIsAnotherString" ; As you’d expect, each string is stored in the resulting built binary and also in memory when the binary is loaded, resulting in 2 separate strings. Now suppose...
  • Blog Post: Advanced debugging: change your program execution without Edit and Continue

    Last time ( Improve your managed debugging skills: examining registers and memory ) we examined some debugging techniques to understand the behavior of managed code. It might take a long time or many manual steps to reproduce a particular software behavior in an application. Changing the instructions...
  • Blog Post: Improve your managed debugging skills: examining registers and memory

      I was helping a colleague and we were deep in the middle of a debug session, single stepping some code and we wanted to see a value in the debugger. The debugger showed either nothing, because the intermediate value has been optimized out, or a message like “cannot display value, possibly because...
  • Blog Post: Play around with .Net Dictionaries and STL maps

    Last time ( Adventures in interop code: explore string interop memory ) we investigated sharing memory between native and managed code, or between processes. Data structures are very useful: things like arrays, structures, lists, dictionaries, stacks, queues, etc. are used throughout programming...
  • Blog Post: Adventures in interop code: explore string interop memory

    In Create an ActiveX control using ATL that you can use from Fox, Excel, VB6, VB.Net , I showed how to create a control (with which the user can interact) which can be hosted in many places. Today’s sample creates a class in C++ that doesn’t necessarily have UI, and thus isn’t visually hosted,...
  • Blog Post: You can develop code faster

    You can make your Visual Studio experience faster. Often you write some code in Visual Studio, hit F5 to test the code. Repeat. Here’s a simple way to make this experience faster. (works with managed, native, mixed code). When a process is started from the debugger, it is started with...
  • Blog Post: What is your computer doing with all that memory? Write your own memory browser

    What is your computer doing with all that memory? There are various kinds of memory allocated and used in each process. These include: · Managed memory (VB.Net, C#, managed C++) · Heap memory · Stacks · Images (files loaded into a process) VirtualAlloc is the basis of these allocations...
  • Blog Post: Its easy to create a graph of memory use in Excel

    In this post Examine .Net Memory Leaks I showed how to find a .Net managed memory leak. Now let’s create a graph of memory and resource use over time. Start Visual Studio 2008, File->New->Visual Basic (or C#) Windows, WPF application. Dbl click the WPF form to get to the Xaml.cs...
  • Blog Post: Process Explorer and Process Monitor can help you understand a program

    Process Explorer and Process Monitor are must-have free utilities that you can download from http://technet.microsoft.com/en-us/sysinternals/bb795533.aspx For example you can use ProcExp to examine a particular behavior of Visual Studio. Start Visual Studio (any version). I was using VS 2008...
  • Blog Post: You can use Visual Studio to debug itself!

    How do you find out why your computer or a running program is so slow? Here’s one way. Let’s attach the VS debugger to VS itself. The main executable for VS is devenv.exe. Start Visual Studio 2008. This will be the “debugger” Choose File->Open Project C:\Program Files\Microsoft Visual...
  • Blog Post: Overload Operator new to detect memory leaks

    There are various leak detection methods for memory allocators. A popular one is to tag each allocation with some information about the caller. When there’s a memory leak, you just need to look at that tag info to find the line of code that allocated the memory. However, this requires that the...
  • Blog Post: Write simple Debug helpers to help you debug and maintain your code

    Much of my time is spent using the Visual Studio debugger examining code to figure out how it works and how to fix it. When stepping through a function, the values the function uses are very useful for code understanding. The debugger shows these values in the Watch/Locals/Auto/Callstack windows. For...
  • Blog Post: Make your code more maintainable: The evils of the Return statement

    What does it mean to make code more maintainable? Certainly obfuscated code is hard to understand, by definition. A big part of maintainability is making it easier for others to read and understand what the code is doing. Your code may have been working for years, but then somebody comes along...
  • Blog Post: Examine .Net Memory Leaks

    Writing programs using .Net is very productive. One reason is because much of memory management is “managed” for you. In C, C++ and other “native” languages, if you allocate memory, you’re responsible for freeing it. There were stopgap measures, like destructors, SmartPointers and reference counting...
  • Blog Post: Find the Executing function's name

    Often I want to write the SAME code that will display the name of the currently executing method or function. That way I can just copy/paste the same code into multiple methods. For example, in sub Form1_Load I could put this line: System.Diagnostics.Debug.WriteLine( "in Form1_Load" ) ...
  • Blog Post: Customize the display of types in the Debugger using Extension Methods and DebuggerDisplay Attribute

    I was writing some code using System.Text.StringBuilder . : Dim sb As New StringBuilder( "Init SB String" ) At a breakpoint the debugger Locals Window shows + sb {System.Text.StringBuilder} System.Text.StringBuilder If I expand by clicking on the "+", I get: - sb {System...
  • Blog Post: Find which DLLs in your system are rebased

    You can use CreateToolhelp32Snapshot and its family of functions to enumerate the running processes on your machine, including the modules loaded by each process My prior post ( DLL Image base addresses are the same in XP, different on Vista ) described how Dlls are loaded and how some can be rebased...
  • Blog Post: How to log application API calls using import module addresses

    Let’s log all the calls that Excel makes to open or create a file. Start Visual Studio (any version), choose File->Open->Projects. In the dialog, change the “Files of Type” to “Executable Files (*.exe)” Choose any application like Excel: C:\Program Files\Microsoft Office\OFFICE11\EXCEL...
  • Blog Post: Customer question: what does the crash call stack mean

    A customer asks: I read your article "Intentionally crash your program". I have some questions that I hope you can answer. When a fatal exception happens, foxpro display the calling stacks. How to read the calling stacks? A calling stack seems to contain the following information: 1. either...
  • Blog Post: Dynamically attaching a debugger

    Sometimes something goes wrong with your program and you want to investigate why. You can start Visual Studio, choose Debug->Attach to Process and choose the process you want to debug. (The dialog also allows remote debugging and choosing Native, Managed, Mixed, Script, T_SQL, and Auto.) Perhaps...
  • Blog Post: Problems reading and writing to the same file stream

    I was helping a friend fix a problem that occurred in a VB.Net application. Upon inspection, an XML file was somehow invalid. The last few bytes were wrong. We found the code that modifies the file. It takes the original file, replaces a string with another string, then writes the file. The code...
  • Blog Post: What API calls reset GetLastError between Declare DLL calls in VFP8?

    I received a comment on this post: Will GetLastError ever work properly in VFP8.0? . I was consistently getting GetLastError() values that were correct in both VFP8 and VFP9. The reader comment said that he was getting an unexpected value of 0 in VFP8 even though he was expecting a failure value with...
  • Blog Post: Edit and Continue in VFP can save you time

    Sometimes it takes many steps to reproduce a problem. Perhaps you have to start an application, log in, navigate some forms, menus, etc. until you finally reach a breakpoint in your code. Then you see the problem, want to modify the code and retest. That means executing all those steps again. Or you...
  • Blog Post: Customize the VS debugger display of your data

    As a software developer, I spend much of my time looking at code, learning how it works, and figuring out how to modify or fix it. A very good tool to help examine code is the Visual Studio debugger. (Even if you’re not a hard core programmer, the following tutorial shows some of the power of the...
Page 1 of 2 (43 items) 12