← Back to team overview

dolfin team mailing list archive

[Bug 424251] Re: Inline algebraic operators on vectors unsafe in pydolfin

 

** Changed in: dolfin
       Status: Fix Committed => Fix Released

-- 
Inline algebraic operators on vectors unsafe in pydolfin
https://bugs.launchpad.net/bugs/424251
You received this bug notification because you are subscribed to DOLFIN.

Status in DOLFIN: Fix Released

Bug description:
>From Bugzilla:

Given 
  v = Vector()
  u = Vector()
  ... initialize v, u

The operations
  v *= -1.0
  v -+= u

are unsafe, causing possible memory corruption. This seems to be because swig
creates new python objects for the vectors, causing deletion the C++ object if
the original reference count goes to zero.

Temporary workaround:
  tmp = numpy.zeros(v.size())
  v.get(tmp)
  v.set(3.0*tmp)
is mathematically equivalent with
  v *= 3.0
and
  tmp1 = numpy.zeros(v.size())
  tmp2 = numpy.zeros(v.size())
  u.get(tmp1)
  v.get(tmp2)
  v.set(tmp1+tmp2)
is mathematically equivalent with
  v += u


------- Comment #1 From Martin Alnæs 2008-06-25 12:43:24 [reply] -------

Reproduction of the bug is easy:

In [1]: from dolfin import *
v
In [2]: v = Vector()
uBlasVector constructor
Vector constructor

In [3]: u = Vector()
uBlasVector constructor
Vector constructor

In [4]: v.init(3)

In [5]: u.init(3)

In [6]: w = v

In [7]: v += u

In [8]: del v
uBlasVector destructor
Vector destructor

In [9]: w.size()
Segmentation fault (core dumped)


------- Comment #2 From Martin Alnæs 2008-06-25 12:45:02 [reply] -------

Temporary workarounds:

def imul(a, b):
    v = numpy.zeros(a.size())
    a.get(v)
    a.set(b*v)

def iadd(a, b):
    v = numpy.zeros(a.size())
    w = numpy.zeros(a.size())
    a.get(v)
    b.get(w)
    a.set(v+w)


References