The bug was here. len+dictionary.get_num_elements() is no longer the correct size of dic_weights, since the size of the dictionary itself has changed from the previous line.