null pointer in nrnpy_nrn.cpp

When Python is the interpreter, what is a good
design for the interface to the basic NEURON
concepts.

Moderator: hines

Post Reply
csh
Posts: 52
Joined: Mon Feb 06, 2006 12:45 pm
Location: University College London
Contact:

null pointer in nrnpy_nrn.cpp

Post by csh » Tue Aug 18, 2009 2:53 pm

Hi,
there's an untested pointer in nrnpy_nrn.cpp that may lead to a seg fault under certain conditions. I've attached two sample files below to reproduce this behaviour. The problem will appear when exiting Python. I've tested Python 2.5 on GNU/Linux. Here's the stack trace:

Code: Select all

0xb7d0c054 in NPySecObj_dealloc (self=0xb7d795d8) at nrnpy_nrn.cpp:121
121				self->sec_->prop->dparam[PROP_PY_INDEX]._pvoid = 0;
Current language:  auto; currently c++
(gdb) where
#0  0xb7d0c054 in NPySecObj_dealloc (self=0xb7d795d8) at nrnpy_nrn.cpp:121
#1  0x08087a91 in ?? ()
#2  0x08089e8e in PyDict_SetItem ()
#3  0x0808b652 in _PyModule_Clear ()
#4  0x080e1df8 in PyImport_Cleanup ()
#5  0x080ed329 in Py_Finalize ()
This is a (probably cosmetic) patch against the hg repository:

Code: Select all

diff -r 6f1564693101 src/nrnpython/nrnpy_nrn.cpp
--- a/src/nrnpython/nrnpy_nrn.cpp	Fri Jul 31 08:58:54 2009 -0400
+++ b/src/nrnpython/nrnpy_nrn.cpp	Tue Aug 18 19:48:39 2009 +0100
@@ -117,9 +117,11 @@
 static void NPySecObj_dealloc(NPySecObj* self) {
 //printf("NPySecObj_dealloc %lx %s\n", (long)self, secname(self->sec_));
 	if (self->sec_) {
-		self->sec_->prop->dparam[PROP_PY_INDEX]._pvoid = 0;
+		if (self->sec_->prop) {
+			self->sec_->prop->dparam[PROP_PY_INDEX]._pvoid = 0;
+		}
 		if (self->name_) { delete [] self->name_; }
-		if (!self->sec_->prop->dparam[0].sym) {
+		if (self->sec_->prop && !self->sec_->prop->dparam[0].sym) {
 			sec_free(self->sec_->prop->dparam[8].itm);
 		}else{
 			section_unref(self->sec_);
Here are the sample files:

test_segfault.py

Code: Select all

from neuron import h

def find_dist_axon( cell, dist ):
    nseg = 0
    h.distance(0,0.0, sec=cell.soma)
    for seg in cell.axon:
        if h.distance( seg.x, sec=cell.axon ) > dist:
            return seg, cell.axon, nseg
        nseg += 1

    return None, None, None

h("""load_file("template.hoc")""")
cell = h.cell()
seg, axon, n = find_dist_axon( cell, 25 )
vc1 = h.SEClamp( cell.somaLoc.secRef.sec(0.5), sec=cell.somaLoc.secRef.sec )
template.hoc

Code: Select all

begintemplate Location
public secRef, loc
objref secRef

proc init() {
        secRef = new SectionRef()
        loc = $1
}
endtemplate Location

begintemplate cell

public init
public somaLoc
public axon, soma

objref somaLoc,this

create axon, soma

proc init() {
    create axon
    create soma

    access soma

    soma somaLoc = new Location(0.5)

    connect axon(0.0), soma(0.0)
}

endtemplate cell

hines
Site Admin
Posts: 1573
Joined: Wed May 18, 2005 3:32 pm

Re: null pointer in nrnpy_nrn.cpp

Post by hines » Thu Aug 20, 2009 3:38 pm

Thanks for that fix. It has been committed to the repository sources.
http://www.neuron.yale.edu/hg/neuron/nr ... bd8752e9c8

csh
Posts: 52
Joined: Mon Feb 06, 2006 12:45 pm
Location: University College London
Contact:

Re: null pointer in nrnpy_nrn.cpp

Post by csh » Thu Aug 20, 2009 6:09 pm

Is this forum the preferred place for patches and bug reports BTW?

Post Reply