We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Memory Corruption of pointer variable in windows 2003 server.

Medium Priority
290 Views
Last Modified: 2013-11-26
Hi,
I have a singleton class definition like this in VC8.

struct Layer
{
    CBrush *pbrush;
    CPen *ppen;
};

typedef map<int, Layer *>  Collection;

class Base
{
  public:
              static int ref_cnt;
              Base *getInstance ();
              void initialize ();
              void cleanup ();
   private:
                  static Base *base=NULL;
                  BYTE *Image;
                  Collection   data;
                  Collection *pdata;
                   Base () { Imgage = NULL;}
           
}
Base * Base::getInstance ()
{
      if (! base)
          base = new Base ();
      return base;
}
void Base::initialize ()
{
      if ( data.size() > 0)
         cleanup ();
      Layer *layer = new Layer ();
      layer->brush = new CBrush ();
      layer->pen = new CPen ();
     cout << 'Brush " << layer->brush;
      data[1] = layer;
}

void Base::cleanup()
{
         if (data.size() > 0)
         {
              Layer *layer = data [1];
               cout << 'Brush " << layer->brush;
              delete layer->brush; // Gets ACCESS_VIOLATION if run only in windows 2003 server.
              delete layer->pen;
              data.removeElement (1);
              delete layer;
        }

         }

};

void main ()
{
Base *b = Base::getInstance ();
b->initialize ();
b->initialize ();
}

}

In windows 2003 server, the application throws ACCESS_VIOLATION exception at the line "delete layer->brush;". In Windows 2000 it works fine.

When we printed the brush object pointer from layer...

1. In initialize ....
   we get the value as 0x0635AFF8.
1.In Cleanup
   we get the value as 0x0635AF00.

The first byte of the pointer value always becomes 00. It happens only in windows 2003 server. So during the delete operation, we always get ACCESS_VIOLATION exception thrown.

It would be helpful if someone can throw some light on this behaviour.

struct Layer
{ 
    CBrush *pbrush;
    CPen *ppen;
};
 
typedef map<int, Layer *>  Collection;
 
class Base
{
  public:
              static int ref_cnt;
              Base *getInstance ();
              void initialize ();
              void cleanup ();
   private:
                  static Base *base;
                  BYTE *Image;
                  Collection   data;
                  Collection *pdata; 
                   Base () { Imgage = NULL;}
           
}
 Base * Base::getInstance ()
{
      if (! base)
          base = new Base ();
      return base;
}
void Base::initialize ()
{
      if ( data.size() > 0)
         cleanup ();
      Layer *layer = new Layer ();
      layer->brush = new CBrush ();
      layer->pen = new CPen ();
     cout << 'Brush " << layer->brush;
      data[1] = layer;
}
 
void Base::cleanup()
{
         if (data.size() > 0)
         {
              Layer *layer = data [1];
               cout << 'Brush " << layer->brush;
              delete layer->brush; // Gets ACCESS_VIOLATION if run only in windows 2003 server.
              delete layer->pen;
              data.removeElement (1);
              delete layer;
        }
 
         }
 
};
void main ()
{
Base *b = Base::getInstance ();
b->initialize ();
b->initialize ();
}

Open in new window

Comment
Watch Question

CERTIFIED EXPERT

Commented:
Hi vijayarajp,

that seems quite strange, IMO code looks ok ... unfortunateley I don't have a Windows Server 2003 for testing.

Just for interest:

Does the same happen if you do create/destroy pen/brush in constructor/destructor, i.e.:

> struct Layer
> {
>    CBrush *pbrush;
>    CPen *ppen;
>    Layer() : pbrush( new CBrush ), ppen( new CPen ) {}
>    ~Layer() { delete pbrush; delete ppen; }
> };

...
// in Base::initialize
> Layer *layer = new Layer ();
> cout << 'Brush " << layer->brush;
...
// in Base::cleanup()
> Layer *layer = data [1];
> cout << 'Brush " << layer->brush;
> data.removeElement (1);
> delete layer;

BTW: Do you compile this code on Win2003 Server? Do you have installed the latest VS2008 ServicePack? Have you tried compiling the code without code-optimization?

ZOPPO

Author

Commented:
Hi Zoppo,

Thanks for the response.

1. It is complied in Windows 2000.
2. Let me verify if I have all the latest VS2008 patches installed.
3. I will try removing code-optimization.

I will get back with response. Thanks again for the suggestions.
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:

The collection looks very odd - I assume this is stripped down code else why do you need a map, there is only ever going to be one 'layer' with the code you have
Hi Zoppo ands AndyAincow,

We have fixed the issue. It is because of one pointer to char variable used without getting memory allocated in the code flow. When the program writes a value to this pointer vy dereferencing it corrupts the memory subsequently allocated in the different part of the code.

We used "Windbg"  and attach the process and used the "ba -r4 <addr>" command to set break point when this address location is read/written. It prints the stack trace and we were able to walk through the code and identifed the bug and fixed it.

Thanks again for your time in responding to this question.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.