469,342 Members | 5,859 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,342 developers. It's quick & easy.

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_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,filena me)
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(s elf.document)
Aug 17 '08 #1
3 1651
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_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,filena me)
* * * * 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(s elf.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
Aug 17 '08 #2
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 <sa************@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

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,filena me)
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
--
http://mail.python.org/mailman/listinfo/python-list


--
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
Aug 17 '08 #3
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

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!
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,
http://www.stud.umk.pl/~wojtekwa/
Aug 17 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Thomas Heller | last post: by
19 posts views Thread by Thomas Heller | last post: by
1 post views Thread by sjdevnull | last post: by
6 posts views Thread by Jack | last post: by
1 post views Thread by moreati | last post: by
3 posts views Thread by Andrew Lentvorski | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.