Hi group.
I have a problem with some C extensions I am working with and
hope that some of you can help. Basically, I am wrapping a a tree
structure from C where I have python methods for extracting either the
entire tree or subtrees; since I don't want the full tree to be
deallocated while python has references to the subtrees, I INCREF the
full tree whenever I hand out a subtree reference. I don't want any of
the subtrees to be deallocated while the full tree lives, either, so I
want the fulltree to have a reference to each of the subtrees. Naturally,
this gives me a cyclik reference structure, and I want to be able to
garbage collect it. But this is where my problems begin...
For some reason, when I turn on the cyclic garbage collection for my
types, a deallocation automatically gives me a segfault. That is, if my
type new type contains any data whatsoever, I cannot garbage collect
without dumping core. I have boiled the problem down to the code shown
below. It contains no cyclic structure, it is as simple as it gets, but
it still seqfaults for me. If I comment out the magic void-pointer (which
isn't used for anything) I don't get the segfault.
#include <Python.h>
struct SimpleObject;
static PyObject * Simple_new (PyTypeObject *type,
PyObject *args,
PyObject *kwds);
static int Simple_init (struct SimpleObject *self,
PyObject *args,
PyObject *kwds);
static int Simple_traverse (struct SimpleObject *self,
visitproc visit,
void *arg);
static void Simple_dealloc (struct SimpleObject *self);
typedef struct SimpleObject {
/* object stuff */
PyObject_HEAD
/* dark magic */
void *magic;
} SimpleObject;
static PyTypeObject simple_SimpleTy pe = {
PyObject_HEAD_I NIT(NULL)
0, /*ob_size*/
"simple.Simple" , /*tp_name*/
sizeof(SimpleOb ject), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)Sim ple_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence */
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFA ULT | Py_TPFLAGS_BASE TYPE | Py_TPFLAGS_HAVE _GC, /*tp_flags*/
"Simple objects", /*tp_doc*/
(traverseproc)S imple_traverse, /*tp_traverse*/
0, /*tp_clear*/
0, /*tp_richcompare */
0, /*tp_weaklistoff set*/
0, /*tp_iter*/
0, /*tp_iternext*/
0, /*tp_methods*/
0, /*tp_members*/
0, /*tp_getset*/
0, /*tp_base*/
0, /*tp_dict*/
0, /*tp_descr_get*/
0, /*tp_descr_set*/
0, /*tp_dictoffset*/
(initproc)Simpl e_init, /*tp_init*/
0, /*tp_alloc*/
Simple_new, /*tp_new*/
0 /*...the rest...*/
};
static PyObject *
Simple_new(PyTy peObject *type, PyObject *args, PyObject *kwds)
{
SimpleObject *self = (SimpleObject *)type->tp_alloc(typ e, 0);
return (PyObject*)self ;
}
static int
Simple_init(Sim pleObject *self, PyObject *args, PyObject *kwds)
{
return 0;
}
/* cyclic gc */
static int
Simple_traverse (SimpleObject *self, visitproc visit, void *arg)
{
fprintf(stderr, "Simple_travers e...\n");
return 0;
}
static int
Simple_clear(Si mpleObject *self)
{
fprintf(stderr, "Simple_clear.. .\n");
return 0;
}
static void
Simple_dealloc( SimpleObject *self)
{
fprintf(stderr, "Simple_dea lloc %p\n", self);
self->ob_type->tp_free((PyObj ect*)self); /* <= segfault here */
return;
}
static PyMethodDef simple_methods[] = {
{0} /* sentinel */
};
void
initsimple(void )
{
if (PyType_Ready(& simple_SimpleTy pe) < 0) return;
Py_INCREF(&simp le_SimpleType);
PyObject* m = Py_InitModule3( "simple", simple_methods, "Simple module.");
PyModule_AddObj ect(m,"Simple", (PyObject*)&sim ple_SimpleType) ;
}
A script sufficient to provoke the fault is this:
import simple
simple.Simple()
Can anyone explain what I'm doing wrong? Or perhaps suggest a better
solution to my "real" problem, if I'm approaching the problem completely
wrong :-)
Yours,
/mailund 3 1777 Can anyone explain what I'm doing wrong? Or perhaps suggest a better solution to my "real" problem, if I'm approaching the problem completely wrong :-)
Yours, /mailund
In my extensions python seems to segfault in the GC most of all...
Just yesterday I removed a malloc that was (why??) causing a segfault.
Have you seen pyrex ? It's absolutely brilliant. Take 2 hours to try it
out if you have the time.
Simon.
"Thomas Mailund" <ma*****@birc.d k> writes: Hi group.
I have a problem with some C extensions I am working with and hope that some of you can help.
[snippety]
static void Simple_dealloc( SimpleObject *self) { fprintf(stderr, "Simple_dea lloc %p\n", self); self->ob_type->tp_free((PyObj ect*)self); /* <= segfault here */
Well, you're calling tp_free from a tp_dealloc. That doesn't *sound*
sensible to me.
Can anyone explain what I'm doing wrong? Or perhaps suggest a better solution to my "real" problem, if I'm approaching the problem completely wrong :-)
There are docs on this sort of thing.
Cheers,
mwh
--
"Sturgeon's Law (90% of everything is crap) applies to Usenet."
"Nothing guarantees that the 10% isn't crap, too."
-- Gene Spafford's Axiom #2 of Usenet, and a corollary
On Thu, 15 Jan 2004 11:15:51 +0000, Michael Hudson wrote: "Thomas Mailund" <ma*****@birc.d k> writes:
Hi group.
I have a problem with some C extensions I am working with and hope that some of you can help.
[snippety]
static void Simple_dealloc( SimpleObject *self) { fprintf(stderr, "Simple_dea lloc %p\n", self); self->ob_type->tp_free((PyObj ect*)self); /* <= segfault here */
Well, you're calling tp_free from a tp_dealloc. That doesn't *sound* sensible to me.
I'm suprised to hear that; the documentation I was working from,
<URL:http://www.python.org/doc/current/ext/node22.html>, does exactly
that. Of course, this is from the section that uses reference counting,
not cyclic gc, but later on, when the garbage collector is introduced, the
deallocator still calls tp_free, it just calls clear first
<URL:http://www.python.org/doc/current/ext/node24.html>.
If I shouldn't free self in this way, how should I do it? Can anyone explain what I'm doing wrong? Or perhaps suggest a better solution to my "real" problem, if I'm approaching the problem completely wrong :-)
There are docs on this sort of thing.
About the garbage collection? In that case, I thought I *was* following
the documentation ;-)
If about the general problem with accessing the whole and parts of a C
structure from python, if you have any specific references in mind, I
would be very grateful if you would post them. I know that I am not the
first to have this problem, and that very likely there are general
patterns for solving the problem, but my googling didn't find anything
(which probably means I wasn't asking the right questions, but never the
less...)
Yours,
/mailund This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ganesh |
last post by:
Is there a utility by microsoft (or anyone) to force garbage collection in a
process without have access to the process code.
regards
Ganesh
|
by: Rick |
last post by:
Hi,
My question is.. if Lisp, a 40 year old language supports garbage
collection, why didn't the authors of C++ choose garbage collection for
this language? Are there fundamental reasons behind this? Is it because
C is generally a 'low level' language and they didn't want garbage
collection to creep into C++ and ruin everything?
Just wondering :)
|
by: Bob lazarchik |
last post by:
Hello:
We are considering developing a time critical system in C#. Our tool used in
Semiconductor production and we need to be able to take meaurements at
precise 10.0 ms intervals( 1000 measurement exactly 10 ms apart. In the
future this may decrease to 5ms ). I am concerned that if garbage
collection invokes during this time it may interfere with our measurement
results. I have looked over the garbage collection mechanism and see no...
|
by: mike2036 |
last post by:
For some reason it appears that garbage collection is releasing an object
that I'm still using. The object is declared in a module and instantiated
within a class that is in turn instantiated by the mainline. The class that
instantiated the object in question is definitely still in existence at the
point garbage collection swoops in and yanks it out from under my processing.
Is there a way to ensure an instantiated object cannot be freed...
|
by: Goalie_Ca |
last post by:
I have been reading (or at least googling) about the potential addition
of optional garbage collection to C++0x. There are numerous myths and
whatnot with very little detailed information.
Will this work be library based or language based and will it be based
on that of managed C++? Then of course there are the finer technical
questions raised (especially due to pointer abuse). Is a GC for C++
just a pipe dream or is there a lot of work...
| |
by: Joe Peterson |
last post by:
I've been doing a lot of searching on the topic of one of Python's more
disturbing issues (at least to me): the fact that if a __del__ finalizer
is defined and a cyclic (circular) reference is made, the garbage
collector cannot clean it up.
First of all, it seems that it's best to avoid using __del__. So far, I
have never used it in my Python programming. So I am safe there. Or am
I? Also, to my knowledge, I have never created a...
|
by: skip |
last post by:
We encountered a situation today where it appeared that a
Boost.Python-provided class didn't participate in Python's cyclic garbage
collection. The wrapped C++ instance held a reference to a method in the
Python object which referenced the Boostified C++ instance, e.g.:
class Foo:
def __init__(self, ...):
self.over_there = BoostifiedClass()
self.over_there.set_callback(self.boost_callback)
...
|
by: Johnny E. Jensen |
last post by:
Hellow
I'am not sure what to think about the Garbage Collector.
I have a Class OutlookObject, It have two private variables.
Private Microsoft.Office.Interop.Outlook.Application _Application = null;
Private Microsoft.Office.Interop.Outlook.NameSpace _Namespace = null;
The Constructor:
public OutlookObject()
|
by: pushpakulkar |
last post by:
Hi all,
Is garbage collection possible in C++. It doesn't come as part of
language support. Is there any specific reason for the same due to the
way the language is designed. Or it is discouraged due to
some specific reason. If someone can give inputs on the same, it will
be of great help.
Regards,
Pushpa
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |