BUG: getenv() Fails when Called in Ctor for QuickWin App

ID: Q116213

1.00 1.50 MS-DOS kbprg kbbuglist

The information in this article applies to:

SYMPTOMS

Calling the getenv() function inside a constructor for a global object may not work for a QuickWin Application. Using the same code for an MS-DOS application works properly.

RESOLUTION

A possible workaround is to move the call to getenv() out of the constructor and into a member function. Call this member after stepping inside main(). (See comments in the sample code below.)

STATUS

Microsoft has confirmed this to be a bug in the products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

The sample code below demonstrates how the same code works for an MS-DOS application but fails in a QuickWin Application.

Sample Code

/* Compile options needed for MS-DOS: cl /AM /D_DOS
   Compile options needed for QuickWin: standard
*/ 

   #include <iostream.h>
   #include <stdlib.h>

   class Enviro
   {
   public:
     Enviro();
     void PrintEnviro()
     { cout << s_ << endl; }

     // void MyGetEnv()          // uncomment as workaround
     // { s_ = getenv("PATH"); }

     static int RefCount;
   private:
     char *s_;
   };

   Enviro::Enviro()
   {
     RefCount++;
     s_ = getenv("PATH");       // enviro1 fails here
     if (!s_)                    // enviro2 works here
       s_ = "Not Found";
   }

   int Enviro::RefCount=0;

   static Enviro enviro1;       // enviro1 ctor is called before main()

   void main(int argc, char *argv[], char *envp[])
   {
      //enviro1.MyGetEnv();  // uncomment as workaround

     Enviro enviro2;          // enviro2 ctor is called after main()

     cout << "print: enviro2" << endl;
     enviro2.PrintEnviro();

     cout << "print: enviro1" << endl;
     enviro1.PrintEnviro();
   }

Additional reference words: 1.00 1.50 runtime KBCategory: kbprg kbbuglist KBSubcategory: CRTIss Keywords : kb16bitonly

Last Reviewed: July 23, 1997