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. Datatype Misalignment Exception

Datatype Misalignment Exception

Scheduled Pinned Locked Moved Clever Code
data-structureshelpquestionannouncement
12 Posts 4 Posters 3 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.
  • J Johann Gerell

    I got into some trouble when some object I was using released one of its shared resources of type SomeSharedResource* in its destructor. The line --refCount_ below caused a datatype misalignment system exception. The type of refCount_ is UINT, and I couldn't immediately realize what harm -- might do to a UINT. However, finding a repro and examining the call stack before --refCount_ got executed revealed the cause of the problem. Can you guess what it was?

    void SomeSharedResource::Release()
    {
    --refCount_;
    .
    .
    .
    }

    As always, the first correct answer wins a pink toaster.

    -- Time you enjoy wasting is not wasted time - Bertrand Russel

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

    Was refCount a pointer by any chance? If so, decrementing it would cause it to move out of alignment.

    Deja View - the feeling that you've seen this post before.

    J 1 Reply Last reply
    0
    • P Pete OHanlon

      Was refCount a pointer by any chance? If so, decrementing it would cause it to move out of alignment.

      Deja View - the feeling that you've seen this post before.

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

      Pete O`Hanlon wrote:

      Was refCount a pointer by any chance?

      Nope, a plain UINT, as stated.

      Pete O`Hanlon wrote:

      If so, decrementing it would cause it to move out of alignment

      If it was a pointer, I think the problem would only occur if it was declared as a pointer to something of incompatible size, say a BYTE or WORD, then decremented and after that cast to UINT* and dereferenced.

      -- Time you enjoy wasting is not wasted time - Bertrand Russel

      P 1 Reply Last reply
      0
      • J Johann Gerell

        I got into some trouble when some object I was using released one of its shared resources of type SomeSharedResource* in its destructor. The line --refCount_ below caused a datatype misalignment system exception. The type of refCount_ is UINT, and I couldn't immediately realize what harm -- might do to a UINT. However, finding a repro and examining the call stack before --refCount_ got executed revealed the cause of the problem. Can you guess what it was?

        void SomeSharedResource::Release()
        {
        --refCount_;
        .
        .
        .
        }

        As always, the first correct answer wins a pink toaster.

        -- Time you enjoy wasting is not wasted time - Bertrand Russel

        E Offline
        E Offline
        Ed Poore
        wrote on last edited by
        #4

        Was refCount_ set to 0 before the desctructor being called?  Just a guess :~

        J 1 Reply Last reply
        0
        • E Ed Poore

          Was refCount_ set to 0 before the desctructor being called?  Just a guess :~

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

          Nope, but this is not the problem. The new value was actually validated after the decrement (in the dotted lines...). Clue: If the value of refCount_ had been validated before instead of after the decrement, the misalignment cause might have been caught quicker.

          -- Time you enjoy wasting is not wasted time - Bertrand Russel

          E 1 Reply Last reply
          0
          • J Johann Gerell

            Pete O`Hanlon wrote:

            Was refCount a pointer by any chance?

            Nope, a plain UINT, as stated.

            Pete O`Hanlon wrote:

            If so, decrementing it would cause it to move out of alignment

            If it was a pointer, I think the problem would only occur if it was declared as a pointer to something of incompatible size, say a BYTE or WORD, then decremented and after that cast to UINT* and dereferenced.

            -- Time you enjoy wasting is not wasted time - Bertrand Russel

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

            Johann Gerell wrote:

            If it was a pointer, I think the problem would only occur if it was declared as a pointer to something of incompatible size, say a BYTE or WORD, then decremented and after that cast to UINT* and dereferenced.

            Of course it would. I've been out of C too long it seems.:-O Time for me to brush up on my pointers again.

            Deja View - the feeling that you've seen this post before.

            1 Reply Last reply
            0
            • J Johann Gerell

              Nope, but this is not the problem. The new value was actually validated after the decrement (in the dotted lines...). Clue: If the value of refCount_ had been validated before instead of after the decrement, the misalignment cause might have been caught quicker.

              -- Time you enjoy wasting is not wasted time - Bertrand Russel

              E Offline
              E Offline
              Ed Poore
              wrote on last edited by
              #7

              Ok since it could be any number of values I'm going to hazard a guess that it was 1 going into the destructor and thus any code later on used a refCount of 0? (I'm not a C++ programmer so may miss any subtle bugs specific to C++ :->)

              J 1 Reply Last reply
              0
              • J Johann Gerell

                I got into some trouble when some object I was using released one of its shared resources of type SomeSharedResource* in its destructor. The line --refCount_ below caused a datatype misalignment system exception. The type of refCount_ is UINT, and I couldn't immediately realize what harm -- might do to a UINT. However, finding a repro and examining the call stack before --refCount_ got executed revealed the cause of the problem. Can you guess what it was?

                void SomeSharedResource::Release()
                {
                --refCount_;
                .
                .
                .
                }

                As always, the first correct answer wins a pink toaster.

                -- Time you enjoy wasting is not wasted time - Bertrand Russel

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

                Johann Gerell wrote:

                I got into some trouble when some object I was using released one of its shared resources of type SomeSharedResource* in its destructor.

                -- Time you enjoy wasting is not wasted time - Bertrand Russel

                1 Reply Last reply
                0
                • E Ed Poore

                  Ok since it could be any number of values I'm going to hazard a guess that it was 1 going into the destructor and thus any code later on used a refCount of 0? (I'm not a C++ programmer so may miss any subtle bugs specific to C++ :->)

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

                  See my clue at the next-to-top level.

                  -- Time you enjoy wasting is not wasted time - Bertrand Russel

                  E 1 Reply Last reply
                  0
                  • J Johann Gerell

                    See my clue at the next-to-top level.

                    -- Time you enjoy wasting is not wasted time - Bertrand Russel

                    E Offline
                    E Offline
                    Ed Poore
                    wrote on last edited by
                    #10

                    Probably something to do with the fact that it's a pointer?  (Not the refCount_).  Can't be more specific sorry :-O

                    1 Reply Last reply
                    0
                    • J Johann Gerell

                      I got into some trouble when some object I was using released one of its shared resources of type SomeSharedResource* in its destructor. The line --refCount_ below caused a datatype misalignment system exception. The type of refCount_ is UINT, and I couldn't immediately realize what harm -- might do to a UINT. However, finding a repro and examining the call stack before --refCount_ got executed revealed the cause of the problem. Can you guess what it was?

                      void SomeSharedResource::Release()
                      {
                      --refCount_;
                      .
                      .
                      .
                      }

                      As always, the first correct answer wins a pink toaster.

                      -- Time you enjoy wasting is not wasted time - Bertrand Russel

                      M Offline
                      M Offline
                      Michael Dunn
                      wrote on last edited by
                      #11

                      That line actually means --(this->refCount_) so the this pointer is misaligned or garbage.

                      --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

                      J 1 Reply Last reply
                      0
                      • M Michael Dunn

                        That line actually means --(this->refCount_) so the this pointer is misaligned or garbage.

                        --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

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

                        Michael Dunn wrote:

                        the this pointer is misaligned or garbage

                        Tadaaa! Actually - both! ;) The SomeSharedResource pointer was never initialized in this particular scenario, so when checked for non-NULLness before calling SomeResource::Release, it's value was 0x3b3b3b3b, which is not only non-NULL but also clearly an unaligned pointer value. So the actual problem was not really that refCount_ was unaligned in some way, but the fact that this was unaligned. Had it been aligned, but unitialized, this problem would have been harder to find, since that likely just would have resulted in garbage and maybe later trashed data.

                        -- Time you enjoy wasting is not wasted time - Bertrand Russel

                        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