Python代码在运行时崩溃,但在调试时却没有崩溃(Ctypes)

时间:2014-05-29 18:37:20

标签: python ctypes

我正在遇到一个非常奇怪的案例,我正在编写一个涉及ctypes的小类。这个类的目标是将一个专有格式的矩阵加载到我必须创建的python结构中(这些矩阵可以有几个核心/层,每个核心/层可以有几个索引,只引用几个元素)矩阵,从而形成子矩阵)。

测试该类的代码是:

import numpy as np
from READS_MTX import mtx
import time

mymatrix=mtx()


mymatrix.load('D:\\MyMatrix.mtx', True)

我创建的课程是这样的:

import os
import numpy as np
from  ctypes import *
import ctypes
import time


def main():
    pass


#A mydll mtx can have several cores
#we need a class to define each core
#and a class to hold the whole file together

class mtx_core:
    def __init__(self):
        self.name=None     #Matrix core name
        self.rows=-1       #Number of rows in the matrix
        self.columns=-1    #Number of columns in the matrix
        self.type=-1       #Data type of the matrix
        self.indexcount=-1 #Tuple with the number of indices for each dimension
        self.RIndex={}     #Dictionary with all indices for the rows
        self.CIndex={}     #Dictionary with all indices for the columns
        self.basedata=None
        self.matrix=None


    def add_core(self, mydll,mat,core):
        nameC=ctypes.create_string_buffer(50)
        mydll.MATRIX_GetLabel(mat,0,nameC)
        nameC=repr(nameC.value)
        nameC=nameC[1:len(nameC)-1]

        #Add the information to the objects' methods
        self.name=repr(nameC)
        self.rows= mydll.MATRIX_GetBaseNRows(mat)
        self.columns=mydll.MATRIX_GetBaseNCols(mat)
        self.type=mydll.MATRIX_GetDataType(mat)
        self.indexcount=(mydll.MATRIX_GetNIndices(mat,0 ),mydll.MATRIX_GetNIndices(mat,0 ))

        #Define the data type Numpy will have according to the data type of the matrix in question
        dt=np.float64
        v=(self.columns*c_float)()

        if self.type==1:
            dt=np.int32
            v=(self.columns*c_long)()
        if self.type==2:
            dt=np.int64
            v=(self.columns*c_longlong)()

        #Instantiate the matrix
        time.sleep(5)
        self.basedata=np.zeros((self.rows,self.columns),dtype=dt)

        #Read matrix and puts in the numpy array
        for i in range(self.rows):
            mydll.MATRIX_GetBaseVector(mat,i,0,self.type,v)
            self.basedata[i,:]=v[:]


        #Reads all the indices for rows and put them in the dictionary
        for i in range(self.indexcount[0]):
            mydll.MATRIX_SetIndex(mat, 0, i)
            v=(mydll.MATRIX_GetNRows(mat)*c_long)()
            mydll.MATRIX_GetIDs(mat,0, v)
            t=np.zeros(mydll.MATRIX_GetNRows(mat),np.int64)
            t[:]=v[:]
            self.RIndex[i]=t.copy()

        #Do the same for columns
        for i in range(self.indexcount[1]):
            mydll.MATRIX_SetIndex(mat, 1, i)
            v=(mydll.MATRIX_GetNCols(mat)*c_long)()
            mydll.MATRIX_GetIDs(mat,1, v)
            t=np.zeros(mydll.MATRIX_GetNCols(mat),np.int64)
            t[:]=v[:]
            self.CIndex[i]=t.copy()



class mtx:
    def __init__(self):
        self.data=None
        self.cores=-1
        self.matrix={}
        mydll=None

    def load(self, filename, verbose=False):
        #We load the DLL and initiate it
        mydll=cdll.LoadLibrary('C:\\Program Files\\Mysoftware\\matrixDLL.dll')
        mydll.InitMatDLL()

        mat=mydll.MATRIX_LoadFromFile(filename, True)

        if mat<>0:
            self.cores=mydll.MATRIX_GetNCores(mat)
            if verbose==True: print "Matrix has ", self.cores, " cores"
            for i in range(self.cores):
                mydll.MATRIX_SetCore(mat,i)
                nameC=ctypes.create_string_buffer(50)
                mydll.MATRIX_GetLabel(mat,i,nameC)
                nameC=repr(nameC.value)
                nameC=nameC[1:len(nameC)-1]
                #If verbose, we list the matrices being loaded
                if verbose==True: print "   Loading core: ", nameC

                self.datafile=filename
                self.matrix[nameC]=mtx_core()
                self.matrix[nameC].add_core(mydll,mat,i)
        else:
            raise NameError('Not possible to open file. TranCad returned '+ str(tc_value))


        mydll.MATRIX_CloseFile(filename)
        mydll.MATRIX_Done(mat)


if __name__ == '__main__':
    main()

当我以任何形式(双击,python的IDLE或Pyscripter)运行测试代码时,它会因熟悉的错误崩溃而导致错误&#34; WindowsError:异常:访问违规写入0x0000000000000246&#34;,但是当我使用Pyscripter在任何内循环中停止来调试代码,它运行得很完美。

我真的很感激任何见解。

修改

DLL的Dumpbin输出:

文件类型:DLL

部分包含CaliperMTX.dll的以下导出

00000000 characteristics
52FB9F15 time date stamp Wed Feb 12 08:19:33 2014
    0.00 version
       1 ordinal base
      81 number of functions
      81 number of names

ordinal hint RVA      name

      1    0 0001E520 InitMatDLL
      2    1 0001B140 MATRIX_AddIndex
      3    2 0001AEE0 MATRIX_Clear
      4    3 0001AE30 MATRIX_CloseFile
      5    4 00007600 MATRIX_Copy
      6    5 000192A0 MATRIX_CreateCache
      7    6 00019160 MATRIX_CreateCacheEx
      8    7 0001EB10 MATRIX_CreateSimple
      9    8 0001ED20 MATRIX_CreateSimpleLike
     10    9 00016D40 MATRIX_DestroyCache
     11    A 00016DA0 MATRIX_DisableCache
     12    B 0001A880 MATRIX_Done
     13    C 0001B790 MATRIX_DropIndex
     14    D 00016D70 MATRIX_EnableCache
     15    E 00015B10 MATRIX_GetBaseNCols
     16    F 00015B00 MATRIX_GetBaseNRows
     17   10 00015FF0 MATRIX_GetBaseVector
     18   11 00015CE0 MATRIX_GetCore
     19   12 000164C0 MATRIX_GetCurrentIndexPos
     20   13 00015B20 MATRIX_GetDataType
     21   14 00015EE0 MATRIX_GetElement
     22   15 00015A30 MATRIX_GetFileName
     23   16 00007040 MATRIX_GetIDs
     24   17 00015B80 MATRIX_GetInfo
     25   18 00015A50 MATRIX_GetLabel
     26   19 00015AE0 MATRIX_GetNCols
     27   1A 00015AB0 MATRIX_GetNCores
     28   1B 00016EC0 MATRIX_GetNIndices
     29   1C 00015AC0 MATRIX_GetNRows
     30   1D 00018AF0 MATRIX_GetVector
     31   1E 00015B40 MATRIX_IsColMajor
     32   1F 00015B60 MATRIX_IsFileBased
     33   20 000171A0 MATRIX_IsReadOnly
     34   21 00015B30 MATRIX_IsSparse
     35   22 0001AE10 MATRIX_LoadFromFile
     36   23 0001BAE0 MATRIX_New
     37   24 00017150 MATRIX_OpenFile
     38   25 000192D0 MATRIX_RefreshCache
     39   26 00016340 MATRIX_SetBaseVector
     40   27 00015C20 MATRIX_SetCore
     41   28 00016200 MATRIX_SetElement
     42   29 00016700 MATRIX_SetIndex
     43   2A 0001AFA0 MATRIX_SetLabel
     44   2B 00018E50 MATRIX_SetVector
     45   2C 00005DA0 MAT_ACCESS_Create
     46   2D 00005E40 MAT_ACCESS_CreateFromCurrency
     47   2E 00004B10 MAT_ACCESS_Done
     48   2F 00005630 MAT_ACCESS_FillRow
     49   30 000056D0 MAT_ACCESS_FillRowDouble
     50   31 00005A90 MAT_ACCESS_GetCurrency
     51   32 00004C30 MAT_ACCESS_GetDataType
     52   33 000058E0 MAT_ACCESS_GetDoubleValue
     53   34 00004C40 MAT_ACCESS_GetIDs
     54   35 00005AA0 MAT_ACCESS_GetMatrix
     55   36 00004C20 MAT_ACCESS_GetNCols
     56   37 00004C10 MAT_ACCESS_GetNRows
     57   38 000055A0 MAT_ACCESS_GetRowBuffer
     58   39 00005570 MAT_ACCESS_GetRowID
     59   3A 00005610 MAT_ACCESS_GetToReadFlag
     60   3B 00005870 MAT_ACCESS_GetValue
     61   3C 00005AB0 MAT_ACCESS_IsValidCurrency
     62   3D 000055E0 MAT_ACCESS_SetDirty
     63   3E 000059F0 MAT_ACCESS_SetDoubleValue
     64   3F 00005620 MAT_ACCESS_SetToReadFlag
     65   40 00005960 MAT_ACCESS_SetValue
     66   41 00005460 MAT_ACCESS_UseIDs
     67   42 00005010 MAT_ACCESS_UseIDsEx
     68   43 00005490 MAT_ACCESS_UseOwnIDs
     69   44 00004D10 MAT_ACCESS_ValidateIDs
     70   45 0001E500 MAT_pafree
     71   46 0001E4E0 MAT_palloc
     72   47 0001E4F0 MAT_pfree
     73   48 0001E510 MAT_prealloc
     74   49 00006290 MA_MGR_AddMA
     75   4A 00006350 MA_MGR_AddMAs
     76   4B 00005F90 MA_MGR_Create
     77   4C 00006050 MA_MGR_Done
     78   4D 000060D0 MA_MGR_RegisterThreads
     79   4E 00006170 MA_MGR_SetRow
     80   4F 00006120 MA_MGR_UnregisterThread
     81   50 0001E490 UnloadMatDLL

摘要

    6000 .data
    5000 .pdata
    C000 .rdata
    1000 .reloc
    1000 .rsrc
   54000 .text

0 个答案:

没有答案