fix for Bug #3085:

weights array is only allocated for (l_count+1) elements,
but then weights[l_count+1] element is accessed.
Merge pull request #987 from jet47:bug-3085-fix