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. debugging app to find out which code module doesn't decrease a count

debugging app to find out which code module doesn't decrease a count

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++help
10 Posts 5 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.
  • A Offline
    A Offline
    abiemann
    wrote on last edited by
    #1

    This c++ application has two static functions that are used everywhere (about 130+ calls for each function): incCount() and decCount() At the start of the program the Count = 0, the problem is that once the processing is complete the Count must == 0. The Count can go as high as it needs to, but by the time the application ends each incCount() call must be balanced with a decCount() call. Currently, the Count == 1 by the time the program ends and I must find out where a decCount() isn't called. What is the best way to find out which code module isn't balancing the Count properly ? (this is a C++ 6 application)

    _ A L A R 5 Replies Last reply
    0
    • A abiemann

      This c++ application has two static functions that are used everywhere (about 130+ calls for each function): incCount() and decCount() At the start of the program the Count = 0, the problem is that once the processing is complete the Count must == 0. The Count can go as high as it needs to, but by the time the application ends each incCount() call must be balanced with a decCount() call. Currently, the Count == 1 by the time the program ends and I must find out where a decCount() isn't called. What is the best way to find out which code module isn't balancing the Count properly ? (this is a C++ 6 application)

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      You cannot do this with one variable. This is usually done by creating many instances of a class. The class will internally have functions to increment and decrement. When the class is created you can generate some sort of cookie that is unique across instances. This was we can log which instance in incrementing and decrementing.

      «_Superman_» I love work. It gives me something to do between weekends.
      Microsoft MVP (Visual C++)
      Polymorphism in C

      1 Reply Last reply
      0
      • A abiemann

        This c++ application has two static functions that are used everywhere (about 130+ calls for each function): incCount() and decCount() At the start of the program the Count = 0, the problem is that once the processing is complete the Count must == 0. The Count can go as high as it needs to, but by the time the application ends each incCount() call must be balanced with a decCount() call. Currently, the Count == 1 by the time the program ends and I must find out where a decCount() isn't called. What is the best way to find out which code module isn't balancing the Count properly ? (this is a C++ 6 application)

        A Offline
        A Offline
        abiemann
        wrote on last edited by
        #3

        my current idea is to define the following with #ifdef _DEBUG playing a huge role: in the class: #ifdef _DEBUG static Uint32 incCount (char cModule); static void decCount (char cModule); #else static Uint32 incCount (); static void decCount (); #endif the class where these functions are defined also implements a linked list. For each incCount(char) the char is added to the linked list (if it doesn't yet exist in the list), and for each decCount(char) the char is removed from the linked list. then for each time the functions are called in the code: #ifdef _DEBUG decCount(__FILE__); #else decCount(); #endif At the end of the program I dump what's in the linked list and the __FILE__ that is dumped is where I need to focus on by setting breakpoints. However, this will touch at least 40 files. Is this the best method or is there a better method ? For example, it would be incredible if I had programmatic access to the call stack (in a debug build) so that I would only need to edit this one class and add the source file to the linked list that's next in the stack.

        1 Reply Last reply
        0
        • A abiemann

          This c++ application has two static functions that are used everywhere (about 130+ calls for each function): incCount() and decCount() At the start of the program the Count = 0, the problem is that once the processing is complete the Count must == 0. The Count can go as high as it needs to, but by the time the application ends each incCount() call must be balanced with a decCount() call. Currently, the Count == 1 by the time the program ends and I must find out where a decCount() isn't called. What is the best way to find out which code module isn't balancing the Count properly ? (this is a C++ 6 application)

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          First of all, if you are using multiple threads (explicit ones, or implicit ones such as used by asynchronous events, callback routines, etc), and your counter increment/decrement isn't atomic, it may well be that the number of calls is correct and the value is incorrect. Assuming you have verified that, you will need to differentiate your function calls; an easy way would be to give them one positive integer parameter, using the same value for matching pairs of incCount(id) and decCount(id). Then you want to count the operations for different id values in separate accumulators. There are many ways to do so: - use an array, ranging from 0 to maximum(id); - if that is not doable, use a smaller array with P elements, and use id%P (the modulo operator will map any value of id to a valid index for your array). Special case: if only one decCount() call is missing, you might even try this:

          int count=0;
          int P=32;

          void incCount(int id) {
          count^=1<<(id%P);
          }

          void decCount(int id) {
          count^=1<<(id%P);
          }

          which means each call is toggling one bit in a single counter (assuming P is 32 or less); in the end the bit number of the one non-zero bit gives you the id%P. And you could use additional runs with different P values to find the actual value of id! :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read formatted code with indentation, so please use PRE tags for code snippets.


          I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


          A 1 Reply Last reply
          0
          • L Luc Pattyn

            First of all, if you are using multiple threads (explicit ones, or implicit ones such as used by asynchronous events, callback routines, etc), and your counter increment/decrement isn't atomic, it may well be that the number of calls is correct and the value is incorrect. Assuming you have verified that, you will need to differentiate your function calls; an easy way would be to give them one positive integer parameter, using the same value for matching pairs of incCount(id) and decCount(id). Then you want to count the operations for different id values in separate accumulators. There are many ways to do so: - use an array, ranging from 0 to maximum(id); - if that is not doable, use a smaller array with P elements, and use id%P (the modulo operator will map any value of id to a valid index for your array). Special case: if only one decCount() call is missing, you might even try this:

            int count=0;
            int P=32;

            void incCount(int id) {
            count^=1<<(id%P);
            }

            void decCount(int id) {
            count^=1<<(id%P);
            }

            which means each call is toggling one bit in a single counter (assuming P is 32 or less); in the end the bit number of the one non-zero bit gives you the id%P. And you could use additional runs with different P values to find the actual value of id! :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read formatted code with indentation, so please use PRE tags for code snippets.


            I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


            A Offline
            A Offline
            abiemann
            wrote on last edited by
            #5

            I see, your method uses each bit in P to track a different code module. That's less work than implementing a linked list and I don't have to edit a load of compiler conditionals. EDIT: I counted 43 code modules that need to be ID'd

            modified on Wednesday, May 19, 2010 5:29 PM

            L 1 Reply Last reply
            0
            • A abiemann

              I see, your method uses each bit in P to track a different code module. That's less work than implementing a linked list and I don't have to edit a load of compiler conditionals. EDIT: I counted 43 code modules that need to be ID'd

              modified on Wednesday, May 19, 2010 5:29 PM

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              if you have a limited number of id values, you can count their calls individually. if they are many, you fold them to a smaller value. And you already expect only one mishap, you could take advantage of that. But first of all, you should inspect the potential multi-threading issue; your observation could simply be wrong! I would avoid introducing fancy things (such as your linked list idea), as things in there can also go wrong; especially with a list, you would find it either difficult or expensive to get it thread-safe. Whatever code you add, it would better be thread-safe! :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read formatted code with indentation, so please use PRE tags for code snippets.


              I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


              A 1 Reply Last reply
              0
              • A abiemann

                This c++ application has two static functions that are used everywhere (about 130+ calls for each function): incCount() and decCount() At the start of the program the Count = 0, the problem is that once the processing is complete the Count must == 0. The Count can go as high as it needs to, but by the time the application ends each incCount() call must be balanced with a decCount() call. Currently, the Count == 1 by the time the program ends and I must find out where a decCount() isn't called. What is the best way to find out which code module isn't balancing the Count properly ? (this is a C++ 6 application)

                A Offline
                A Offline
                Aescleal
                wrote on last edited by
                #7

                Hi, If you're using static functions then the functions can only be called from one translation unit - the one that they're defined in. Funnily enough that's the one the balancing won't be happening in. From what you've said there's something a bit more going on - perhaps if you described what you were trying to count someone could give you a suggestion as to how to count it more accurately? Cheers, Ash PS: For example if you're counting objects of a particular class you can use RAII to keep track of your count. If the count's unbalanced because you're not clearing up objects properly then a decent debug heap might be able to give you clue as to what sort of object is still alive when the program terminates. You might have to do something a bit gauche like look at the still allocated block and compare vtable pointers but it's possible.

                1 Reply Last reply
                0
                • L Luc Pattyn

                  if you have a limited number of id values, you can count their calls individually. if they are many, you fold them to a smaller value. And you already expect only one mishap, you could take advantage of that. But first of all, you should inspect the potential multi-threading issue; your observation could simply be wrong! I would avoid introducing fancy things (such as your linked list idea), as things in there can also go wrong; especially with a list, you would find it either difficult or expensive to get it thread-safe. Whatever code you add, it would better be thread-safe! :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  I only read formatted code with indentation, so please use PRE tags for code snippets.


                  I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                  A Offline
                  A Offline
                  abiemann
                  wrote on last edited by
                  #8

                  I went with your idea of passing in an ID. The integer ID is used by the counting functions to track which module the call came from (keeps track with an array). with this method I can track what modules inc's and dec's don't match up.

                  1 Reply Last reply
                  0
                  • A abiemann

                    This c++ application has two static functions that are used everywhere (about 130+ calls for each function): incCount() and decCount() At the start of the program the Count = 0, the problem is that once the processing is complete the Count must == 0. The Count can go as high as it needs to, but by the time the application ends each incCount() call must be balanced with a decCount() call. Currently, the Count == 1 by the time the program ends and I must find out where a decCount() isn't called. What is the best way to find out which code module isn't balancing the Count properly ? (this is a C++ 6 application)

                    R Offline
                    R Offline
                    Roger Allen
                    wrote on last edited by
                    #9

                    Are there the same number of calls to each function in your source code. Assuming each inc cas a corresponding dec, look for the code section without the missing dec. If that cannot be done. I would suggest checking all the dec calss to make sure one cannot be bypassed by an early "return" within the function code. Next I would place breakpoints at given points so you can check the count value at the inc and dec calls. This should allow you to narrow done the rea of code which is not inc/dec correctly. Are you sure its not a threading issue? Is it multithreaded and how are the numbrs being increemnted/decremented? this is where InterlockedIncrement and InterlockedDecrement can be of help.

                    If you vote me down, my score will only get lower

                    A 1 Reply Last reply
                    0
                    • R Roger Allen

                      Are there the same number of calls to each function in your source code. Assuming each inc cas a corresponding dec, look for the code section without the missing dec. If that cannot be done. I would suggest checking all the dec calss to make sure one cannot be bypassed by an early "return" within the function code. Next I would place breakpoints at given points so you can check the count value at the inc and dec calls. This should allow you to narrow done the rea of code which is not inc/dec correctly. Are you sure its not a threading issue? Is it multithreaded and how are the numbrs being increemnted/decremented? this is where InterlockedIncrement and InterlockedDecrement can be of help.

                      If you vote me down, my score will only get lower

                      A Offline
                      A Offline
                      abiemann
                      wrote on last edited by
                      #10

                      Roger, thanks for your reply. my initial problem statement is a little bit simplified... in actuality the two calls vary wildly in the project. For example, 110 inc's and 140 dec's. However, with the aid of some flags and using an array to track which modules are making the calls I have been able to balance the code such that dec count == inc count by the time the executable is finished. I am actually really pleased with the implementation. So much so that I will keep the code in place and let it trigger only when an environment variable is set so that I can debug this problem if it shows up again :-)

                      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