Announcement

Collapse
No announcement yet.

Gpf

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Gpf

    Hi all,

    Can someone please explain why this code gpf?
    Not always...just sometimes. I double checked
    not going beyond array bonds and the pointers > 0,
    ...same thing. I'm not using strptr(string) but varptr(string).
    I isolated this offending snippet. Actually, the type shown
    below has many other pointers, but just the string ptr gpf.
    The weird thing, it sometimes gpf when reading, others when
    trying to assign a string. There is something I'm doing wrong?

    Thanks in advance

    Gus

    Code:
    type main_type
       nstr as string ptr
       idx as long
    end type
    
    function mainthing() as long     '<- main entrance
      local mtp as main_type
      local pmtp as dword
      local names() as string
      redim names(512)
      mtp.nstr = varptr(names(0))
      mtp.idx = 3
      pmtp = varptr(mtp)
      
      
    ' then some processing...ie
      redim preserve names(ubound(names)+1)
      array insert names(), "this thing"
      '...
      names(28) = "this other guy"
    
    ' then a call to do some other processing
      dothings(pmst)
      '...
      '...
        
    end function
    
    function dothings(byval mtp as main_type ptr) as long
      local st as string
      st = "damngpf"
      @[email protected][@mtp.idx] = st   '<---THIS GPFs
      st =  @[email protected][@mtp.idx]  '<---THIS also GPFs
    
      @[email protected][1] = st   '<---THIS GPFs
      st =  @[email protected][1]  '<---THIS also GPFs!!!
    
    end function

  • #2
    By glancing at the code I see that your REDIM PRESERVE and ARRAY INSERT statements may be reallocating the array data, making the previously obtained pointer invalid. Try obtaining the names(0) pointer after the REDIM, to remedy.
    kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

    Comment


    • #3
      ARRAY INSERT won't change the address of element zero, but REDIM will for sure.

      i.e, Kev's fix will work.
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        You right.

        It is not supossed string handles doesn't moves?

        Thank you.

        Comment


        • #5
          Originally posted by Gus del Solar View Post
          You right.

          It is not supossed string handles doesn't moves?

          Thank you.
          REDIM causes the block of memory allocated to hold the array elements to move, so the address of the string handle (the "STRING PTR") changes on REDIM.

          MCM
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            I know redim moves the actual string data, but I'm using VARPTR and supossed (evidently wrong) handles was fixed.
            Anyway, just refreshing the pointers after any redim did the trick.
            This thing flyes now

            Thank you both.

            Comment


            • #7
              Originally posted by Gus del Solar View Post
              I know redim moves the actual string data, but I'm using VARPTR and supossed (evidently wrong) handles was fixed.
              Anyway, just refreshing the pointers after any redim did the trick.
              Actually, the string *data* do not move at all during a REDIM PRESERVE of a STRING array...i.e, the STRPTR would still be valid. In this case the STRPTR is useless (you need the VARPTR) but it would remain valid.

              (I think that's correct: it wouldn't make sense to rebuild all the strings when not necessary).

              But since it's working correctly now, do you really care?

              MCM
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                In a REDIM PRESERVE:

                1- The array descriptor never moves.
                2- The string data never moves.
                3- The string handles which point to the string data never change.
                4- The block of memory where the string handles are stored may change if the block can not be expanded to the new size.
                5- VARPTR() of an array element may change.
                6- STRPTR() of an array element will not change.

                Bob Zale
                PowerBASIC Inc.

                Comment


                • #9
                  Bob,

                  I think it would be very helpful to include these comments on the clause "REDIM PRESERVE" in the Help file.

                  Best Regards,
                  "The trouble with quotes on the Internet is that you can never know if they are genuine." - Abraham Lincoln.

                  Comment

                  Working...
                  X