Dovid Hamelech was punished because he called the words of Torah "zemiros" [Sotah 35] זמירות היו לי חקיך. Nevertheless we say that pasuk in tehillim which would imply that he did nothing wrong.
Chazal say that Dovid was punished for asking Hashem to be tested [Sanhedrin 107] yet that ALSO made its way into tehillim בחנני השם ונסני, implying that he did nothing wrong.
An example to explain: The gemara [Brachos 31] says that one who fasts on Shabbos for a bad dream can rip up an evil decree even of 70 years. Yet the gemara continues that he is held accountable for nullifying his oneg Shabbos. The way to make up for that is to fast a different day as atonement. Meaning, the heter to fast on Shabbos is חובת האברים an obligation upon the body but the heart should still have reservations because he will miss out on oneg Shabbos.
Dovid was correct when he asked Hashem to test him but in his heart he should have had reservations, as we ask every day in davening אל תביאנו לידי נסיון don't test us. Because he didn't have reservations about asking he failed the test [with Bas Sheva].
Similarly, Dovid was correct in calling Torah "zemiros" but he should have had some reservations in his heart because the Torah might thereby be cheapened.
However, after Dovid was punished the tikkun was made and the Jewish people can say those psukim [calling Torah zemiros and asking for tests].
A person should at first learn even if his motivations are impure but he should have reservations in his heart and he should know that the ultimate goal is to learn only for the purest of motivations.
The breaking of the luchos, however, was met with Hashem's complete and unequivocal approval. No reservations. As the gemara says [Menachos 99] "Sometimes the nullification of the Torah [for example, missing learning to attend a wedding] is its fulfillment." לפעמים ביטולה זוהי קיומה!
[Maran HaRav Hutner - שבועות תשל"א מובא בספר רשימות לב]
I once heard a line - "Somebody who doesn't know when to close his gemara shouldn't open it." Sometimes it is necessary to close the gemara and one should have no reservations [as we just learned from the gemara in Menachos].
Of course when not involved in important mitzva projects and other necessary activities every available moment should be spent learning.