Python Forum
python kernell crash with a ctypes program - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: python kernell crash with a ctypes program (/thread-14293.html)



python kernell crash with a ctypes program - Jstechg - Nov-23-2018

We have a python program that by means of ctypes calls several functions of a C dll.

1) In the first version of this program all works right:

1.a)C function declarations are :

  // Callback functions:
  typedef void (*function_1) (int i);
  typedef void (*function_2) (char* s);
  int functions_dispacher(function1 f1, function2 f2);

  // Functions:
  int function_4 (int i, char* s);
1.b)Python program is (works right) :
  dll_lib=cdll.LoadLibrary("dll_file_name")

  # Callback functions:

  def callback1(i):
      # Process i value
      return ()

  def callback2(s):
      # Process s value
      return ()

  pyf1 = CFUNCTYPE(c_int)
  cb1 = pyf1(callback_1)

  pyf2 = CFUNCTYPE(c_char_p)
  cb2 = pyf2(callback_2)

  f3 = dll_lib.functions_dispacher
  f3.argtypes = [pyf1, pyf2]
  f3.restype = int
  return_code_3 = f3(cb1, cb2)


  # Functions:

    int_value = 5
    str_value = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    f4 = dll_lib.function_1
    f4.argtypes = [c_int,c_char_p]
    f4.restype = int
    return_code_4 = f4(int_value, str_value)
2) If we modify the function "functions_dispacher". when we run the python program, a crash of the python
kernell occurs on the sentence that is indicated (that has not changed on the dll and before it worked
fine) :

Thae change on "functions_dispacher" is the only change in the dll. All the other functions remain
unchanged.

2.a) New C function declarations are :
  // Callback funtions:
  typedef void (*function_1) (int i);
  typedef void (*function_2) (char* s);

  typedef struct
  {
    function_1 f1; 
    function_2 f2;
  }s_func;

  int functions_dispacher(s_func f);

  // Functions:
  int function_4 (int i, char* s);
2.b)New python program is (python kernel crash) :
  dll_lib=cdll.LoadLibrary("dll_file_name")

  # Callback funtions:

  def callback1(i):
      # Process i value
      return ()

  def callback2(s):
      # Process s value
      return ()

  class s_func(Structure):
      _fields_ = [("f1", CFUNCTYPE(c_int)),
                  ("f2", CFUNCTYPE(c_char_p))]

  struc_func = s_func()

  pyf1 = CFUNCTYPE(c_int)
  cb1 = pyf1(callback_1)

  pyf2 = CFUNCTYPE(c_char_p)
  cb2 = pyf2(callback_2)

  struc_func.f1 = callback1
  struc_func.f2 = callback2

  f3 = dll_lib.functions_dispacher
  f3.argtypes = [s_func]
  f3.restype = int
  return_code_3 = f3(struc_func)


  # Functions:

  int_value = 5
  str_value = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  f4 = dll_lib.function_4
  f4.argtypes = [c_int,c_char_p]
  f4.restype = int

  # ON THE NEXT SENTENCE, PYTHON KERNEL CRASH
  return_code_4 = f4(int_value, str_value)
I think the problem should be in process of calling the function "functions_dispacher" from python, because
this is what has changed, but I do not know how can I debug this and find the problem.
Besides, I think the python code is right, but it should not be.

The python program is executed from spyder on a Windows PC. Could this be a spyder/anaconda bug?
I think not, that the problem is in the python program.

Any help is appreciated.


RE: python kernell crash with a ctypes program - Jstechg - Nov-24-2018

I am reviewing the source code, and then I will put it available.