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

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

Just Attach

Attach to what? To the process you want to debug, of course. How many developers attach to and debug arbitrary processes running on their machines? Very few, I’d imagine. And I’d think that even those few people typically prefer Windbg or an equivalent debugger to the one supplied with Visual Studio. Which means that aside from ASP .NET developers, almost all of us using Visual Studio attach to the application that we are working on and nothing else. To attach 1. You summon the “Attach to Process” dialog by hitting Ctrl + Alt + P. Or if you are a … Continue reading Just Attach

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