?
Solved

Memory Corruption of pointer variable in windows 2003 server.

Posted on 2009-02-14
4
Medium Priority
?
269 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

0
Comment
Question by:vijayarajp
  • 2
4 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 23658249
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
 

Author Comment

by:vijayarajp
ID: 23666677
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
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 23714244

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
 

Accepted Solution

by:
vijayarajp earned 0 total points
ID: 23731963
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question