snd-add mem leak and snd-xform bugs EXPLAINED!

Using Nyquist scripts in Audacity.
Post and download new plug-ins.
Forum rules
If you require help using Audacity, please post on the forum board relevant to your operating system:
Windows
Mac OS X
GNU/Linux and Unix-like
Post Reply
Paul L
Posts: 1782
Joined: Mon Mar 11, 2013 7:37 pm
Operating System: Please select

snd-add mem leak and snd-xform bugs EXPLAINED!

Post by Paul L » Mon Nov 25, 2013 12:28 am

I haven't yet built Audacity to test these fixes, but I think I found the causes of known bugs while reading some source code this weekend.

I refer to source code versions found here: http://code.google.com/p/audacity/sourc ... 253Dclosed
  • The memory leak discussed in this thread: http://forum.audacityteam.org/viewtopic ... 39&t=75022
    I believe this is the fault of lines 471:

    Code: Select all

            susp->s1 = NULL;
    and 721:

    Code: Select all

            susp->s2 = NULL;
    of file add.c. I believe these lines can just be commented out without harm. I believe the bug in the addition of two sounds will happen whenever two sounds are added with snd-add, and the sum is completely evaluated, and one sound outlasts the physical stop of the other sound (by at least one internal block of samples), and this later-ending sound is not otherwise referenced: then it leaks. Note that if you add many sounds with sim or sum, the last two are added with snd-add, and the others accumulated right to left onto that result with more calls to snd-add. Perhaps nobody detected this common occurrence until I wrote effects that cause this to happen many thousands of times?
  • The problems with results of snd-xform given to snd-add, with the resulting sound sometimes incorrect in a way dependent on the sequence of the summands, discussed here: http://forum.audacityteam.org/viewtopic ... 39&t=72398

    The fault is again in add.c, not in snd-xform. Change line 405 to:

    Code: Select all

                susp->s1->get_next == SND_get_next && susp->s1->stop == MAX_STOP &&
    and analogously at line 650:

    Code: Select all

                susp->s2->get_next == SND_get_next && susp->s2->stop == MAX_STOP &&
    
    Those changes will have the effect of disabling some "optimizations" in snd-add for more structure sharing, which were applied incorrectly to sounds that were shortened by snd-xform: the shortening was ignored and the underlying sound sequence copied to its physical stop. A better fix might be written that does not sacrifice sharing in this case, but it would not be as simple.

    To see the bug, generate 1 second of a 100Hz wave, do this in Nyquist prompt:

    Code: Select all

            (defun extract-global (snd start end)
              (let* ((t0 (snd-t0 snd))
                (result
                 (shift-time (snd-xform snd (snd-srate snd) t0 start end 1.0) start)))
                result))
    
        (let* (
    
        (s1 (extract-global s 0.5 0.51))
        (s2 (extract-global s 0.6 0.61))
        (s3 (extract-global s 0.7 0.71))
    
        )
     (sum s3 s2 s1)
    )
    Whereas if you change the result to (sum s1 s2 s3), you get a different (and correct) result, which should just be three isolated samples of the original wave.
Last edited by Paul L on Wed Nov 27, 2013 4:35 am, edited 1 time in total.

steve
Site Admin
Posts: 80679
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: snd-add mem leak and snd-xform bugs EXPLAINED!

Post by steve » Mon Nov 25, 2013 12:50 am

Nyquist is developed and maintained by Roger Dannenberg at CMU. He rarely visits this forum, so you would probably do better to contact him directly.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Paul L
Posts: 1782
Joined: Mon Mar 11, 2013 7:37 pm
Operating System: Please select

Re: snd-add mem leak and snd-xform bugs EXPLAINED!

Post by Paul L » Mon Nov 25, 2013 1:23 am

I send rbd a message via this board, which should appear in his email.

Is there an official source repository for Nyquist separate from Audacity? So we don't fork the library?

I know you were concerned to track the memory leak bug, and now I think I know the source code mistake.

steve
Site Admin
Posts: 80679
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: snd-add mem leak and snd-xform bugs EXPLAINED!

Post by steve » Mon Nov 25, 2013 1:33 am

Paul L wrote:Is there an official source repository for Nyquist separate from Audacity? So we don't fork the library?
The Nyquist home page is here: http://www.cs.cmu.edu/~music/music.software.html
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Paul L
Posts: 1782
Joined: Mon Mar 11, 2013 7:37 pm
Operating System: Please select

Re: snd-add mem leak and snd-xform bugs EXPLAINED!

Post by Paul L » Mon Nov 25, 2013 1:48 am

What about Xlisp?

I was pondering how Xlisp might be made faster but maybe I should curb that enthusiasm.

steve
Site Admin
Posts: 80679
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: snd-add mem leak and snd-xform bugs EXPLAINED!

Post by steve » Mon Nov 25, 2013 10:14 am

Paul L wrote:What about Xlisp?
http://www.xlisp.org/
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Paul L
Posts: 1782
Joined: Mon Mar 11, 2013 7:37 pm
Operating System: Please select

Re: snd-add mem leak and snd-xform bugs EXPLAINED!

Post by Paul L » Mon Nov 25, 2013 1:36 pm

We use Xlisp 2.0, which is based on a subset of Common Lisp not Scheme, and besides 3.0 has not been updated in over ten years!

So can we feel free to fork it?

I was fantasizing about compiling Lisp, not necessarily to machine code but to a more rapidly interpreted form with optimizations applied. It could be someone's summer project.

steve
Site Admin
Posts: 80679
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: snd-add mem leak and snd-xform bugs EXPLAINED!

Post by steve » Wed Nov 27, 2013 12:29 am

Paul L wrote:The problems with results of snd-xform given to snd-add, with the resulting sound sometimes incorrect in a way dependent on the sequence of the summands, discussed here: http://forum.audacityteam.org/viewtopic ... 39&t=72398

The fault is again in add.c, not in snd-xform. Change line 405 to:

Code: Select all
susp->s1->get_next == SND_get_next && susp->s1->stop != MAX_STOP &&



and analogously at line 650:

Code: Select all
susp->s2->get_next == SND_get_next && susp->s2->stop != MAX_STOP &&



Those changes will have the effect of disabling some "optimizations" in snd-add for more structure sharing, which were applied incorrectly to sounds that were shortened by snd-xform: the shortening was ignored and the underlying sound sequence copied to its physical stop. A better fix might be written that does not sacrifice sharing in this case, but it would not be as simple.

To see the bug, generate 1 second of a 100Hz wave, do this in Nyquist prompt:

Code: Select all
(defun extract-global (snd start end)
(let* ((t0 (snd-t0 snd))
(result
(shift-time (snd-xform snd (snd-srate snd) t0 start end 1.0) start)))
result))

(let* (

(s1 (extract-global s 0.5 0.51))
(s2 (extract-global s 0.6 0.61))
(s3 (extract-global s 0.7 0.71))

)
(sum s3 s2 s1)
)



Whereas if you change the result to (sum s1 s2 s3), you get a different (and correct) result, which should just be three isolated samples of the original wave.
I think you are close to a solution, but I tried that modification and your test case, but the result is still the same.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Paul L
Posts: 1782
Joined: Mon Mar 11, 2013 7:37 pm
Operating System: Please select

Re: snd-add mem leak and snd-xform bugs EXPLAINED!

Post by Paul L » Wed Nov 27, 2013 4:33 am

Oh duhh, I wrote != where it should have been == and twice. I edited my previous posting.

Did you try the memory leak fix?

Paul L
Posts: 1782
Joined: Mon Mar 11, 2013 7:37 pm
Operating System: Please select

Re: snd-add mem leak and snd-xform bugs EXPLAINED!

Post by Paul L » Wed Dec 04, 2013 5:40 am

At last! I built Audacity for Windows today, tried my fix, and verified that the sum does not depend on the sequence of the summands.

Now, to make a patch and publicize it in appropriate boards.

Post Reply