stan-dev/stan — pull request #548 by bob-carpenter (2014-02-11 18:18:17+00:00)
Title: Bugfix/issue 543 grad functional leak

#### Summary:

Fix memory leak in gradient calculations.

#### Intended Effect:

Encapsulate the memory management in the autodiff.hpp functionals. Specifically put the calls to the function which might throw an exception in a try/catch block.

#### How to Verify:

You can run before/after tests or look at the new unit test in autodiff_test.cpp, which does a bunch of calls to gradient with a function hard-coded to use var instances and throw an exception. Before the fix, memory leaked and went up to 37MB on stack alloc w/o test.

#### Side Effects:

It encapsulates the memory management, so outside callers shouldn't have to deal with recover_memory calls.

#### Documentation:

Not user facing, but I documented the functions.

#### Reviewer Suggestions:

Michael or Daniel.