Naggy : Live compiler diagnostics in AVR Studio!

If you’ve programmed in any managed language in Visual Studio, you’d have definitely seen those nagging red squiggles that appear as you type, telling you just how dumb you are, every time you pause typing. Might not be everyone’s cup of tea, but I’ve personally found them very useful; they save quite a few compile-groan-swear-fix-compile cycles. So I decided to write a similar “squiggly generator” for AVR Studio 5, the product that I’m working on with a bunch of other guys. Naggy is what I call it, and it is a VSIX extension that installs into AVR Studio 5. You … Continue reading Naggy : Live compiler diagnostics in AVR Studio!

Ramble – a Ruby sentence generator

There was an interesting problem at work recently, where I basically wanted to generate a whole lot of C language expressions for testing purposes. Rather than generating them by hand, I wondered if I could somehow feed the C language grammar into a program and get most, if not all, possible valid expressions out of it. Of course, I knew that a syntactically valid program might not necessarily by semantically valid, but I figured that I could eliminate or transform those cases manually. So off I went, using this problem as an opportunity to write some Ruby code. The end … Continue reading Ramble – a Ruby sentence generator

Why you should always dispose DataGridViews

Because your application can crash otherwise, that’s why. While it’s always a good idea to explicitly dispose everything that is disposable, we can usually get away without disposing UI controls because a. Complex controls aren’t created often – a single instance is often reused. b. The finalizer kicks in and saves the day. If you’re creating multiple instances of a System.Windows.Forms.DataGridView, however, watch out, because (b) doesn’t happen at all if you don’t call Dispose or otherwise cause the control to be destroyed. When debugging a software crash dump recently, I found that the crash occurred because there was an … Continue reading Why you should always dispose DataGridViews

When a C++ destructor did not run – Part 2

In the previous post, we saw that linking a C++ static library compiled with /EHs to a mixed mode application prevented the destructor from running when an exception is thrown. Here’s the sample project that demonstrates the behavior, in case you aren’t convinced. This is the code inside the library. 1: C::C() 2: { 3: cout << "Constructed" << endl; 4: } 5:  6: C::~C() 7: { 8: cout << "Destructed" << endl; 9: } 10:  11: void SomeFunc() 12: { 13: C c; 14: throw std::exception("Gone"); 15: } And this is the code consuming the library. 1: int main(array<System::String … Continue reading When a C++ destructor did not run – Part 2

When a C++ destructor did not run – Part 1

Consider this piece of C++ code. 1: using namespace std; 2:  3: class C 4: { 5: public: 6: C() 7: { 8: cout << “Constructed”; 9: } 10: ~C() 11: { 12: cout << “Destructed”; 13: } 14: }; 15:  16: void SomeFunc() 17: { 18: C c; 19: throw std::exception(“Gone”); 20: } If you know any C++ at all, you’ll know that when SomeFunc returns, both “Constructed” and “Destructed” will be printed to the console. That is because C++ guarantees that the destructor of an object created on the stack will always run when control leaves the scope, … Continue reading When a C++ destructor did not run – Part 1

Wrong compiler warning? Or Not?

This piece of code results in a warning. 1: class Test 2: { 3: public static void Main() 4: { 5: object obj = "Senthil"; 6: string myName = "Senthil"; 7: Console.WriteLine(obj == myName); 8: } 9: } The compiler says “warning CS0252: Possible unintended reference comparison; to get a value comparison, cast the left hand side to type ‘string’” on line 7. The compiler is right; it has no way of knowing that obj is actually a string. It infers the static type of obj to be System.Object, and if you’ve done any programming in C# at all, you’ll … Continue reading Wrong compiler warning? Or Not?

When what you set is not what you get : SetEnvironmentVariable and getenv

Mixing SetEnvironmentVariable and getenv is asking for trouble, as we recently found to our dismay (and exasperation!). It took some serious debugging to figure out the actual problem – let’s see if you can figure it out. Here’s some C++/CLI code that uses getenv to read the value of an environment variable and print it to the console. 1: public ref class EnvironmentVariablePrinter 2: { 3: public: 4: void PrintEnv(String ^key) 5: { 6: IntPtr ptr = Marshal::StringToHGlobalAnsi(key); 7: const char *ckey = (const char *)ptr.ToPointer(); 8: const char *cval = getenv(ckey); 9: string val = cval == NULL ? … Continue reading When what you set is not what you get : SetEnvironmentVariable and getenv

Anonymous methods as event handlers – Part 2

The previous post discussed having anonymous methods as event handlers and ended with a question – why doesn’t unsubscription work while subscription works out alright? Vivek got the answer spot on – the way the C# compiler handles and translates anonymous methods is the reason. Here’s the code involved. 1: public void Initialize() 2: { 3: control.KeyPressed += IfEnabledThenDo(control_KeyPressed); 4: control.MouseMoved += IfEnabledThenDo(control_MouseMoved); 5: } 6:  7: public void Destroy() 8: { 9: control.KeyPressed -= IfEnabledThenDo(control_KeyPressed); 10: control.MouseMoved -= IfEnabledThenDo(control_MouseMoved); 11: } 12:  13: public EventHandler<Control.ControlEventArgs> IfEnabledThenDo(EventHandler<Control.ControlEventArgs> actualAction) 14: { 15: return (sender, args) => { if (args.Control.Enabled) actualAction(sender, args); … Continue reading Anonymous methods as event handlers – Part 2

Anonymous methods as event handlers – Part 1

The syntactic sugar offered by anonymous methods makes them great candidates for writing event handlers; together with smart type inference, they reduce the amount of code written by an order of magnitude. And that’s without considering the power offered by closures. With event handlers, closures allow you to kind of “stuff” extra parameters into the handler, without changing the actual number of formal parameters. This blog post shows a situation where an anonymous method acting as an event handler makes code simpler, and then goes on to show a gotcha with un-subscription and anonymous methods. Here’s a simple Control class … Continue reading Anonymous methods as event handlers – Part 1

Remoting IEnumerables

You’re writing this really cool and innovative class to calculate the first hundred thousand natural numbers. You think about the API, and you realize that returning an array of the numbers a) might take a long time to complete, and b) is going to cause memory usage to spike up like mad. So you decide to stream them instead, returning an IEnumerable<T> instance instead of an array. Being a crack C# developer, you use the incredibly powerful yield keyword, rather than rolling your own implementation of the IEnumerable<T> interface. 1: class MyCoolMathEngine 2: { 3: public IEnumerable<int> GetFirstHundredThousandNaturalNumbers() 4: { … Continue reading Remoting IEnumerables