Over the HEAP: Tracking down a heap error
-
Hey code mercenaries, I'm new to MFC, but I've been hired to update a Visual C++/MFC application. Mostly going pretty well, but it wouldn't be any fun if there weren't at least a few hickups, right? Basically the application that I am working on is a quasi complicated app with about a thousand classes or so, which uses numerous pages/tabs/views/lists to display some database info. I am adding a new SQL query class to a payment calculator class so i can do some new calcs on the db info. This is causing a Heap Error: ///// HEAP CORRUPTION DETECTED: after client block (#4531) at 0x015E51B0. CRT detected that the application wrote to memory after end of the heap buffer. ///// The error occurs when I am closing the application, but I have tracked the source down to the following line of code: //// CSetspIsAttachedAuthValid* m_pSetspIsAttachedAuthValid2; //// Basically the error occurs when this line is inserted into the code, and does not occur when it is commented out, regardless of whether I actually use this member or not in any way. Just adding the above line to the header file causes the heap error, which occurs on close of the application. What could possibly be causing this? Is there some reason why I can't add a member to an existing class in this application with out causing an error? i have added other members to this same class with out trouble. HUGE thanks - Jason
-
Hey code mercenaries, I'm new to MFC, but I've been hired to update a Visual C++/MFC application. Mostly going pretty well, but it wouldn't be any fun if there weren't at least a few hickups, right? Basically the application that I am working on is a quasi complicated app with about a thousand classes or so, which uses numerous pages/tabs/views/lists to display some database info. I am adding a new SQL query class to a payment calculator class so i can do some new calcs on the db info. This is causing a Heap Error: ///// HEAP CORRUPTION DETECTED: after client block (#4531) at 0x015E51B0. CRT detected that the application wrote to memory after end of the heap buffer. ///// The error occurs when I am closing the application, but I have tracked the source down to the following line of code: //// CSetspIsAttachedAuthValid* m_pSetspIsAttachedAuthValid2; //// Basically the error occurs when this line is inserted into the code, and does not occur when it is commented out, regardless of whether I actually use this member or not in any way. Just adding the above line to the header file causes the heap error, which occurs on close of the application. What could possibly be causing this? Is there some reason why I can't add a member to an existing class in this application with out causing an error? i have added other members to this same class with out trouble. HUGE thanks - Jason
You've probably exposed an existing problem. Is the block number always #4531? Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
Hey code mercenaries, I'm new to MFC, but I've been hired to update a Visual C++/MFC application. Mostly going pretty well, but it wouldn't be any fun if there weren't at least a few hickups, right? Basically the application that I am working on is a quasi complicated app with about a thousand classes or so, which uses numerous pages/tabs/views/lists to display some database info. I am adding a new SQL query class to a payment calculator class so i can do some new calcs on the db info. This is causing a Heap Error: ///// HEAP CORRUPTION DETECTED: after client block (#4531) at 0x015E51B0. CRT detected that the application wrote to memory after end of the heap buffer. ///// The error occurs when I am closing the application, but I have tracked the source down to the following line of code: //// CSetspIsAttachedAuthValid* m_pSetspIsAttachedAuthValid2; //// Basically the error occurs when this line is inserted into the code, and does not occur when it is commented out, regardless of whether I actually use this member or not in any way. Just adding the above line to the header file causes the heap error, which occurs on close of the application. What could possibly be causing this? Is there some reason why I can't add a member to an existing class in this application with out causing an error? i have added other members to this same class with out trouble. HUGE thanks - Jason
@largeinsd wrote:
Basically the error occurs when this line is inserted into the code, and does not occur when it is commented out...
Which means you've moved memory around just enough to expose an existing problem. What happens if you insert some other type of pointer (in place of
CSetspIsAttachedAuthValid
), or maybe aDWORD
variable?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Hey code mercenaries, I'm new to MFC, but I've been hired to update a Visual C++/MFC application. Mostly going pretty well, but it wouldn't be any fun if there weren't at least a few hickups, right? Basically the application that I am working on is a quasi complicated app with about a thousand classes or so, which uses numerous pages/tabs/views/lists to display some database info. I am adding a new SQL query class to a payment calculator class so i can do some new calcs on the db info. This is causing a Heap Error: ///// HEAP CORRUPTION DETECTED: after client block (#4531) at 0x015E51B0. CRT detected that the application wrote to memory after end of the heap buffer. ///// The error occurs when I am closing the application, but I have tracked the source down to the following line of code: //// CSetspIsAttachedAuthValid* m_pSetspIsAttachedAuthValid2; //// Basically the error occurs when this line is inserted into the code, and does not occur when it is commented out, regardless of whether I actually use this member or not in any way. Just adding the above line to the header file causes the heap error, which occurs on close of the application. What could possibly be causing this? Is there some reason why I can't add a member to an existing class in this application with out causing an error? i have added other members to this same class with out trouble. HUGE thanks - Jason
Try enabling the page heap[^] for your process. Follow these steps: 1. Download and install WinDBG[^]. 2. Select “Start”->“All Programs”->“Debugging Tools for Windows”->“Global Flags”. 3. Select the “Image File” tab. 4. In the “Image: (TAB to refresh)” edit control enter the name of your app then press TAB. Just the name with the extension; not the full path. 5. Tick the following: - “Enable page heap” - “Enable heap tail checking” - “Enable heap free checking” - “Enable heap parameter checking” - “Enable heap validation on call” - “Create user mode stack trace database” 6. Press “Apply”. 7. Debug your application. Any debugger will do but with WinDBG you have access to the stack traces of allocations via the
!heap –p –a
command, for example. When a heap problem is detected a breakpoint will be generated. 8. When done un-tick all the options you ticked, press “Apply” then dismiss GFlags. This step is important as if it’s skipped all applications named as entered in step 4 will run with the page heap enabled. Note that when using the page heap your application will run much slower than normal and consume way more memory. It’s good to have a beefy machine to do such tests; and such tests should be ran regularly on all applications you develop as part of regular testing activities. If I find a part of my application that’s too slow with the page heap enabled I optimize the memory allocation in that region.Steve