Repository: numba/numba

Issue:
Minimal not working example (MNWE) using `nopython` causes console to crash
Hi all,

(back again after a hiatus!)

Here's the MNWE: https://gist.github.com/bmer/a9eb9454723d0905ce13

Does running the MNWE script cause your console to crash as well?

I am using the latest version of numba available for win64 through the development channel.



Discussion:
Works for me using Numba 0.20.0 on OSX installed via Anaconda, but fails with the following message using `0.21.0-np19py27_34` from the dev channel:

```
python(23557,0x7fff7c73b310) malloc: *** error for object 0x10c74b3e0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
```

@sklam Are you able to reproduce the error too?

I can reproduce the crash under Linux 64-bit.


Resolving patch:
diff --git a/numba/lowering.py b/numba/lowering.py
--- a/numba/lowering.py
+++ b/numba/lowering.py
@@ -641,6 +641,8 @@ def lower_expr(self, resty, expr):
             # If we have a tuple, we needn't do anything
             # (and we can't iterate over the heterogenous ones).
             if isinstance(ty, types.BaseTuple):
+                assert ty == resty
+                self.incref(ty, val)
                 return val
 
             itemty = ty.iterator_type.yield_type


Test patch:
diff --git a/numba/tests/test_unpack_sequence.py b/numba/tests/test_unpack_sequence.py
--- a/numba/tests/test_unpack_sequence.py
+++ b/numba/tests/test_unpack_sequence.py
@@ -4,8 +4,8 @@
 
 import numba.unittest_support as unittest
 from numba.compiler import compile_isolated, Flags
-from numba import errors, types
-from .support import TestCase
+from numba import errors, types, typeof
+from .support import TestCase, MemoryLeakMixin
 
 enable_pyobj_flags = Flags()
 enable_pyobj_flags.set("enable_pyobject")
@@ -14,6 +14,7 @@
 force_pyobj_flags.set("force_pyobject")
 
 no_pyobj_flags = Flags()
+no_pyobj_flags.set("nrt")
 
 
 def unpack_list(l):
@@ -76,6 +77,19 @@ def unpack_nested_heterogenous_tuple():
     return a + b + c
 
 
+def unpack_arbitrary(seq):
+    a, b = seq
+    return b, a
+
+
+def unpack_nrt():
+    a = numpy.zeros(1)
+    b = numpy.zeros(2)
+    tup = b, a
+    alpha, beta = tup
+    return alpha, beta
+
+
 def chained_unpack_assign1(x, y):
     # Used to fail in object mode (issue #580)
     a = (b, c) = (x, y)
@@ -90,7 +104,7 @@ def conditional_swap(x, y):
     return x, y
 
 
-class TestUnpack(TestCase):
+class TestUnpack(MemoryLeakMixin, TestCase):
 
     def test_unpack_list(self):
         pyfunc = unpack_list
@@ -198,6 +212,21 @@ def test_conditional_swap(self):
     def test_conditional_swap_npm(self):
         self.check_conditional_swap(no_pyobj_flags)
 
+    def test_unpack_tuple_of_arrays(self):
+        tup = tuple(numpy.zeros(i + 1) for i in range(2))
+        tupty = typeof(tup)
+        pyfunc = unpack_arbitrary
+        cr = compile_isolated(pyfunc, (tupty,),
+                              flags=no_pyobj_flags)
+        cfunc = cr.entry_point
+        self.assertPreciseEqual(cfunc(tup), pyfunc(tup))
+
+    def test_unpack_nrt(self):
+        pyfunc = unpack_nrt
+        cr = compile_isolated(pyfunc, (), flags=no_pyobj_flags)
+        cfunc = cr.entry_point
+        self.assertPreciseEqual(cfunc(), pyfunc())
+
 
 if __name__ == '__main__':
     unittest.main()