Python Forum
python kernell crash with a ctypes program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python kernell crash with a ctypes program
#1
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.
Reply
#2
I am reviewing the source code, and then I will put it available.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using ctypes to use a dll in a python module dauriac 3 291 Mar-06-2024, 04:38 PM
Last Post: dauriac
  Trying to debug segfault in ctypes binding to Fortran bthomas 1 568 Sep-01-2023, 11:26 AM
Last Post: bthomas
  ctypes juliolop 7 1,326 Apr-20-2023, 03:33 PM
Last Post: Larz60+
  Issue while using ctypes in python GiggsB 6 2,688 Mar-27-2022, 03:38 AM
Last Post: GiggsB
  Catching a crash within a library code ebolisa 9 3,060 Nov-22-2021, 11:02 AM
Last Post: bowlofred
  Ctypes and libffi.so.7 luxedo 1 5,982 Oct-23-2021, 09:24 PM
Last Post: DeaD_EyE
  Python Crash Course ( 2nd edition) alok 1 1,844 Jul-19-2021, 05:55 PM
Last Post: snippsat
  Embedding python cause crash when use boost::asio multi threading udvatt108 0 1,694 Oct-04-2020, 03:15 PM
Last Post: udvatt108
  possible ctypes and numpy conflict? herbal_rage 0 3,092 Apr-16-2020, 11:35 AM
Last Post: herbal_rage
  Im using python crash course version 2 james_newbie 3 2,345 Sep-07-2019, 09:21 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020