Page 1 of 1

Wrong ellement in array pointer

Posted: Sat Jan 02, 2010 8:38 pm
by RE-A
I have a strange problem with an array pointer returned from a procedure. I think this is a bug but I'm not sure.

The b(0)\a element is not correct returned from the procedure until you uncomment the "Debug..." line :shock:

PB version 4.40 and Win Vista 32bit.

Code: Select all

Structure struc
  a.s
  b.l
EndStructure

Procedure.i proc()
Dim b.struc(2)

  b(0)\a = "ZZZZZZZZ" ;not the correct value
  b(0)\b = 4
  b(1)\a = "AAAAAAAA"
  b(1)\b = 2
  b(2)\a = "BBBBBBBB"
  b(2)\b = 3

  ;Debug "OK -> " + b(0)\a ;Uncomment this line and the element is correct returned.

  ProcedureReturn @b.struc()
EndProcedure

Dim test.struc(2)
*ptr.i = proc()
test() = *ptr

For iCount = 0 To 2
  Debug test(iCount)\a + " -> " + Str(test(iCount)\b)
Next

Re: Wrong ellement in array pointer

Posted: Sat Jan 02, 2010 8:46 pm
by srod
Well, the array you declare within the procedure is local to the procedure; meaning that it's memory is released imediately prior to the procedure returning. Your code is thus flawed because you are returning a pointer to an array which has been released.

Declare the array as static and it should run fine.

Re: Wrong ellement in array pointer

Posted: Sat Jan 02, 2010 8:53 pm
by RE-A
thx, srod. That did it.

Re: Wrong ellement in array pointer

Posted: Sat Jan 02, 2010 8:54 pm
by srod
To be honest, it might be better simply creating your array in your main code and then passing the array as a parameter to the function etc.

Re: Wrong ellement in array pointer

Posted: Sun Jan 03, 2010 9:09 am
by RE-A
I could narrow my problem down to the little code I posted but this function is part of a dll I'm creating which main purpose is to collect and transfer large array's of data structures from a datasection.