Recently, while working on a project for ImageSource, I ran into a bit of problem that pretty much had me stumped. I had written a .NET library to integrate with a 3rd party COM dll. The code looked correct, it passed all of its test cases and ran just fine when compiled in Debug mode. I created a test application to use this library and stress test it. Everything checked out.
Then I compiled it for Release and obfuscated it. Suddenly it did not run fine anymore. The test application would consistently give me errors of varying severity. Everything from Automation error The server threw an exception to The remote server machine does not exist or is unavailable.
I could get various errors depending on my input parameters, but they didn’t always occur at the same time. Sometimes I would get a COM exception after 10 iterations of a loop, other times it would be 20.
At first, I thought the issue was that the code was obfuscated. I’ve run into some small bugs in the past when obfuscating code, and thought this was no different. However, testing a non-obfuscated Release build of the code produced the same problem. Only switching back to the Debug build fixed the issue. Lots of head banging ensued.
After trying to figure out error messages that seemed to have little rhyme or reason, I finally went back and looked at sample code provided by the 3rd party. After testing their code and finding that it run fine in Release mode, I had to figure out what this code was doing that mine was not.
Their code called a logout method in the API and mine didn’t.
Looking at my own code, I had completely missed making that call and it made all of the difference in the world. My code was staying logged into the server, or at least causing problems server-side because I wasn’t disconnecting.
One added line later and the Release build of my library was working perfectly. In fact, even the obfuscated version worked without a problem. All of that grief and headache caused by one line of missing code.
So I guess the moral of this story is this: Make sure that you close you connections, you release your objects, that you make sure you clean up your mess. Because if you don’t, it could come back to bite down the road. Also, don’t be ashamed to go back and look at sample code when you’re stuck. Sometimes the answer is right in front of you.