home     software     articles     blog     resume

When a C++ destructor did not run – Part 2
Sat, 05 Dec 2009 10:24:24 GMT

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: }

More...

When a C++ destructor did not run – Part 1
Fri, 04 Dec 2009 06:01:00 GMT

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: }

More...

Wrong compiler warning? Or Not?
Tue, 01 Dec 2009 05:27:57 GMT

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: }

More...

When what you set is not what you get : SetEnvironmentVariable and getenv
Tue, 13 Oct 2009 06:14:12 GMT

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 ? "" : string(cval);
  10:  
  11:              cout << "Key is " << ckey << " and value is " << val;
  12:  
  13:              Marshal::FreeHGlobal(ptr);    
  14:         }
  15: };

More...

Just Attach
Thu, 17 Sep 2009 20:17:27 GMT

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

More...