Smart pointers…

Can someone explain me the following behaviour:
I have the following smart pointer:

class AlsaHwParams
{
public:
    AlsaHwParams() : m_params(0)
    { snd_pcm_hw_params_alloca (&m_params); }
    ~AlsaHwParams()
    { if (m_params != 0) snd_pcm_hw_params_free (m_params); }
    operator snd_pcm_hw_params_t*() { return m_params; }
private:
    snd_pcm_hw_params_t *m_params;
};

Thing is: It doesn’t work. Alsa behaves very strange if I use it. Even if I make the m_params member public and access it directly. The only way it works is to not allocate from inside the class but outside like

snd_pcm_hw_params_alloca (&(hwparams.m_params));

Update: Seems replacing _alloca with _malloc fixes it.
Update: Duh, that is because alloca allocates on stack and not on heap.

Leave a Reply

Your email address will not be published. Required fields are marked *