Welcome to the forum!
It sounds like a neutral position switch problem - happens quite a bit in the D22, rarely in the D40.
Black smoke like that will possibly be turbo-related - but could also be EGR flooding the intake too if the EGR valve opens fully. I can't think of why the gear selection should have any connection to EGR though - or even turbo control, for that matter.
To test the neutral switch, drive along in 4th and while the smoke is bad, move the gear lever from side to side. If the smoke disappears while doing this, your NPS is bad and needs replacing (not sure if it can be cleaned).
If that doesn't fix it, it's a curly one - I can't think of any reason why 4th gear will cause any difference in fuel or air delivery.