QT, ctypes dll and SEG Faults

S

sapsi

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_contained_in_clicked_tableWidgetItem )
However on i get a segfault while trying document_load.
Surprisingly, before i run sys.exit(app.exec_()) (i.e before the app
starts) if run PostScriptImage(command_line_specified_ps_file) 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,filename):
print "Doc New"
self.document=libspec.spectre_document_new()
print "Load Doc" , filename
#############crashed in the following
line#########################
libspec.spectre_document_load(self.document,filename)
print "Done load doc"
if libspec.spectre_document_status(self.document):
return False
self.scale=[1.0,1.0]
self.quicksetup()

def quicksetup(self):
print "RC"
rc=libspec.spectre_render_context_new()
print "Get 0th Page"
page=libspec.spectre_document_get_page (self.document, 0)
if libspec.spectre_document_status(self.document):
raise Exception("Spectre:Setup Document Error")
w= c_int()
h= c_int()
print "Page Size"
libspec.spectre_page_get_size(page, byref(w),byref(h))
self.initialSize=(h.value*1.0,w.value*1.0)
self.initialAspect=float(h.value)/float(w.value)

self.npages=libspec.spectre_document_get_n_pages(self.document)
 
S

sapsi

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_contained_in_clicked_tableWidgetItem )
However on i get a segfault while trying document_load.
Surprisingly, before i run sys.exit(app.exec_()) (i.e before the app
starts) if run PostScriptImage(command_line_specified_ps_file) 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,filename):
        print "Doc New"
        self.document=libspec.spectre_document_new()
        print "Load Doc" , filename
        #############crashed in the following
line#########################
        libspec.spectre_document_load(self.document,filename)
        print "Done load doc"
        if libspec.spectre_document_status(self.document):
            return False
        self.scale=[1.0,1.0]
        self.quicksetup()

    def quicksetup(self):
        print "RC"
        rc=libspec.spectre_render_context_new()
        print "Get 0th Page"
        page=libspec.spectre_document_get_page (self.document, 0)
        if libspec.spectre_document_status(self.document):
            raise Exception("Spectre:Setup Document Error")
        w= c_int()
        h= c_int()
        print "Page Size"
        libspec.spectre_page_get_size(page, byref(w),byref(h))
        self.initialSize=(h.value*1.0,w.value*1.0)
        self.initialAspect=float(h.value)/float(w.value)

self.npages=libspec.spectre_document_get_n_pages(self.document)

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
 
M

Matthieu Brucher

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 said:
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_contained_in_clicked_tableWidgetItem )
However on i get a segfault while trying document_load.
Surprisingly, before i run sys.exit(app.exec_()) (i.e before the app
starts) if run PostScriptImage(command_line_specified_ps_file) 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,filename):
print "Doc New"
self.document=libspec.spectre_document_new()
print "Load Doc" , filename
#############crashed in the following
line#########################
libspec.spectre_document_load(self.document,filename)
print "Done load doc"
if libspec.spectre_document_status(self.document):
return False
self.scale=[1.0,1.0]
self.quicksetup()

def quicksetup(self):
print "RC"
rc=libspec.spectre_render_context_new()
print "Get 0th Page"
page=libspec.spectre_document_get_page (self.document, 0)
if libspec.spectre_document_status(self.document):
raise Exception("Spectre:Setup Document Error")
w= c_int()
h= c_int()
print "Page Size"
libspec.spectre_page_get_size(page, byref(w),byref(h))
self.initialSize=(h.value*1.0,w.value*1.0)
self.initialAspect=float(h.value)/float(w.value)

self.npages=libspec.spectre_document_get_n_pages(self.document)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top