Help with code for Midi legato playing and pitchbend

I need to keep track of the order of notes played so note-off and pitchbend commands are treated correctly. I guess i need a note-off to retrigger the pitchbend for previously played notes if they have not been released yet. could this be done with an array listing active notes, last played notes? so the program knows when and not to send the pitchbend?