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. Other Discussions
  3. Clever Code
  4. Bad use of ASSERT...

Bad use of ASSERT...

Scheduled Pinned Locked Moved Clever Code
helpannouncementcomdebugging
20 Posts 13 Posters 7 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.
  • N Offline
    N Offline
    Nibu babu thomas
    wrote on last edited by
    #1

    Well I am in the midst of developing a personal project. Now there is a certain control which should be displayed when my application starts up. The type of control is decided at runtime. Debug version all is fine, the control shows up as it should. Release version the control fails to show up. I spent about one week scratching my head as to what was happening. I did not find any trouble with my code. After digging and tracing my code for sometime I found out that the control was not getting created at all in Release version. Further digging of my code led to this statement.

    ASSERT( CreateControl() );

    The problem was here. Since in non debug versions `ASSERT` expands to nothing. So any statement inside it does not get expanded. I knew that ASSERT had such an issue but still I did this mistake again. So the moral of the story is not to write any assignment or executable code inside `ASSERT` instead in such cases `VERIFY` should be used.


    Nibu thomas A Developer Programming tips[^]  My site[^]

    J K P M C 7 Replies Last reply
    0
    • N Nibu babu thomas

      Well I am in the midst of developing a personal project. Now there is a certain control which should be displayed when my application starts up. The type of control is decided at runtime. Debug version all is fine, the control shows up as it should. Release version the control fails to show up. I spent about one week scratching my head as to what was happening. I did not find any trouble with my code. After digging and tracing my code for sometime I found out that the control was not getting created at all in Release version. Further digging of my code led to this statement.

      ASSERT( CreateControl() );

      The problem was here. Since in non debug versions `ASSERT` expands to nothing. So any statement inside it does not get expanded. I knew that ASSERT had such an issue but still I did this mistake again. So the moral of the story is not to write any assignment or executable code inside `ASSERT` instead in such cases `VERIFY` should be used.


      Nibu thomas A Developer Programming tips[^]  My site[^]

      J Offline
      J Offline
      Johann Gerell
      wrote on last edited by
      #2

      Nibu babu thomas wrote:

      the moral of the story is not to write any assignment or executable code inside ASSERT instead in such cases VERIFY should be used.

      Generalization: Never assert an expression with side effects.

      -- The Blog: Bits and Pieces

      E 1 Reply Last reply
      0
      • N Nibu babu thomas

        Well I am in the midst of developing a personal project. Now there is a certain control which should be displayed when my application starts up. The type of control is decided at runtime. Debug version all is fine, the control shows up as it should. Release version the control fails to show up. I spent about one week scratching my head as to what was happening. I did not find any trouble with my code. After digging and tracing my code for sometime I found out that the control was not getting created at all in Release version. Further digging of my code led to this statement.

        ASSERT( CreateControl() );

        The problem was here. Since in non debug versions `ASSERT` expands to nothing. So any statement inside it does not get expanded. I knew that ASSERT had such an issue but still I did this mistake again. So the moral of the story is not to write any assignment or executable code inside `ASSERT` instead in such cases `VERIFY` should be used.


        Nibu thomas A Developer Programming tips[^]  My site[^]

        K Offline
        K Offline
        Kevin McFarlane
        wrote on last edited by
        #3

        Nibu babu thomas wrote:

        So the moral of the story is not to write any assignment or executable code inside ASSERT instead in such cases VERIFY should be used.

        Yes, I've used this quite often especially around MFC API UI calls, which often return BOOL for control creation, updates, etc.

        Kevin

        N 1 Reply Last reply
        0
        • K Kevin McFarlane

          Nibu babu thomas wrote:

          So the moral of the story is not to write any assignment or executable code inside ASSERT instead in such cases VERIFY should be used.

          Yes, I've used this quite often especially around MFC API UI calls, which often return BOOL for control creation, updates, etc.

          Kevin

          N Offline
          N Offline
          Nibu babu thomas
          wrote on last edited by
          #4

          Kevin McFarlane wrote:

          Yes, I've used this quite often especially around MFC API UI calls, which often return BOOL for control creation, updates, etc.

          The problem is that people keep looking at the wrong places... I was purely lucky in catching this bug. :phew:


          Nibu thomas A Developer Programming tips[^]  My site[^]

          1 Reply Last reply
          0
          • N Nibu babu thomas

            Well I am in the midst of developing a personal project. Now there is a certain control which should be displayed when my application starts up. The type of control is decided at runtime. Debug version all is fine, the control shows up as it should. Release version the control fails to show up. I spent about one week scratching my head as to what was happening. I did not find any trouble with my code. After digging and tracing my code for sometime I found out that the control was not getting created at all in Release version. Further digging of my code led to this statement.

            ASSERT( CreateControl() );

            The problem was here. Since in non debug versions `ASSERT` expands to nothing. So any statement inside it does not get expanded. I knew that ASSERT had such an issue but still I did this mistake again. So the moral of the story is not to write any assignment or executable code inside `ASSERT` instead in such cases `VERIFY` should be used.


            Nibu thomas A Developer Programming tips[^]  My site[^]

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            I know one company that did this, and got away with it because they only shipped debug builds. Talk about clueless.

            Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world." Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that." Deja View - the feeling that you've seen this post before.

            P PJ ArendsP C 3 Replies Last reply
            0
            • P Pete OHanlon

              I know one company that did this, and got away with it because they only shipped debug builds. Talk about clueless.

              Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world." Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that." Deja View - the feeling that you've seen this post before.

              P Offline
              P Offline
              Pierre Leclercq
              wrote on last edited by
              #6

              Pete O`Hanlon wrote:

              they only shipped debug builds

              :omg: Stunning!

              P 1 Reply Last reply
              0
              • P Pierre Leclercq

                Pete O`Hanlon wrote:

                they only shipped debug builds

                :omg: Stunning!

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                How do you talk to rocket scientists like that?

                Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world." Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that." Deja View - the feeling that you've seen this post before.

                1 Reply Last reply
                0
                • P Pete OHanlon

                  I know one company that did this, and got away with it because they only shipped debug builds. Talk about clueless.

                  Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world." Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that." Deja View - the feeling that you've seen this post before.

                  PJ ArendsP Offline
                  PJ ArendsP Offline
                  PJ Arends
                  wrote on last edited by
                  #8

                  Of course you ship debug builds. Debug builds work, but the stupid compiler does not know how to make release builds. Release builds always crash as soon as they are run. Release builds are just faulty and should never, ever be used.


                  You may be right
                  I may be crazy
                  -- Billy Joel --

                  Within you lies the power for good, use it!!!

                  Within you lies the power for good; Use it!

                  A P 2 Replies Last reply
                  0
                  • PJ ArendsP PJ Arends

                    Of course you ship debug builds. Debug builds work, but the stupid compiler does not know how to make release builds. Release builds always crash as soon as they are run. Release builds are just faulty and should never, ever be used.


                    You may be right
                    I may be crazy
                    -- Billy Joel --

                    Within you lies the power for good, use it!!!

                    A Offline
                    A Offline
                    Alois Kraus
                    wrote on last edited by
                    #9

                    PJ Arends wrote:

                    Release builds are just faulty and should never, ever be used.

                    Are you waiting for someone to take this statement serious to start a flame war? ;P Yours, Alois Kraus

                    1 Reply Last reply
                    0
                    • PJ ArendsP PJ Arends

                      Of course you ship debug builds. Debug builds work, but the stupid compiler does not know how to make release builds. Release builds always crash as soon as they are run. Release builds are just faulty and should never, ever be used.


                      You may be right
                      I may be crazy
                      -- Billy Joel --

                      Within you lies the power for good, use it!!!

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #10

                      Too true, and how naive I was to expect otherwise.:-D

                      Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world." Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that." Deja View - the feeling that you've seen this post before.

                      1 Reply Last reply
                      0
                      • P Pete OHanlon

                        I know one company that did this, and got away with it because they only shipped debug builds. Talk about clueless.

                        Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world." Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that." Deja View - the feeling that you've seen this post before.

                        C Offline
                        C Offline
                        Chris Losinger
                        wrote on last edited by
                        #11

                        i worked at a company that only shipped debug builds.

                        image processing | blogging

                        N 1 Reply Last reply
                        0
                        • N Nibu babu thomas

                          Well I am in the midst of developing a personal project. Now there is a certain control which should be displayed when my application starts up. The type of control is decided at runtime. Debug version all is fine, the control shows up as it should. Release version the control fails to show up. I spent about one week scratching my head as to what was happening. I did not find any trouble with my code. After digging and tracing my code for sometime I found out that the control was not getting created at all in Release version. Further digging of my code led to this statement.

                          ASSERT( CreateControl() );

                          The problem was here. Since in non debug versions `ASSERT` expands to nothing. So any statement inside it does not get expanded. I knew that ASSERT had such an issue but still I did this mistake again. So the moral of the story is not to write any assignment or executable code inside `ASSERT` instead in such cases `VERIFY` should be used.


                          Nibu thomas A Developer Programming tips[^]  My site[^]

                          M Offline
                          M Offline
                          Matt Gerrans
                          wrote on last edited by
                          #12

                          :wtf: That is not an issue with assert at all. It is 100% by design. It is also well known that you should never have working code inside of an assert. In fact, it is the most common example used to illustrate the danger of macros in general. The real lesson to be learned from this is to avoid macros! :mad: Replace the assert macro in your code with an assert() function and watch how much performance of the release version is not changed. Also, that allows you to turn on run-time asserts (with a reg key, etc.) in the release version. Tracing can be handled similarly.

                          Matt Gerrans

                          N 1 Reply Last reply
                          0
                          • M Matt Gerrans

                            :wtf: That is not an issue with assert at all. It is 100% by design. It is also well known that you should never have working code inside of an assert. In fact, it is the most common example used to illustrate the danger of macros in general. The real lesson to be learned from this is to avoid macros! :mad: Replace the assert macro in your code with an assert() function and watch how much performance of the release version is not changed. Also, that allows you to turn on run-time asserts (with a reg key, etc.) in the release version. Tracing can be handled similarly.

                            Matt Gerrans

                            N Offline
                            N Offline
                            Nibu babu thomas
                            wrote on last edited by
                            #13

                            Matt Gerrans wrote:

                            That is not an issue with assert at all. It is 100% by design.

                            Exactly, the title says so... "Bad Use of Assert...". It doesn't say ASSERT is bad.

                            Matt Gerrans wrote:

                            It is also well known that you should never have working code inside of an assert.

                            True. But sometimes you know that you won't do it but still you do it. Since you know that you won't do it hence you won't look there. That's what happened with me. I couldn't believe that I did this...:doh::sigh::doh:

                            Matt Gerrans wrote:

                            The real lesson to be learned from this is to avoid macros!

                            This is not true. These bugs happens due to carelessness. Macros are for a purpose and they satisfy that purpose well. Any other use leads to disaster.

                            Matt Gerrans wrote:

                            Also, that allows you to turn on run-time asserts (with a reg key, etc.) in the release version. Tracing can be handled similarly.

                            Thanks for this info. :)


                            Nibu thomas A Developer Programming tips[^]  My site[^]

                            1 Reply Last reply
                            0
                            • C Chris Losinger

                              i worked at a company that only shipped debug builds.

                              image processing | blogging

                              N Offline
                              N Offline
                              Nibu babu thomas
                              wrote on last edited by
                              #14

                              Chris Losinger wrote:

                              i worked at a company that only shipped debug builds.

                              I am working with a company that only ships Release builds.


                              Nibu thomas A Developer Programming tips[^]  My site[^]

                              1 Reply Last reply
                              0
                              • J Johann Gerell

                                Nibu babu thomas wrote:

                                the moral of the story is not to write any assignment or executable code inside ASSERT instead in such cases VERIFY should be used.

                                Generalization: Never assert an expression with side effects.

                                -- The Blog: Bits and Pieces

                                E Offline
                                E Offline
                                Ennis Ray Lynch Jr
                                wrote on last edited by
                                #15

                                Don't assert logic?


                                On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage

                                J 1 Reply Last reply
                                0
                                • E Ennis Ray Lynch Jr

                                  Don't assert logic?


                                  On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage

                                  J Offline
                                  J Offline
                                  Johann Gerell
                                  wrote on last edited by
                                  #16

                                  No, I don't consider the value of an assertable expression to be a side effect, if that's what you mean. The side effect obviously occurs when the state of one or more inputs to the expression have changed when the expression was evaluated.

                                  -- The Blog: Bits and Pieces

                                  1 Reply Last reply
                                  0
                                  • N Nibu babu thomas

                                    Well I am in the midst of developing a personal project. Now there is a certain control which should be displayed when my application starts up. The type of control is decided at runtime. Debug version all is fine, the control shows up as it should. Release version the control fails to show up. I spent about one week scratching my head as to what was happening. I did not find any trouble with my code. After digging and tracing my code for sometime I found out that the control was not getting created at all in Release version. Further digging of my code led to this statement.

                                    ASSERT( CreateControl() );

                                    The problem was here. Since in non debug versions `ASSERT` expands to nothing. So any statement inside it does not get expanded. I knew that ASSERT had such an issue but still I did this mistake again. So the moral of the story is not to write any assignment or executable code inside `ASSERT` instead in such cases `VERIFY` should be used.


                                    Nibu thomas A Developer Programming tips[^]  My site[^]

                                    C Offline
                                    C Offline
                                    charlieg
                                    wrote on last edited by
                                    #17

                                    Nibu - did that once... *once*, will never do it again. It's like writing if (i = 1) { }; You learn to look for these things....

                                    Charlie Gilley Will program for food... Whoever said children were cheaper by the dozen... lied. My son's PDA is an M249 SAW. My other son commutes in an M1A2 Abrams

                                    1 Reply Last reply
                                    0
                                    • N Nibu babu thomas

                                      Well I am in the midst of developing a personal project. Now there is a certain control which should be displayed when my application starts up. The type of control is decided at runtime. Debug version all is fine, the control shows up as it should. Release version the control fails to show up. I spent about one week scratching my head as to what was happening. I did not find any trouble with my code. After digging and tracing my code for sometime I found out that the control was not getting created at all in Release version. Further digging of my code led to this statement.

                                      ASSERT( CreateControl() );

                                      The problem was here. Since in non debug versions `ASSERT` expands to nothing. So any statement inside it does not get expanded. I knew that ASSERT had such an issue but still I did this mistake again. So the moral of the story is not to write any assignment or executable code inside `ASSERT` instead in such cases `VERIFY` should be used.


                                      Nibu thomas A Developer Programming tips[^]  My site[^]

                                      D Offline
                                      D Offline
                                      devvvy
                                      wrote on last edited by
                                      #18

                                      Good stuff :)

                                      Norman Fung

                                      1 Reply Last reply
                                      0
                                      • N Nibu babu thomas

                                        Well I am in the midst of developing a personal project. Now there is a certain control which should be displayed when my application starts up. The type of control is decided at runtime. Debug version all is fine, the control shows up as it should. Release version the control fails to show up. I spent about one week scratching my head as to what was happening. I did not find any trouble with my code. After digging and tracing my code for sometime I found out that the control was not getting created at all in Release version. Further digging of my code led to this statement.

                                        ASSERT( CreateControl() );

                                        The problem was here. Since in non debug versions `ASSERT` expands to nothing. So any statement inside it does not get expanded. I knew that ASSERT had such an issue but still I did this mistake again. So the moral of the story is not to write any assignment or executable code inside `ASSERT` instead in such cases `VERIFY` should be used.


                                        Nibu thomas A Developer Programming tips[^]  My site[^]

                                        S Offline
                                        S Offline
                                        Soundman32 2
                                        wrote on last edited by
                                        #19

                                        Can I suggest you look up VERIFY? VERIFY(CreateControl()); This will do the same as assert in debug builds, but in release builds will compile to: CreateControl(); Which gives you the best of both worlds.

                                        N 1 Reply Last reply
                                        0
                                        • S Soundman32 2

                                          Can I suggest you look up VERIFY? VERIFY(CreateControl()); This will do the same as assert in debug builds, but in release builds will compile to: CreateControl(); Which gives you the best of both worlds.

                                          N Offline
                                          N Offline
                                          Nibu babu thomas
                                          wrote on last edited by
                                          #20

                                          Soundman32.2 wrote:

                                          Can I suggest you look up VERIFY? VERIFY(CreateControl()); This will do the same as assert in debug builds, but in release builds will compile to: CreateControl(); Which gives you the best of both worlds.

                                          Yeah I've already mentioned that towards the end of my message. Thanks. :) :)


                                          Nibu thomas A Developer Programming tips[^]  My site[^]

                                          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