Announcement

Collapse
No announcement yet.

Is this a bug or am i missing something?

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

  • Is this a bug or am i missing something?

    I ran across this problem and thought maybe someone can shed some light as to what i am doing wrong. I was able to boil down the code to one that shows the issue. To see the error happen just single step while in the debugger. I am using PowerBasic for Windows 10.03.

    Code:
    #COMPILE EXE
    #DIM ALL
    
    FUNCTION PBMAIN () AS LONG
        LOCAL i as LONG
    
        'This Works
        DIM b(0) AS LONG
        b(0) = FIX(RND * 100)
        REDIM b(1)
        FOR i = 0 to 1
          b(i) = FIX(RND * 100)
        NEXT i
    
        'This FAILS
        DIM a AS iTestObject
        LET a = CLASS "cTestObject"
    
        a.DoSomething
    
    END FUNCTION
    
    CLASS cTestObject
      INSTANCE TestData() AS LONG
    
      'Create
      CLASS METHOD InitData
    
        REDIM TestData(1) AS LONG
    
      END METHOD
    
      CLASS METHOD CREATE()
    
        'This one works
        DIM TestData(0)
    
      END METHOD
    
      CLASS METHOD Destroy()
    
        ERASE TestData()
    
      END METHOD
    
      INTERFACE iTestObject
        INHERIT IUNKNOWN
    
        METHOD DoSomething()
          LOCAL i AS LONG
    
          'Does not REDIM the TestData
          ME.InitData
    
          FOR i = 0 TO 1
            '"Subscript/Pointer out of range" when i = 1
            TestData(i) = FIX(RND * 100)
          NEXT i
    
        END METHOD
    
    
      END INTERFACE
    END CLASS
    "Alone we can do so little; together we can do so much." - Helen Keller

    PowerBasic On Wikipedia, PowerBasic On FreeBase, and "PowerBasic Users Guide" on Wikia

  • #2
    Weird. Change REDIM TestData(1) AS LONG to REDIM TestData(1) and it works.

    All that Help says is "The AS type clause is optional, but recommended for the purposes of clarity."

    There appears to be no reason for the different behaviour.

    You should report it to support@powerbasic.com.
    Last edited by Stuart McLachlan; 16 Apr 2012, 06:06 PM.
    --
    [URL="http://www.camcopng.com"]CAMCo - Applications Development & ICT Consultancy[/URL][URL="http://www.hostingpng.com"]
    PNG Domain Hosting[/URL]

    Comment


    • #3
      Works for me, 10.03 on XP SP3.

      Comment


      • #4
        Chris,

        Does it still work if you insert
        #DEBUG ERROR ON
        #DEBUG DISPLAY ON

        (that way you don't need to use the single step debugger to see the issue)
        --
        [URL="http://www.camcopng.com"]CAMCo - Applications Development & ICT Consultancy[/URL][URL="http://www.hostingpng.com"]
        PNG Domain Hosting[/URL]

        Comment


        • #5
          @Stuart, yes, no error is raised either way.

          Comment


          • #6
            Chris,

            On Vista with 10.03
            Using this:
            Code:
            #COMPILE EXE
            #DIM ALL
            #DEBUG ERROR ON
            #DEBUG DISPLAY ON
            
            FUNCTION PBMAIN () AS LONG
            10    LOCAL i AS LONG
            
            20    DIM a AS iTestObject
            30    LET a = CLASS "cTestObject"
            
            40    a.DoSomething
            
            END FUNCTION
            
            CLASS cTestObject
            50  INSTANCE TestData() AS LONG
                INSTANCE msg AS STRING
              'Create
              CLASS METHOD InitData
            
            60    REDIM TestData(1) AS LONG
            msg +=  "During InitData:" & STR$(UBOUND(TestData)) & $CRLF
            
              END METHOD
            
              CLASS METHOD CREATE()
            
                'This one works
             70   DIM TestData(0)
               msg = "Create, after Dim:" & STR$(UBOUND(TestData)) & $CRLF
              END METHOD
            
              CLASS METHOD DESTROY()
            
             80   ERASE TestData()
            
              END METHOD
            
              INTERFACE iTestObject
                INHERIT IUNKNOWN
            
                METHOD DoSomething()
             90     LOCAL i AS LONG
            
                  'Does not REDIM the TestData
             100     ME.InitData
            msg +=  "After InitData:" & STR$(UBOUND(TestData))
            ? msg
             110     FOR i = 0 TO 1
                    '"Subscript/Pointer out of range" when i = 1
             120       TestData(i) = FIX(RND * 100)
             130     NEXT i
            
                END METHOD
             END INTERFACE
             END CLASS
            I get
            Create, after Dim: 0
            During InitData: 1
            After InitData: 0
            Followed by:
            ---------------------------
            Error
            ---------------------------
            Untrapped Error #9 (Subscript/Pointer out of range)
            has occurred following execution of 120
            If I comment out the AS LONG after the REDIM, I get
            ---------------------------
            PowerBASIC
            Create, after Dim: 0
            During InitData: 1
            After InitData: 1
            Can you confirm that you get a different result on XP?
            --
            [URL="http://www.camcopng.com"]CAMCo - Applications Development & ICT Consultancy[/URL][URL="http://www.hostingpng.com"]
            PNG Domain Hosting[/URL]

            Comment


            • #7
              No, I now get exactly the same results as you. Strange - the only difference that I can think of since my first run is that last night I had compiled several times from the same IDE session.

              Presumably the AS LONG qualifier is forcing a local declaration, the correct syntax would be
              Code:
              redim TestData(1) as instance long
              Not sure if this is documented and no time to look this morning.

              Comment


              • #8
                Presumably the AS LONG qualifier is forcing a local declaration, the correct syntax would be

                redim TestData(1) as instance long
                Yes, this is the correct syntax.
                Forum: http://www.jose.it-berater.org/smfforum/index.php

                Comment


                • #9
                  Originally posted by Chris Holbrook View Post
                  No, I now get exactly the same results as you. Strange - the only difference that I can think of since my first run is that last night I had compiled several times from the same IDE session.

                  Presumably the AS LONG qualifier is forcing a local declaration, the correct syntax would be
                  Code:
                  redim TestData(1) as instance long
                  Not sure if this is documented and no time to look this morning.
                  Thank You Chris

                  Yep that seems to be the error. While searching for documentation of the "AS type" usage I found a neat little switch that caught the error. "#UNIQUE VAR [ON|OFF]". The default is off however when turned on the error in the code was spotted at compile time with
                  Code:
                  A likely conflict is in C:\Projects\local\testobj.bas:
                    Line 28:   INSTANCE TestData() AS LONG
                  Just wish it pointed to the REDIM also, so it would read.
                  Code:
                  A likely conflict is in C:\Projects\local\testobj.bas:
                    Line 28:   INSTANCE TestData() AS LONG
                    Line 33:    REDIM TestData(1) AS LONG
                  I think "#UNIQUE VAR ON" will be added to my default flags for now on.
                  "Alone we can do so little; together we can do so much." - Helen Keller

                  PowerBasic On Wikipedia, PowerBasic On FreeBase, and "PowerBasic Users Guide" on Wikia

                  Comment


                  • #10
                    That counts as a documentation error to me.

                    Looks like Help needs to be changed from:
                    REDIM [PRESERVE] array([subscripts]) [AS type] [AT address] [, ...]

                    to


                    REDIM [PRESERVE] array([subscripts]) [AS
                    [GLOBAL | INSTANCE | LOCAL | STATIC ]type] [AT address] [, ...]
                    (i.e. similar to the syntax shown for DIM)
                    --
                    [URL="http://www.camcopng.com"]CAMCo - Applications Development & ICT Consultancy[/URL][URL="http://www.hostingpng.com"]
                    PNG Domain Hosting[/URL]

                    Comment


                    • #11
                      I made a few changes. What's occuring is the ReDim TestData(2) As Long is creating a new LOCAL array TestData2 inside the InitData routine. As mentioned before if you drop AS LONG or add INSTANCE it works correctly.

                      Code:
                      #Compile Exe
                      #Dim All
                      #Debug Error On
                      #Debug Display On
                      Function PBMain () As Long
                      10    Local i As Long
                      20    Dim a As iTestObject
                      30    Let a = Class "cTestObject"
                      40    a.DoSomething
                      End Function
                      Class cTestObject
                      50  Instance TestData() As Long
                          Instance msg As String
                        'Create
                        Class Method InitData
                      60    ReDim TestData(2) As Long
                      msg +=  "During InitData:" & Str$(UBound(TestData)) & $CrLf
                            TestData(0) = 2
                            TestData(1) = 2
                            TestData(2) = 2
                        End Method
                        Class Method Create()
                          'This one works
                       70   Dim TestData(1)
                            TestData(0)=1
                            TestData(1)=1
                         msg = "Create, after Dim:" & Str$(UBound(TestData)) & $CrLf
                        End Method
                        Class Method Destroy()
                       80   Erase TestData()
                        End Method
                        Interface iTestObject
                          Inherit IUnknown
                          Method DoSomething()
                       90     Local i As Long
                            'Does not REDIM the TestData
                       100     ME.InitData
                      msg +=  "After InitData:" & Str$(UBound(TestData))
                      ? msg
                       110     For i = 0 To 2
                              '"Subscript/Pointer out of range" when i = 1
                      120            MsgBox Str$(TestData(i))
                       '120       TestData(i) = FIX(RND * 100)
                       130     Next i
                          End Method
                       End Interface
                       End Class
                      LarryC
                      Website
                      Sometimes life's a dream, sometimes it's a scream

                      Comment


                      • #12
                        So we learn that the "scope" part of the (RE)DIM statement actually works, and that without it, the scope becomes LOCAL by default if a type specifier is used.

                        The documentation states:

                        The AS type clause is optional, but recommended for the purposes of clarity
                        I too would describe that as a documentation issue, because the AS type cluase is also steering the declaration in regards to the scope.

                        Comment


                        • #13
                          I've lodged it as a "Document Improvement" request at http://www.powerbasic.com/support/request.html
                          --
                          [URL="http://www.camcopng.com"]CAMCo - Applications Development & ICT Consultancy[/URL][URL="http://www.hostingpng.com"]
                          PNG Domain Hosting[/URL]

                          Comment


                          • #14
                            >I too would describe that as a documentation issue
                            >I've lodged it as a "Document Improvement" request

                            How do you know it's the documentation which is in error?

                            Maybe the compiler itself is not operating as it was designed!

                            Me, I would think a simple "REDIM ArrayName(number)" of an array defined with "INSTANCE arrayname()" should not create a "new" array with scope LOCAL. That's how REDIM() works everywhere else!

                            If I wanted a new array with the same name but a different scope I'd try.... well, I'd try counseling before I resorted to pharmaceutical products.

                            MCM
                            Michael Mattias
                            Tal Systems Inc.
                            Racine WI USA
                            mmattias@talsystems.com
                            http://www.talsystems.com

                            Comment


                            • #15
                              > Me, I would think a simple "REDIM ArrayName(number)" of an array defined with "INSTANCE arrayname()" should not create a "new" array with scope LOCAL. That's how REDIM() works everywhere else!

                              Andi it works in this situation the same way. The problem is that if you change it to "REDIM ArrayName(number) AS type", it does create a new array with scope LOCAL.
                              This is undocumented (Help just says that "as type" is optional).
                              --
                              [URL="http://www.camcopng.com"]CAMCo - Applications Development & ICT Consultancy[/URL][URL="http://www.hostingpng.com"]
                              PNG Domain Hosting[/URL]

                              Comment


                              • #16
                                It has always worked this way. If you are unsure you can always add;

                                #UNIQUE VAR ON

                                to your source file.
                                Scott Slater
                                Summit Computer Networks, Inc.
                                www.summitcn.com

                                Comment


                                • #17
                                  The problem is that if you change it to "REDIM ArrayName(number) AS type", it does create a new array with scope LOCAL.
                                  This is undocumented (Help just says that "as type" is optional).
                                  ISTR this "phenomenon" being reported by others in the context of "procedures, " not just in the context of "Interfaces" ... anything other than simple 'REDIM arrayname(new_bounds) [ end of statement]' will create a 'new' array using the type and scope specified.

                                  I just, out of force of habit now that everything works, always do this:
                                  Code:
                                   Procedure Foo (params) 
                                  
                                    LOCAL   arrayname() AS type      ' define the symbol 
                                  
                                   ..... 
                                    REDIM arrayname (number)        '  create the array so it exists and may be used 
                                                                    ' datatype and scope already defined above
                                  Apparently I won't have to change anything should I desire to create my own INTERFACEs with METHODs.

                                  MCM
                                  Michael Mattias
                                  Tal Systems Inc.
                                  Racine WI USA
                                  mmattias@talsystems.com
                                  http://www.talsystems.com

                                  Comment

                                  Working...
                                  X