fixed the foreach API and finally fixed foreach itself. memleak left.