Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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
C#
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!!
C#
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)
        {
            // convert a double vector to an mxArray mxPointer 
            // transfer to an mxArray vector with number of row = 1

            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.
Posted
Updated 23-Feb-16 6:47am
v2
Comments
Richard MacCutchan 23-Feb-16 12:52pm    
From the diagnostic message it appears that the pointer is either not valid, or does not have the correct control fields before it. Try using one of the mentioned allocation calls to see if that resolves the issue. Alternatively, if relevant, rebuild under a 64 bit system.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900