views:

81

answers:

1

hello stack,

this is really strange, i have an array in delphi and fill it with directX matrices. then i get the pointer to the first element and pass it via com to c# managed code:

function TMPlugTransformInPin.GetMatrixPointer(out SliceCount: Integer; out
    ValueP: Int64): HResult;
var
  matrices: array of TD3DMatrix;
  i: Integer;
begin
  SliceCount := UserSliceCount;

  //make a temp array of all matrices
  SetLength(matrices, SliceCount);
  for i := 0 to SliceCount - 1 do
    matrices[i] := FTransformManager.ModelMatrix[i];

  //return a pointer to the first matrices cell [0,0]
  if SliceCount = 0 then
    ValueP := 0
  else
    ValueP := Int64(@matrices[0]);

  Result := S_OK;
end;

on managed side the code looks like this:

if (IsChanged)
            {
                int sliceCount;
                long source;

                FTransformIn.GetMatrixPointer(out sliceCount, out source);
                SliceCount = sliceCount;

                System.Diagnostics.Debug.WriteLine(source);

                if (FSliceCount > 0)
                    Marshal.Copy(new IntPtr(source), FData, 0, FData.Length);
            }

this all works for an array size up to 4136 (66176 floats), but for higher counts, the pointer to the array gets invalid..

any ideas?

thanks a lot! thalm

+6  A: 

Your matrices variable is a local dynamic array. At the end of the function, the array's reference count is reduced to zero and the array is destroyed. The pointer you stored in ValueP is invalid no matter how big the array was. The fact that it appears to work for smaller values just means you're unlucky. (If you were lucky, the code would have crashed every time, which is a bigger clue that your code is wrong than when it only crashes sometimes.)

You need to find some other way to manage the lifetime of that array. It will need to belong to something bigger than just that function. Maybe you can return the array itself from that function, or maybe you can make the matrices variable be a field of the class instead of a local variable. Or you could just return @FTransformManager.ModelMatrix[0].

Rob Kennedy
uff, simple one, shame on me.. thanks!
thalm