Fixed a serious memory leak bug in Lagrange function