Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help with Heap Corruption "Critical error detected c0000374"

Help with Heap Corruption "Critical error detected c0000374"

Scheduled Pinned Locked Moved C / C++ / MFC
helpvisual-studiodebugging
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    ForNow
    wrote on last edited by
    #1

    Hi I am getting a heap corruption as indicated by the message in quotes to my VS output debug window At first I tried gflags from the command line and did a full enable for my application or my image As I read up on gflags it puts a extra block of storage (that's is protected) between every allocation I have what I think is a pretty big machine 64 gb of ram, While running the program I quickly got a message that I was out of storage I am wondering if there is anything I can do programmatically to invoke gflags As I have an idea in what part of the program this happening I allocate storage for a dialog derived object via new upon receiving a tcp/ip message in a modeless dialog I do a create for derived Cdialog for which I previously did the "new" it is after do the create of the dialog and populate the control including allocating via new a Richedit and streaming data to it that something goes wrong all storage is allocated via new I am thinking maybe I can track this down with HeapValidate putting it various pots in this path of code and checking the return code Thanks

    L J 2 Replies Last reply
    0
    • F ForNow

      Hi I am getting a heap corruption as indicated by the message in quotes to my VS output debug window At first I tried gflags from the command line and did a full enable for my application or my image As I read up on gflags it puts a extra block of storage (that's is protected) between every allocation I have what I think is a pretty big machine 64 gb of ram, While running the program I quickly got a message that I was out of storage I am wondering if there is anything I can do programmatically to invoke gflags As I have an idea in what part of the program this happening I allocate storage for a dialog derived object via new upon receiving a tcp/ip message in a modeless dialog I do a create for derived Cdialog for which I previously did the "new" it is after do the create of the dialog and populate the control including allocating via new a Richedit and streaming data to it that something goes wrong all storage is allocated via new I am thinking maybe I can track this down with HeapValidate putting it various pots in this path of code and checking the return code Thanks

      L Offline
      L Offline
      leon de boer
      wrote on last edited by
      #2

      I don't know how to do what you want with heap but there is an obvious way to make the program more deterministic which is to pre-create the dialogs just held in a storage array not in use. It is really dangerous to be creating anything so significant off an event. When you get a message you call your little routine to pass you back the first free dialog which you load and use. When it's finished you put it back in the pool. It's a lot easier to avoid such problems by changing behaviour to determinstic rather than chase the bug itself :-)

      In vino veritas

      F 1 Reply Last reply
      0
      • F ForNow

        Hi I am getting a heap corruption as indicated by the message in quotes to my VS output debug window At first I tried gflags from the command line and did a full enable for my application or my image As I read up on gflags it puts a extra block of storage (that's is protected) between every allocation I have what I think is a pretty big machine 64 gb of ram, While running the program I quickly got a message that I was out of storage I am wondering if there is anything I can do programmatically to invoke gflags As I have an idea in what part of the program this happening I allocate storage for a dialog derived object via new upon receiving a tcp/ip message in a modeless dialog I do a create for derived Cdialog for which I previously did the "new" it is after do the create of the dialog and populate the control including allocating via new a Richedit and streaming data to it that something goes wrong all storage is allocated via new I am thinking maybe I can track this down with HeapValidate putting it various pots in this path of code and checking the return code Thanks

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        The heap corruption indicates that you have an out of bound memory write access (e.g. writing to an array beyond it's size). Running out of memory indicates that you are allocating a lot of memory but not freeing it. For modeless dialogs follow these adviceses:

        • Create a variable and initialise it (assigning NULL or using new)
        • When the dialog should be created the first time check if the variable is NULL and call new if so
        • Create the dialog
        • When finished with the dialog, call DestroyWindow()
        • If the allocated memory is not needed anymore, delete it and assign NULL to the variable
        • When the variable goes out of scope, delete the memory (e.g. in the destructor of the class holding the variable)

        I suggest to check your code for the above (each new must have a corresponding delete, and each CDialog::Create must have a corresponding CDialog::DestroyWindow). Because the memory corruption is a severe error, I suggest to find the source and fix it first. Use a debug build. Then you don't need the GFlags. They are for debugging executable files where you don't have the sources. If you know the code portions (or have a guess) where the heap corruption occurs, you can insert some AfxCheckMemory[^] calls to check the C++ heap.

        Quote:

        it is after do the create of the dialog and populate the control including allocating via new a Richedit and streaming data to it that something goes wrong all storage is allocated via new

        A common error for such cases is forgetting to append a NULL char to the received data before passing them as string and/or forgetting to allocate the additional memory for the NULL char.

        1 Reply Last reply
        0
        • L leon de boer

          I don't know how to do what you want with heap but there is an obvious way to make the program more deterministic which is to pre-create the dialogs just held in a storage array not in use. It is really dangerous to be creating anything so significant off an event. When you get a message you call your little routine to pass you back the first free dialog which you load and use. When it's finished you put it back in the pool. It's a lot easier to avoid such problems by changing behaviour to determinstic rather than chase the bug itself :-)

          In vino veritas

          F Offline
          F Offline
          ForNow
          wrote on last edited by
          #4

          Leon I don't know if that's an option the dialog has a number of controls including a richedit which can be filled with a number of text lines Would going down the path of using the functions in this article help me track down my problem CRT Debug Heap Details[^]

          L 1 Reply Last reply
          0
          • F ForNow

            Leon I don't know if that's an option the dialog has a number of controls including a richedit which can be filled with a number of text lines Would going down the path of using the functions in this article help me track down my problem CRT Debug Heap Details[^]

            L Offline
            L Offline
            leon de boer
            wrote on last edited by
            #5

            This is where MFC obfuscating what is happening is annoying. You are building the dialog currently try not setting the modal flag and don't insert it into the desktop and tell me what happens :). You can make a dialog at any point in the lifecycle of your program, later on you can insert it into the desktop and make it visible and modal because that is how these things are designed to work. The RichEdit control will happily take text, new or added or anything else you want via the message system. You can also talk it by it's control ID the sample below uses the handle.

            CHARRANGE cr;
            cr.cpMin = -1;
            cr.cpMax = -1;
            SendMessage(hwnd, EM_EXSETSEL, 0, (LPARAM)&cr);
            SendMessage(hwnd, EM_REPLACESEL, 0, (LPARAM)some_new_string);

            That is how windows is designed to work. The best bit if you don't use the HasString flag you can just send the pointer in to the text which saves copying and moving the text. If you have a dialog coming up a lot you are expected to do it that way or via the dialog template system so you don't fragment the hell out of the memory. Common dialogs works that way you know FileOpen, FileSave etc. CreateDialogIndirect function (Windows)[^] Creates a modeless dialog box from a dialog box template in memory All you have to do is insert your changes to the text etc via the message system using the control ID's, put it into the desktop or your app and then set the dialog modal and viola it looks just like what you are doing now. I don't have a problem creating the odd dialog on the fly but if it comes up a lot try and at least use windows the way it was intended. The debug thing uses a similar trick but into a multiline text window. They change the malloc stub to post the text messages to the multiline edit window. I think that this is the sample Microsoft uses to show how to do it in MFC. Not much of a sample and doesn't explain why you do it. The template sample does however contain a multiline edit element to show you they are not anything special. VCSamples/VC2010Samples/MFC/general/dlgtempl at master · Microsoft/VCSamples · GitHub[

            F 1 Reply Last reply
            0
            • L leon de boer

              This is where MFC obfuscating what is happening is annoying. You are building the dialog currently try not setting the modal flag and don't insert it into the desktop and tell me what happens :). You can make a dialog at any point in the lifecycle of your program, later on you can insert it into the desktop and make it visible and modal because that is how these things are designed to work. The RichEdit control will happily take text, new or added or anything else you want via the message system. You can also talk it by it's control ID the sample below uses the handle.

              CHARRANGE cr;
              cr.cpMin = -1;
              cr.cpMax = -1;
              SendMessage(hwnd, EM_EXSETSEL, 0, (LPARAM)&cr);
              SendMessage(hwnd, EM_REPLACESEL, 0, (LPARAM)some_new_string);

              That is how windows is designed to work. The best bit if you don't use the HasString flag you can just send the pointer in to the text which saves copying and moving the text. If you have a dialog coming up a lot you are expected to do it that way or via the dialog template system so you don't fragment the hell out of the memory. Common dialogs works that way you know FileOpen, FileSave etc. CreateDialogIndirect function (Windows)[^] Creates a modeless dialog box from a dialog box template in memory All you have to do is insert your changes to the text etc via the message system using the control ID's, put it into the desktop or your app and then set the dialog modal and viola it looks just like what you are doing now. I don't have a problem creating the odd dialog on the fly but if it comes up a lot try and at least use windows the way it was intended. The debug thing uses a similar trick but into a multiline text window. They change the malloc stub to post the text messages to the multiline edit window. I think that this is the sample Microsoft uses to show how to do it in MFC. Not much of a sample and doesn't explain why you do it. The template sample does however contain a multiline edit element to show you they are not anything special. VCSamples/VC2010Samples/MFC/general/dlgtempl at master · Microsoft/VCSamples · GitHub[

              F Offline
              F Offline
              ForNow
              wrote on last edited by
              #6

              Leon I can try to Create the Dialog Box from a Template ::LoadResource however The actual call DialogBoxIndirect has to be when the message is Received from the other computer Via Sockets as that Contains the Data Thanks

              L 1 Reply Last reply
              0
              • F ForNow

                Leon I can try to Create the Dialog Box from a Template ::LoadResource however The actual call DialogBoxIndirect has to be when the message is Received from the other computer Via Sockets as that Contains the Data Thanks

                L Offline
                L Offline
                leon de boer
                wrote on last edited by
                #7

                Yep in the sample these are the key lines dlg.InitModalIndirect((DLGTEMPLATE*)pBuffer); dlg.DoModal(); // tadaaa! this is the line everyone's been waiting for!!! I loved the tadaa in the source comments and it's because at that moment the penny should drop that the template has just been sitting in memory and wont do anything until you execute those two lines. It's much kinder on windows especially in a threaded app and less chance of making mistakes. So when you get the message from your ethernet port all you have to do is execute those two lines and dialog flies out of memory and up on screen. You can actaully do everything else before that any earlier time. Not sure how MFC does this but there is an lParam value that is part of the indirect and usually you pass a struct with your data you want loaded in the dialog handler in as well. It saves having to carry a global with your data.In your case I would imagine it would be a pointer to your payload data recieved. Again saves copy or moving any data. It stuns me dlg.InitModalIndirect doesn't take a second parameter which is what I was expecting and where you would pass in your data pointer. So bit of mystery to me how you do that under MFC. Oh I wonder does dlg.InitModalIndirect have overrides and used as a shim on the indirect call to cater for that. In which case there will be a form of that you can pass a second parameter on.

                In vino veritas

                F 1 Reply Last reply
                0
                • L leon de boer

                  Yep in the sample these are the key lines dlg.InitModalIndirect((DLGTEMPLATE*)pBuffer); dlg.DoModal(); // tadaaa! this is the line everyone's been waiting for!!! I loved the tadaa in the source comments and it's because at that moment the penny should drop that the template has just been sitting in memory and wont do anything until you execute those two lines. It's much kinder on windows especially in a threaded app and less chance of making mistakes. So when you get the message from your ethernet port all you have to do is execute those two lines and dialog flies out of memory and up on screen. You can actaully do everything else before that any earlier time. Not sure how MFC does this but there is an lParam value that is part of the indirect and usually you pass a struct with your data you want loaded in the dialog handler in as well. It saves having to carry a global with your data.In your case I would imagine it would be a pointer to your payload data recieved. Again saves copy or moving any data. It stuns me dlg.InitModalIndirect doesn't take a second parameter which is what I was expecting and where you would pass in your data pointer. So bit of mystery to me how you do that under MFC. Oh I wonder does dlg.InitModalIndirect have overrides and used as a shim on the indirect call to cater for that. In which case there will be a form of that you can pass a second parameter on.

                  In vino veritas

                  F Offline
                  F Offline
                  ForNow
                  wrote on last edited by
                  #8

                  Leon I would assume the same is True for Modeless Dialogs I pass all my parameters once I have allocated the Cdialog Object on the Heap I populated it before doing Create Or Indirect I have another related question can the same HGLOBAL template be used to Create multiple instances of that object Thanks

                  L 1 Reply Last reply
                  0
                  • F ForNow

                    Leon I would assume the same is True for Modeless Dialogs I pass all my parameters once I have allocated the Cdialog Object on the Heap I populated it before doing Create Or Indirect I have another related question can the same HGLOBAL template be used to Create multiple instances of that object Thanks

                    L Offline
                    L Offline
                    leon de boer
                    wrote on last edited by
                    #9

                    Yes any dialog or window can be created from templates. The window one usually catches even seasoned programmers out. If you look at the template call CreateDialogIndirectParam the DLGPROC is optional. What you do is pass in the window handler and use setwindowlong to put it onto the modeless dialog which just happens to have all the style flags of a window. Once it has the handler attached you can just insert it into any APP window or even the desktop because it's a normal window and so you fake CreateDialogIndirectParam returning a fail. Did you notice it's up to you to call DestroyWindow and that is why they don't do it automatically. Sometimes it's useful to lie to the API :-) Yes its a template you can launch multiple copies as it only gets read and all global alloc memory is thread safe. It can't write or move anything as you usually only allocate just enough space to hold the resource. I assume that all still holds true for MFC. Officially the GlobalAlloc etc is now "old" but they have not got around to giving us replacement template system. Most windows programmers are familiar with LoadResource and loading bitmap/icons/menus etc because they are going to draw them on screen alot but using the system for dialogs and windows usually escapes them because they don't need the response of a window like in your example.

                    In vino veritas

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups