130. RAII

Question.5

A developer manages DMA channel allocation with RAII:

class DMAGuard {
   int channel;
public:
   DMAGuard() : channel(dma_alloc_channel()) {}
   ~DMAGuard() { dma_free_channel(channel); }
   int get() const { return channel; }
   DMAGuard(const DMAGuard&) = delete;
   DMAGuard& operator=(const DMAGuard&) = delete;
};

If a function using DMAGuard throws an exception or returns early, what happens to the DMA channel?

Need Help? Refer to the Quick Guide below

Select Answer