I am using the code from a book to convert a double vector to an mxArray mxPointer
the code is as follows.
but it gives "assertion failed" error on the line of
mxSetPr(mxPointerBuffer, mxPointerTemp);
fhs pointer passed to vector_check is invalid and does not appear to have come from any of the following routines: vector_malloc, vector_calloc, vector_realloc maxMaloc*.mxCalloc*, mxRealloc*...
but the two lines of
mxPointerTemp = Marshal.AllocHGlobal(dbVector.Length * Marshal.SizeOf(dbVector[0]));
Marshal.Copy(dbVector, 0, mxPointerTemp, dbVector.Length);
have done this tasks, right?
it seems before I run it on 32bit Matlab without problem, now it runs on 64bit Matlab and the problems come out.
I could not find a way to solve this problem.
Any one give a hint?
thanks!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
[DllImport("libmx.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr mxCreateDoubleMatrix(int m, int n, mxComplexity flag);
public enum mxComplexity
{ mxREAL, mxCOMPLEX };
[DllImport("libmx.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void mxSetPr(IntPtr pa, IntPtr preal);
[DllImport("libmx.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr mxDuplicateArray(IntPtr pa);
[DllImport("libmx.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr mxCreateCharArray(int pa, int[] dim);
public Form1()
{
InitializeComponent();
double[] db_A = { 1.1, 2.2, 3.3 };
IntPtr mx_A = (IntPtr)null;
mx_A = double2mxArray_vectorRowReal(db_A);
}
public static IntPtr double2mxArray_vectorRowReal(double[] dbVector)
{
int vectorSize;
vectorSize = dbVector.GetUpperBound(0) - dbVector.GetLowerBound(0) + 1;
IntPtr mxPointer = (IntPtr)0;
IntPtr mxPointerBuffer = (IntPtr)0;
IntPtr mxPointerTemp;
int col = vectorSize;
double[] dbVectorBuffer;
dbVectorBuffer = new double[col];
try
{
mxPointerBuffer = mxCreateDoubleMatrix(1, col, mxComplexity.mxREAL);
}
catch
{
System.Console.Write("We cannot create a pointer mxArray. Please check again.");
return (IntPtr)0;
}
mxPointerTemp = Marshal.AllocHGlobal(dbVector.Length * Marshal.SizeOf(dbVector[0]));
Marshal.Copy(dbVector, 0, mxPointerTemp, dbVector.Length);
mxSetPr(mxPointerBuffer, mxPointerTemp);
mxPointer = mxDuplicateArray(mxPointerBuffer);
Marshal.FreeHGlobal(mxPointerTemp);
return mxPointer;
}
}
}
What I have tried:
I have debugged step by step and check the pointer and it seems nothing is null, or rempty. the poiter has been assigned with a memory.