The teaching is from the Milindapañha.
'Venerable Nāgasena, these givers when they bestow their offerings, devote them specifically to former (relatives) now departed, saying: "May this gift benefit such and such." Now do they (the dead) derive any benefit therefrom?'
'Some do, O king, and some do not.'
'Which then are they that do, and which do not?'
'Those who have been reborn in purgatory, O king, do not; nor those reborn in heaven; nor those reborn as animals. And of those reborn as Pretas three kinds do not: the Vantāsikā (who feed on vomit), the Khuppipāsino (who hunger and thirst.), the Nijjhāmatañhikā (who are consumed by thirst). But the Paradattūpajīvino (who live on the gifts of others) they do derive profit, and those who bear them in remembrance do so too.'
'Then, Nāgasena, offerings given by the givers have run to waste, and are fruitless, since those for whose benefit they are given derive no profit therefrom.'
'No, O king. They run not to waste, neither are fruitless. The givers themselves derive profit from them.'
(Mil. 294-7, Th. Rhys Davids trans.)
For the full dialogue:http://www.sacred-texts.com/bud/sbe36/sbe3606.htm
The Great Sage did thus proclaim
With many a diverse chosen name
Vipassanā stilled and purified,
That to Emergence is allied.
Saṃsāra-cycle’s swamp of pain
Is vast and terrible; a man
Wisely should strive as best he can,
If he would this Emergence gain.