Memory Corruption of pointer variable in windows 2003 server.

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

vijayarajpAsked:
Who is Participating?
 
vijayarajpAuthor Commented:
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.
0
 
ZoppoCommented:
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
0
 
vijayarajpAuthor 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.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:

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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.