# Crash/stackoverflow when calling constructor with too many inputs

**URL:** https://discourse.julialang.org/t/crash-stackoverflow-when-calling-constructor-with-too-many-inputs/7116
**Category:** General Usage
**Tags:** question
**Created:** [November 17, 2017, 8:21am UTC](https://discourse.julialang.org/t/crash-stackoverflow-when-calling-constructor-with-too-many-inputs/7116 "2017-11-17T08:21:16Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [November 17, 2017, 8:21am UTC](https://discourse.julialang.org/t/crash-stackoverflow-when-calling-constructor-with-too-many-inputs/7116/1 "2017-11-17T08:21:17Z")

</div>

I’m writing some code where I define vectors and points in homogeneous coordinates. To this end, I inherit from `StaticArrays.FieldVector` with length N+1, in order to enforce the invariant that the last coordinate be equal to 1.

The code has been working fine for a while, then I started experiencing that Julia would suddenly close without warning. I’m not sure exactly what’s going on, but I have recreated similar behaviour in the below examples. The behaviour occurs when I call the type constructors with too many inputs. Presumably, some behaviour is inherited from the supertype, causing the problems.

The problem progresses in severity with the number of dimensions. In example 1, I get a simple ‘ambiguous method’ error. In example 2, I get stack overflow error (which prints 80,000 times!), and in example 3, Julia closes immediately without warning.

```julia
using StaticArrays

struct Dummy1D{T<:Real} <: FieldVector{2, T}
    x::T
    y::T
    Dummy1D{T}(x) where {T} = new{T}(x, one(T))
end

struct Dummy2D{T<:Real} <: FieldVector{3, T}
    x::T
    y::T
    z::T
    Dummy2D{T}(x, y) where {T} = new{T}(x, y, one(T))
end

struct Dummy3D{T<:Real} <: FieldVector{4, T}
    x::T
    y::T
    z::T
    w::T
    Dummy3D{T}(x, y, z) where {T} = new{T}(x, y, z, one(T))
end

```

The errors:

```julia
Main> Dummy1D{Int}(1,2)
ERROR: MethodError: Dummy1D{Int64}(::Tuple{Int64,Int64}) is ambiguous. Candidates:
  (::Type{Dummy1D{T}})(x) where T in Main at none:4
  (::Type{FV})(x::Tuple) where FV<:StaticArrays.FieldVector in StaticArrays at /Users/dnf/.julia/v0.6/StaticArrays/src/FieldVector.jl:20
Possible fix, define
  (::Type{Dummy1D{T}})(::Tuple)
Stacktrace:
 [1] Dummy1D{Int64}(::Int64, ::Int64) at /Users/dnf/.julia/v0.6/StaticArrays/src/convert.jl:3
 [2] eval(::Module, ::Any) at ./boot.jl:235

Main> Dummy2D{Int}(1,2,3)
ERROR: StackOverflowError...
...
...
[79994] Dummy2D{Int64}(::Tuple{Int64,Int64,Int64}) at /Users/dnf/.julia/v0.6/StaticArrays/src/FieldVector.jl:20
[79995] Dummy2D{Int64}(::Int64, ::Int64, ::Int64) at /Users/dnf/.julia/v0.6/StaticArrays/src/convert.jl:3
[79996] Dummy2D{Int64}(::Tuple{Int64,Int64,Int64}) at /Users/dnf/.julia/v0.6/StaticArrays/src/FieldVector.jl:20
[79997] Dummy2D{Int64}(::Int64, ::Int64, ::Int64) at /Users/dnf/.julia/v0.6/StaticArrays/src/convert.jl:3

Main> Dummy2D{Int}(1,2,3,4)
<Julia closes without warning>

Main> Dummy3D{Int}(1,2,3,4)
<Julia closes without warning>

```

In these cases I deliberately call the constructors with too many arguments. In the original code I don’t do that, but I suspect that with the increasing number of complicated constructors I’ve made to cover all use cases, somehow, that behaviour is inadvertently triggered.

Is the above behaviour expected?

**Edit:** Oh, BTW, version 0.6.1.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [November 17, 2017, 3:34pm UTC](https://discourse.julialang.org/t/crash-stackoverflow-when-calling-constructor-with-too-many-inputs/7116/2 "2017-11-17T15:34:50Z")

</div>

I would consider this a StaticArrays bug. Issue opened: [https://github.com/JuliaArrays/StaticArrays.jl/issues/342](https://github.com/JuliaArrays/StaticArrays.jl/issues/342)

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [November 17, 2017, 3:38pm UTC](https://discourse.julialang.org/t/crash-stackoverflow-when-calling-constructor-with-too-many-inputs/7116/3 "2017-11-17T15:38:04Z")

</div>

But Julia should not crash. Or is StaticArrays doing some unsafe stuff under the hood?

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [November 17, 2017, 3:46pm UTC](https://discourse.julialang.org/t/crash-stackoverflow-when-calling-constructor-with-too-many-inputs/7116/4 "2017-11-17T15:46:39Z")

</div>

I frequently observe Julia crashing when stack overflow occurs, including in situations which do not involve StaticArrays.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [November 17, 2017, 3:46pm UTC](https://discourse.julialang.org/t/crash-stackoverflow-when-calling-constructor-with-too-many-inputs/7116/5 "2017-11-17T15:46:54Z")

</div>

(though I of course agree that it _should_ not)

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [November 17, 2017, 4:03pm UTC](https://discourse.julialang.org/t/crash-stackoverflow-when-calling-constructor-with-too-many-inputs/7116/6 "2017-11-17T16:03:39Z")

</div>

Probably relevant: [https://github.com/JuliaLang/julia/issues/23126#issuecomment-320165678](https://github.com/JuliaLang/julia/issues/23126#issuecomment-320165678)

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [November 18, 2017, 11:01am UTC](https://discourse.julialang.org/t/crash-stackoverflow-when-calling-constructor-with-too-many-inputs/7116/7 "2017-11-18T11:01:50Z")

</div>

Okay, should be fixed on StaticArrays master. You can run `Pkg.checkout("StaticArrays")` to try it out.
