this makes me believe that I have not yet memorized the piece by heart
yet
Not necessarily true: Our brains (most people's - unless you have total recall...) memorize things using pattern analysis - in blocks. You know a particular sequence - break it up and you can get confused, but why do that? It's a nice challenging exercise, true. But that's not how you're going to play the music, so IMO it's not that important. (Unless you're constantly late for gigs - in which case you won't have to worry about it very long...)
i can sing the notes in my head I don't really know where its on the
keyboard
I think that's a different issue, not necessarily linked to memorization. Since memorization is your goal, you see this problem manifest in those terms, but getting something from your head to your instrument is always one of the fundamental challenges of playing music.
Training your brain and your ear and connecting them to your instrument so you can express the music that's in your mind is something every musician has to work on, most of us for a long time - even a lifetime. I've had musical lines in my head from the age of 5 and it's taken me 50 years to figure out how to express them on my instrument. (I'm not all that talented and I'm not a full-time musician - some people get there quite quickly.)
In general, rather than worry about that "bad habit", I'd say focus less on mechanical memorization per-se, and think more about ear training, generalization and theoretical analysis of what you're playing. Those are skills that will make it easier to play something/anything - "from memory", because you won't have memorize lots of specific details. You'll understand the theory and the patterns underlying the music you're playing, and the right notes will logically follow. Rudimentary example: It's a lot easier to remember "G7" - a G Dominant 7th chord, than it is to remember G-B-D-F. You have one data point to remember, instead of 4!
Analyzing, generalizing and understanding the common, all encompassing patterns that comprise a system is important for learning music, or any deep and complex discipline. (I was taught that approach originally when I studied Law.)
When I learn something new, I read through the chart and then analyze it before I play the piece. When I hit it, I already have a theoretical blueprint in my head of the music I'm going to play: OK - this is a 32 bar tune with an 8 bar head that you play twice. Then it's a II-V-I that starts in Bb, and moves down through the circle of 5ths every 8 measures, with a chromatic section in the last 2 bars to bring it back to the top... - the result is that a large percentage of what I have to play comes automatically. I only have to remember specifically those parts that are different - unique to that particular piece of music. In that example, maybe I'd only have to remember the details of the head and maybe how that chromatic turnaround is played, although that could also be something standard.
Granted: I'm a bass player and I play Jazz and RnB, so sight-reading and absolute memorization are usually not important to what I do - it's more important for me to generalize. Still, IMO this idea is valid for all musicians, and I know that the good classical players use plenty of analysis to get to their interpretations. That certainly helps them to play from memory as well, when necessary.
An accomplished musician can read and analyze very quickly - virtually simultaneously - that's something to strive for. (I cannot do that. I take lessons from someone who can, and he makes a nice living playing bass for every possible sort of gig, in NYC no less.)