QT, ctypes dll and SEG Faults

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sapsi

    QT, ctypes dll and SEG Faults

    Hello,
    Below is a small class using ctypes and libspectre to read a
    postscript file.
    My program is a PyQT 4.4 application and when the user clicks on a
    entry in a QTableWidget, i run

    PostScriptImage ( filename_as_con tained_in_click ed_tableWidgetI tem )
    However on i get a segfault while trying document_load.
    Surprisingly, before i run sys.exit(app.ex ec_()) (i.e before the app
    starts) if run PostScriptImage (command_line_s pecified_ps_fil e) it
    works!

    I know the ctypes so file works with QT since i wrote an application
    using it, but i hadn't separated the ctypes stuff into separate class
    (i.e the spectre code was in the widget method).

    Any ideas why the crash?

    Regards
    Saptarshi
    --Python Code--
    class PostScriptImage :
    def __init__(self,f ilename):
    print "Doc New"
    self.document=l ibspec.spectre_ document_new()
    print "Load Doc" , filename
    #############cr ashed in the following
    line########### ##############
    libspec.spectre _document_load( self.document,f ilename)
    print "Done load doc"
    if libspec.spectre _document_statu s(self.document ):
    return False
    self.scale=[1.0,1.0]
    self.quicksetup ()

    def quicksetup(self ):
    print "RC"
    rc=libspec.spec tre_render_cont ext_new()
    print "Get 0th Page"
    page=libspec.sp ectre_document_ get_page (self.document, 0)
    if libspec.spectre _document_statu s(self.document ):
    raise Exception("Spec tre:Setup Document Error")
    w= c_int()
    h= c_int()
    print "Page Size"
    libspec.spectre _page_get_size( page, byref(w),byref( h))
    self.initialSiz e=(h.value*1.0, w.value*1.0)
    self.initialAsp ect=float(h.val ue)/float(w.value)

    self.npages=lib spec.spectre_do cument_get_n_pa ges(self.docume nt)
  • sapsi

    #2
    Re: QT, ctypes dll and SEG Faults

    On Aug 16, 11:37 pm, sapsi <saptarshi.g... @gmail.comwrote :
    Hello,
    Below is a small class using ctypes and libspectre to read a
    postscript file.
    My program is a PyQT 4.4 application  and when the user clicks on a
    entry in a QTableWidget, i run
    >
    PostScriptImage ( filename_as_con tained_in_click ed_tableWidgetI tem )
    However on i get a segfault while trying document_load.
    Surprisingly, before i run sys.exit(app.ex ec_()) (i.e before the app
    starts) if run PostScriptImage (command_line_s pecified_ps_fil e) it
    works!
    >
    I know the ctypes so file works with QT since i wrote an application
    using it, but i hadn't separated the ctypes stuff into separate class
    (i.e the spectre code was in the widget method).
    >
    Any ideas why the crash?
    >
    Regards
    Saptarshi
    --Python Code--
    class PostScriptImage :
        def __init__(self,f ilename):
            print "Doc New"
            self.document=l ibspec.spectre_ document_new()
            print "Load Doc" , filename
            #############cr ashed in the following
    line########### ##############
            libspec.spectre _document_load( self.document,f ilename)
            print "Done load doc"
            if libspec.spectre _document_statu s(self.document ):
                return False
            self.scale=[1.0,1.0]
            self.quicksetup ()
    >
        def quicksetup(self ):
            print "RC"
            rc=libspec.spec tre_render_cont ext_new()
            print "Get 0th Page"
            page=libspec.sp ectre_document_ get_page (self.document, 0)
            if libspec.spectre _document_statu s(self.document ):
                raise Exception("Spec tre:Setup Document Error")
            w= c_int()
            h= c_int()
            print "Page Size"
            libspec.spectre _page_get_size( page, byref(w),byref( h))
            self.initialSiz e=(h.value*1.0, w.value*1.0)
            self.initialAsp ect=float(h.val ue)/float(w.value)
    >
    self.npages=lib spec.spectre_do cument_get_n_pa ges(self.docume nt)
    To answer my own question, partially, i found out if i replace
    filename with a hard coded value it doesn't crash.
    Now why is that? Does python lose the reference? Should i store
    filename as attributed of the object?
    Regards
    Saptarshi

    Comment

    • Matthieu Brucher

      #3
      Re: QT, ctypes dll and SEG Faults

      Hi,

      If the crash occurs in __init__, why do you give us more code ?

      The crash can occur because you pass a char* to your library. This
      char* is not managed by your library but by Python. This means that
      when the constructor is finished, the string is freed and the char* is
      not valid anymore.

      This is perhaps what is causing your error. I can't say more because
      your code sample is too long and not well explained (what is happening
      and what should happen ?)

      Matthieu

      2008/8/17 sapsi <saptarshi.guha @gmail.com>:
      On Aug 16, 11:37 pm, sapsi <saptarshi.g... @gmail.comwrote :
      >Hello,
      >Below is a small class using ctypes and libspectre to read a
      >postscript file.
      >My program is a PyQT 4.4 application and when the user clicks on a
      >entry in a QTableWidget, i run
      >>
      >PostScriptImag e( filename_as_con tained_in_click ed_tableWidgetI tem )
      >However on i get a segfault while trying document_load.
      >Surprisingly , before i run sys.exit(app.ex ec_()) (i.e before the app
      >starts) if run PostScriptImage (command_line_s pecified_ps_fil e) it
      >works!
      >>
      >I know the ctypes so file works with QT since i wrote an application
      >using it, but i hadn't separated the ctypes stuff into separate class
      >(i.e the spectre code was in the widget method).
      >>
      >Any ideas why the crash?
      >>
      >Regards
      >Saptarshi
      >--Python Code--
      >class PostScriptImage :
      > def __init__(self,f ilename):
      > print "Doc New"
      > self.document=l ibspec.spectre_ document_new()
      > print "Load Doc" , filename
      > #############cr ashed in the following
      >line########## ###############
      > libspec.spectre _document_load( self.document,f ilename)
      > print "Done load doc"
      > if libspec.spectre _document_statu s(self.document ):
      > return False
      > self.scale=[1.0,1.0]
      > self.quicksetup ()
      >>
      > def quicksetup(self ):
      > print "RC"
      > rc=libspec.spec tre_render_cont ext_new()
      > print "Get 0th Page"
      > page=libspec.sp ectre_document_ get_page (self.document, 0)
      > if libspec.spectre _document_statu s(self.document ):
      > raise Exception("Spec tre:Setup Document Error")
      > w= c_int()
      > h= c_int()
      > print "Page Size"
      > libspec.spectre _page_get_size( page, byref(w),byref( h))
      > self.initialSiz e=(h.value*1.0, w.value*1.0)
      > self.initialAsp ect=float(h.val ue)/float(w.value)
      >>
      >self.npages=li bspec.spectre_d ocument_get_n_p ages(self.docum ent)
      >
      To answer my own question, partially, i found out if i replace
      filename with a hard coded value it doesn't crash.
      Now why is that? Does python lose the reference? Should i store
      filename as attributed of the object?
      Regards
      Saptarshi
      --

      >


      --
      French PhD student
      Website : http://matthieu-brucher.developpez.com/
      Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92
      LinkedIn : http://www.linkedin.com/in/matthieubrucher

      Comment

      • Wojtek Walczak

        #4
        Re: QT, ctypes dll and SEG Faults

        On Sat, 16 Aug 2008 21:17:25 -0700 (PDT), sapsi wrote:
        >Below is a small class using ctypes and libspectre to read a
        >postscript file.
        >My program is a PyQT 4.4 application  and when the user clicks on a
        >entry in a QTableWidget, i run
        >>
        >PostScriptImag e( filename_as_con tained_in_click ed_tableWidgetI tem )
        >However on i get a segfault while trying document_load.
        >Surprisingly , before i run sys.exit(app.ex ec_()) (i.e before the app
        >starts) if run PostScriptImage (command_line_s pecified_ps_fil e) it
        >works!
        To answer my own question, partially, i found out if i replace
        filename with a hard coded value it doesn't crash.
        Now why is that? Does python lose the reference? Should i store
        filename as attributed of the object?
        Are you sure that the hardcoded value is _exactly_ the same
        as the one passed to __init__? Try it in __init__ to make sure:

        assert filename == 'your_hardcoded _value'

        And why won't you try to store the filename as an attribute?
        (even though I doubt it could help).

        --
        Regards,
        Wojtek Walczak,

        Comment

        Working...