Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

[heap problem]  memory check error ...

Posted on 2007-08-12
24
Medium Priority
?
1,021 Views
Last Modified: 2013-12-14
Hello All:

i have a heap memory problem... something like 'memory check error at 0x02A0EEA4 = 0xFF, should be 0xFD.' our code base is huge so i can't really post it. i have narrowed down where the memory becomes corrupted [maybe / i think] but this makes everything even more confusing. take a look at the following to see what i mean.

widget()
{
...
      _ASSERTE(_CrtCheckMemory());            // no assert!

      m_currentEditRow = -1;                  // declared as a private class variable in the header

      _ASSERTE(_CrtCheckMemory());            // asserts here!
}

now for the really interesting part: the above is declared in the header below...

class widget public: another_widget
{
public:
      widget();
...

private:
      // other member declarations

      int m_currentEditRow;
};

if the declaration for m_currentEditRow stays where it is the assert happenes above, but if i start moving the declaration around the assert happens erratically.\

any help would be greatly appreciated, as i am feeling quite lost.

thanx,
~sean

0
Comment
Question by:loki982
  • 9
  • 8
  • 7
24 Comments
 
LVL 13

Expert Comment

by:josgood
ID: 19679449
One clue is that the memory check message mentions the value 0xff and you're storing -1 into the integer.

The FD will be a guard character, used by malloc or new.

I suggest you may have an allocation that you are overrunning somewhere else.  That is, the problem is not with this class, but with another one.
0
 
LVL 13

Expert Comment

by:josgood
ID: 19679458
When did this problem start and what changed in the code base just before then?
0
 
LVL 3

Author Comment

by:loki982
ID: 19679596
the actual message i'm getting is as follows:
(memory address is variable of course)
memory check error at 0x02A0EEA4 = 0xFF, should be 0xFD.
memory check error at 0x02A0EEA5 = 0xFF, should be 0xFD.
memory check error at 0x02A0EEA6 = 0xFF, should be 0xFD.
memory check error at 0x02A0EEA7 = 0xFF, should be 0xFD.
DAMAGE: after Normal block (#418983) at 0x02A0EC58.

from the documentation i found out that i am overwriting the 'no mans land' buffer (0xFD) in the debug version of new

the problem popped up from a merge between two branches in our source (which should have been done much earlier than it was but needless to say there are tons of changes in there)

is there any way to find out what is being overwritten? if so that would at least narrow down the search
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

by:jkr
ID: 19679617
If the memory address is consitent, set a memory breakpoint on it and you can see in the call stack from where the overwrite happens. Using '_CrtSetAllocHook()', you should be able to track down where the overwritten block was allocated.
0
 
LVL 13

Expert Comment

by:josgood
ID: 19679624
Since the address varies, could we set a breakpoint in new?  jkr, I don't know how to do this exactly -- perhaps you do.

I'm thinking that the buffer may contain recognizable data and that would gtive loki982 an idea where the problem is.
0
 
LVL 3

Author Comment

by:loki982
ID: 19679703
i shut pretty much everything down in the background and its not varying anymore [me == novice ;)]
0
 
LVL 13

Expert Comment

by:josgood
ID: 19679706
That is a very clever move.  Not novice at all.

Since you can turn the problem off, now turn things back on one at a time, or as close to that as you can, until the problem reappears.
0
 
LVL 13

Expert Comment

by:josgood
ID: 19679717
Once you have a module or a thread that, when you turn it on, the problem reappears, turn it back off to prove that you have the right one.

Then turn the module/thread back on, with some code commented out.  The objective is to keep the problem turned off and then enable code, bit by bit, until you've turned the problem back on and you know exactly what code does this.

Then you should have enought data to fix the problem.
0
 
LVL 86

Expert Comment

by:jkr
ID: 19679720
You can set a memory breakpoint in Visual Studio, there's a tab for that on the "Breakpoints" dialog. Prepare for a slow running program then ;o)
0
 
LVL 13

Expert Comment

by:josgood
ID: 19679730
jkr, are you saying set a data breakpoint on widget::m_currentEditRow?
0
 
LVL 3

Author Comment

by:loki982
ID: 19679777
i think i have a workable version of _CrtSetAllocHook() but right now its not doing very much...

int __cdecl MyAllocHook(int a_nAllocType, void * a_pvData, size_t a_nSize, int a_nBlockUse, long a_lRequest, const unsigned char * a_szFileName, int a_nLine)
{...}

looks like it works but szFileName and nLine are null and 0 respectively  :-\

perhaps i thinking about this completely wrong ...but would i redefine 'new' to use __FILE__ and __LINE__? /if so how would i go about doing that?
0
 
LVL 86

Expert Comment

by:jkr
ID: 19679797
Redefining 'new' won't make a big difference, since it ends up calling 'malloc()' sooner or later. Check http://msdn2.microsoft.com/en-us/library/z2zscsc2(VS.80).aspx
0
 
LVL 3

Author Comment

by:loki982
ID: 19679952
indeed it actually looks like the problem was _nh_malloc passing null & 0:

     return _nh_malloc_dbg(nSize, nhFlag, _NORMAL_BLOCK, NULL, 0);

the following seems to be working for now:

#ifdef _DEBUG
#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif

...& now for the tedious part ;)
0
 
LVL 13

Expert Comment

by:josgood
ID: 19679962
Good catch, loki982 !
0
 
LVL 3

Author Comment

by:loki982
ID: 19680245
thanx for the suggestions all
after doing all of the above the debugger gives a much nicer warning:

memory check error at 0x05203954 = 0xFF, should be 0xFD.
memory check error at 0x05203955 = 0xFF, should be 0xFD.
memory check error at 0x05203956 = 0xFF, should be 0xFD.
memory check error at 0x05203957 = 0xFF, should be 0xFD.
DAMAGE: after Normal block (#415952) at 0x05203708.
Normal allocated at file C:\AFT\branches\UptoDateMerge\AFTGUI\widgets\report\src\orderbookwidget.cpp(43).
Normal located at 0x05203708 is 588 bytes long.

hokay so
i did some playing around and found out that anywhere m_currentEditRow is used that point is where the heap gets screwed.
i have removed it completely (it was legacy code anyways) but now this poses the new question...
how could using an integer memory variable do this?
and more importantly does this mean there is some underlying problem somewhere else in the code?
0
 
LVL 86

Expert Comment

by:jkr
ID: 19680275
Outdated/altered headers that use a different alignment or classes having new members that not all of the code can "see"?  Lots of things can cause this.
0
 
LVL 13

Assisted Solution

by:josgood
josgood earned 800 total points
ID: 19680471
Some things to check:
  -  look for duplicated headers.  Different projects in the solution may be referencing different headers that are _almost_ the same
  -  ensure that all parts of the solution are really being built -- you have a project for each DLL and lib
  -  all projects use the same struct alignment (Configuration Properties | C/C++ | Code Generation | Struct Member Alignment)
0
 
LVL 3

Author Comment

by:loki982
ID: 19687299
I have come to a startling discovery

we are using visual c++ 6.0 (and the libs etc)
but in certain places we are using the Platform SDK for windows 2003

if i understand this right... this is bad news bears yes?
0
 
LVL 86

Accepted Solution

by:
jkr earned 1200 total points
ID: 19687336
No, the PSDK also works for VC6. All you have to make sure is that *only* the PSDK headers are used, don't mix them with the ones that come with the original installation.
0
 
LVL 3

Author Comment

by:loki982
ID: 19687355
! problem is we are using qt
and when qt was compiled initially i think only vc6 was installed
0
 
LVL 86

Expert Comment

by:jkr
ID: 19687443
OK, this *could* be a problem. What about rebuilding Qt?
0
 
LVL 3

Author Comment

by:loki982
ID: 19687541
looks like we found a *major* problem
the sdk was included only for a few things (before i even started work)

we are currently starting on a clean box w/fresh installs of everything
0
 
LVL 86

Expert Comment

by:jkr
ID: 19687617
Good luck!
0
 
LVL 3

Author Comment

by:loki982
ID: 19692936
thanx for the help all
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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

578 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