# Variable gets updated without even being referenced. Advice on debugging this

**URL:** <https://discourse.julialang.org/t/variable-gets-updated-without-even-being-referenced-advice-on-debugging-this/68612>\
**Category:** New to Julia\
**Created:** [September 23, 2021, 3:57am UTC](https://discourse.julialang.org/t/variable-gets-updated-without-even-being-referenced-advice-on-debugging-this/68612 "2021-09-23T03:57:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sudhanshu\_Mishra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sudhanshu_mishra/32/29093_2.png) [@Sudhanshu\_Mishra](https://discourse.julialang.org/u/Sudhanshu_Mishra)\
**Post date:** [September 23, 2021, 3:57am UTC](https://discourse.julialang.org/t/variable-gets-updated-without-even-being-referenced-advice-on-debugging-this/68612/1 "2021-09-23T03:57:36Z")

</div>

This is a code for the function [lyapunovs](https://github.com/SudoMishra/ChaosTools.jl/blob/edit_rescale/src/chaosdetection/lyapunovs.jl) [link attached] in ChaosTools.jl .  
The code below is a bit modified as I am using `r=[1.0,1.0]` here instead of `r=copy(get_state(pinteg,2)` for debugging purposes. I have tried `r=deepcopy(get_state(pinteg,2)` as well and yet this is happening . As far as I know, by using `deepcopy` this r should be independent and should only change in the `rescale` function where I am passing it.

```julia
t0 = pinteg.t
    # array to preallocate state for rescale
    r = [1.0,1.0]#deepcopy(pinteg.u[1])#copy(pinteg.u[2])
    while pinteg.t < t0 + Ttr
        step!(pinteg, Δt)
        d = λdist(pinteg)
        lt ≤ d ≤ ut || rescale!(pinteg, d/d0, r)
    end

    t0 = pinteg.t
    d = λdist(pinteg)
    d == 0 && error("Initial distance between states is zero!!!")
    rescale!(pinteg, d/d0, r)
    λ = zero(d)
    while pinteg.t < t0 + T
        d = λdist(pinteg)
        #evolve until rescaling:
        while lt ≤ d ≤ ut
            step!(pinteg, Δt)
            d = λdist(pinteg)
            pinteg.t ≥ t0 + T && break
        end
        # local lyapunov exponent is simply the relative distance of the trajectories
        a = d/d0
        λ += log(a)
        rescale!(pinteg, a, r)
    end

```

I define a variable `r=[1.0,1.0]`. However, r is getting updated when the `step!` function is being called. `r` is not being referenced as far as I can see in the step function but still somehow it is changing. This seems like odd behavior to me, maybe I am making a silly mistake. I was trying to visualize why this using the debugger and I cannot understand why this is happening. Any advice on debugging this would be super helpful.  
I am attaching a gif showing this in the debugger below  
 ![2021-09-23-0930-27-online-video](https://global.discourse-cdn.com/julialang/original/3X/f/7/f742571bbef1bf931ecf49edef15bbdfb2e00572.gif)

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 23, 2021, 4:45am UTC](https://discourse.julialang.org/t/variable-gets-updated-without-even-being-referenced-advice-on-debugging-this/68612/2 "2021-09-23T04:45:27Z")

</div>

Well you’re modifying it:

[https://github.com/SudoMishra/ChaosTools.jl/blob/afa85ba2030c7df977c9bf2289fd0719ac7fb20d/src/chaosdetection/lyapunovs.jl#L339](https://github.com/SudoMishra/ChaosTools.jl/blob/afa85ba2030c7df977c9bf2289fd0719ac7fb20d/src/chaosdetection/lyapunovs.jl#L339)

and `set_state!` attaches it to the integrator:

[https://github.com/JuliaDynamics/DynamicalSystemsBase.jl/blob/264d463dccf193870b10c6365ac6e1ac7ffc47af/src/core/continuous.jl#L186](https://github.com/JuliaDynamics/DynamicalSystemsBase.jl/blob/264d463dccf193870b10c6365ac6e1ac7ffc47af/src/core/continuous.jl#L186)

I’m not 100% if that’s the correct `set_state!` method for your integrator, please do check by stepping into that call instead of over it, but that’s what I think is going on when later on `step!` changes the integrator state.
