473,385 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

My C module crashes

Hi!

I've written a C-module for Python, and it works as intended, but
obviously does something wrong with its memmory management (refference
counting), as it causes Python to segfault now and then (randomly,
whey :S)

The module source code is available at
http://grimoire.takeit.se/files/CReader.c

The code isn't _that_ long, so someone experienced with writing Python C
modules should probably not have that hard a time finding what I do
wrong :)

Thanks in advance,
Egil Möller
Jul 18 '05 #1
3 1049
Egil Moeller wrote:
I've written a C-module for Python, and it works as intended, but
obviously does something wrong with its memmory management (refference
counting), as it causes Python to segfault now and then (randomly,
whey :S)


You can atry to use the Python function sys.getrefcount. Print the
refcount of the parameter values before and after your extension gets
called and print the refcount of the return value (s). I once forgot to
increment the refcount of the None object which was put into the return
tuple.

If you pass a list as a parameter, you should also print the refcounts
of the list elements etc.

Daniel Dittmar
Jul 18 '05 #2

Egil Moeller wrote:
Hi!

I've written a C-module for Python, and it works as intended, but
obviously does something wrong with its memmory management (refference counting), as it causes Python to segfault now and then (randomly,
whey :S)

The module source code is available at
http://grimoire.takeit.se/files/CReader.c

The code isn't _that_ long, so someone experienced with writing Python C modules should probably not have that hard a time finding what I do
wrong :)


What you are doing wrong:

1. Unless getting the source to/from a web page has stuffed up the
indentation [which you should have checked before posting your
message], it appears to be indented only ONE space per level. Are you
using tabs? Don't. They are intrinsically evil.

2. Your code's over 1200 lines long, and even after allowing for the
indentation problem it certainly doesn't read like the poetry of your
namesake, Skalla-Grim's son.

3. Expecting people to debug your code remotely when it exhibits
problems (1) and (2)

4. Not stating what (if anything) you have tried yourself before
posting.

5. Using system free() and realloc() instead the Python-supplied memory
API functions.

6. Using *ANY* free() or realloc() function on memory that has *NOT*
been allocated with the allocator function from the same family.

7. Not checking whether a buffer has even been allocated before trying
to deallocate it.

Jul 18 '05 #3
On 2005-02-28, Egil Moeller <re****@takeit.se> wrote:
I've written a C-module for Python, and it works as intended, but
obviously does something wrong with its memmory management (refference
counting), as it causes Python to segfault now and then (randomly,
whey :S)


Have you tried compiling Python and your module with debugging
enabled? It might make the crash less random--or it might not help at
all, but it's probably worth a try. Running Python with debugging
enabled is a good idea anyway when developing extension modules.
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: TheDustbustr | last post by:
I just downloaded the uthread module for stackless python. Whenever I import it it says it can't find the continuation module. Anyone know where I can get it? Thanks, Dustin
2
by: Jeff Epler | last post by:
Hello. Recently, Generator Comprehensions were mentioned again on python-list. I have written an implementation for the compiler module. To try it out, however, you must be able to rebuild...
12
by: Nathaniel Echols | last post by:
I've written a function in C to perform protein sequence alignment. This works fine in a standalone C program. I've added the necessary packaging to use it in Python; it returns three strings and...
6
by: Joe HM | last post by:
Hello - The following code will crash if the TestA() instances are declared as Private and work just fine if they are Public. Is there a way to hide these instances to callers from outside the...
3
by: JimInAJam | last post by:
Hi, Please don't stress to find a solution - I've got a workaround I just want to see if I can improve on it. I'm using Access 2003 and have a form module which I want to edit using a standard...
8
by: Grant Edwards | last post by:
Can anybody point me to a Delaunay triangulation module (for Win32)? I'm currently using http://flub.stuffwillmade.org/delny/ under Linux, but I have been unable to find a build for Windows. I...
3
by: Mitko Haralanov | last post by:
I have a Python module that I have written using the C API and I am having a problem accessing a dictionary from that module. Here is what I have done: 1. In my init function I call module =...
22
by: ben mitch | last post by:
Hi I hope you'll see this cross-post (c/c++) as appropriate. I also admit immediately that the question *may* turn out to be compiler-/os-specific, in which case I apologize. But I wonder if...
0
by: John [H2O] | last post by:
Hello, I have a module created from a Fortran file to read in unformatted binary fortran output. It works fine for some datasets, but crashes with others. The strange thing is it will loop...
0
by: John [H2O] | last post by:
I can try, would you mind giving very brief instructions on how to 'run it under gdb'... thanks! I'll post results over at numpy-discussions. Robert Kern-2 wrote: -- View this message in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.