Today I got adventurous and decided to learn a new PowerBASIC statement - Type Set. New for me anyway! All I wanted to do was find the simplest way to copy the members of a type from one instance to another without having to do a member by member copy - and that is definitely the way to do it.
Emboldened by my success at this rather advanced achievement I figured I'd give PowerBASIC's new GlobalMem Alloc/Free a try. What I needed to do was copy data from a type to another type of the exact same kind but the latter type would be in allocated memory and I'd be using a 'type ptr'. I can't seem to get the memory to free when I do this. Here is my first example with the output afterwards...
Output from above
As you can see iRet is the return PowerBASIC provides for the memory de-allocation and from the docs it follows the standard convention of using zero as a successful release. Note that the Type Set statement was successful in copying the TYPE data to the template in memory.
According to the docs the variable after the 'To' in the GlobalMem statement can only be a LONG or DWORD, and that is what I used above. Below, however, is another iteration of the program where I simply did away with the dwPtr variable and told GlobalMem to just put the memory directly in an everywhere variable of TYPE Third. It actually works but the memory doesn't release as in the 1st example...
This works quite good, but doesn't release the memory either. Finally I tried an example with just 32 bit numbers and that is the only example I was able to get to work...
This is leaving me with the impression I ought to stick to my trusty old Api calls as this new statement doesn't seem to work well with user defined types??? What am I doing wrong?
Emboldened by my success at this rather advanced achievement I figured I'd give PowerBASIC's new GlobalMem Alloc/Free a try. What I needed to do was copy data from a type to another type of the exact same kind but the latter type would be in allocated memory and I'd be using a 'type ptr'. I can't seem to get the memory to free when I do this. Here is my first example with the output afterwards...
Code:
#Compile Exe #Dim All Type First a As Long b As Long c As Long End Type Type Second a As Long b As Long c As Long End Type Type Third a As Long b As Long c As Long End Type Function PBMain() As Long Local here As First Local there As Second Local everywhere As Third Ptr Local iRet As Long Local dwVar As Dword here.a=1 : here.b=2 : here.c=3 Type Set there=here Print here.a, here.b, here.c Print there.a, there.b, there.c Print "Sizeof(Third) = " Sizeof(Third) GlobalMem Alloc Sizeof(Third) To dwVar everywhere=dwVar Print "dwVar = " dwVar Print "everywhere = " everywhere Type Set @everywhere=there Print @everywhere.a, @everywhere.b, @everywhere.c GlobalMem Free dwVar To iRet Print "iRet = " iRet Waitkey$ PBMain=0 End Function
Code:
1 2 3 1 2 3 Sizeof(Third) = 12 dwVar = 8519684 everywhere = 8519684 1 2 3 iRet = 8519684
According to the docs the variable after the 'To' in the GlobalMem statement can only be a LONG or DWORD, and that is what I used above. Below, however, is another iteration of the program where I simply did away with the dwPtr variable and told GlobalMem to just put the memory directly in an everywhere variable of TYPE Third. It actually works but the memory doesn't release as in the 1st example...
Code:
#Compile Exe #Dim All Type First a As Long b As Long c As Long End Type Type Second a As Long b As Long c As Long End Type Type Third a As Long b As Long c As Long End Type Function PBMain() As Long Local here As First Local there As Second Local everywhere As Third Ptr Local iRet As Long here.a=1 : here.b=2 : here.c=3 Type Set there=here Print here.a, here.b, here.c Print there.a, there.b, there.c Print "Sizeof(Third) = " Sizeof(Third) GlobalMem Alloc Sizeof(Third) To everywhere Type Set @everywhere=there Print @everywhere.a, @everywhere.b, @everywhere.c GlobalMem Free everywhere To iRet Print "iRet = " iRet Waitkey$ PBMain=0 End Function
Code:
#Compile Exe #Dim All Function PBMain() As Long Register i As Long Local dwPtr As Dword Ptr GlobalMem Alloc 16*Sizeof(dwPtr) To dwPtr Print "dwPtr = " dwPtr : Print For i=0 To 15 @dwPtr[i]=i Next i For i=0 To 15 Print i, Varptr(@dwPtr[i]), @dwPtr[i] Next i Print GlobalMem Free dwPtr To dwPtr Print "dwPtr = " dwPtr Waitkey$ PBMain=0 End Function 'dwPtr = 8519684 ' ' 0 8519684 0 ' 1 8519688 1 ' 2 8519692 2 ' 3 8519696 3 ' 4 8519700 4 ' 5 8519704 5 ' 6 8519708 6 ' 7 8519712 7 ' 8 8519716 8 ' 9 8519720 9 ' 10 8519724 10 ' 11 8519728 11 ' 12 8519732 12 ' 13 8519736 13 ' 14 8519740 14 ' 15 8519744 15 ' 'dwPtr = 0
Comment