8 years, 7 months ago.

App crashes when deleting PwmOut object

Hi All,

I'm trying to dynamically create/delete PwmOut objects to save power when not needed. Other posts have discussed various ways, however I've done it within a class, since the RGB is controlled with timer callbacks, so I couldn't use a static object that simply went out of scope when not needed.

class RGBControl {
public:
    RGBControl(PinName red, PinName green, PinName blue) {  
        
        _redPin = red;
        _greenPin = green;
        _bluePin = blue;                  
    }
     
    /** @param RGB values 0-255
    */
    void rgb(RGBColour c) {
        if(_red == NULL) {
            init();
        }
        if(c.brightness > 100) {
            c.brightness = 100;
        }    
        if(c.brightness < 0) {
            c.brightness = 0;
        }    
        _brightness = (float)c.brightness/100.0;
        _red->write(convert(c.red));
        _green->write(convert(c.green));
        _blue->write(convert(c.blue));   
    }

    void sleep(void) {
        if(_red) {
            delete _red;
            delete _green;
            delete _blue;
        }    
        _red = NULL;
    }

   
private:
    PinName _redPin;
    PinName _greenPin;
    PinName _bluePin;
    PwmOut *_red;
    PwmOut *_green;
    PwmOut *_blue;
    float _brightness;
   
    void init(void) {
        _red = new PwmOut(_redPin);
        _green = new PwmOut(_greenPin);
        _blue = new PwmOut(_bluePin);
        
        _red->period_us(RGB_PERIOD_US);    
        _green->period_us(RGB_PERIOD_US);
        _blue->period_us(RGB_PERIOD_US);          
    }    

    float convert(int value) {
        float nvalue = _brightness * (float)value / 255.0;
        
        // if (inverted)
            nvalue = 1.0 - nvalue; 
            
        return nvalue;
    }   
    
};

/* colour class
*/
class RGBColour
{
public:
    RGBColour(int r=0, int g=0, int b=0) {
        red = r;
        green = g;
        blue = b;        
        brightness = 100;
    }    
    
    bool isOn(void) {
        return (brightness && (red || green || blue));
    }    
    
    int red;        // 0 - 255
    int green;
    int blue;
    int brightness; // % 0 - 100
};

The code works if I comment out the sleep function (where the PwmOut objects are deleted). If I include the object deletion the program crashes.

Any feedback welcome.

EDIT: Problem occurs with static objects as well, perhaps bug in PwmOut implementation?

The following function also crashes program,

void forward(void) {
  PwmOut led(LED1);
  
  led = 0.5;
  
  wait(0.5);
}  

Although I'm not confident enough to start playing around with the mbed library, I did had a quick look at the PwmOut, and the only interesting thing I can see, is that there's a pwm_free function, presumably to free the resources, however as far as I can tell there's no destructor, so I wonder if it's being called at all.

Maybe this should be moved to the forum?

Andrew

Thanks for the replies. I've started a thread on this subject in the bugs & suggestions forum. My suspicion is that there's something required in the destructor that's particular to the nRF51822 platform, that's either not there or has been removed. Hopefully someone who understands the HAL layers will be able to resolve the problem

posted by Andrew Fox 25 Aug 2015

2 Answers

8 years, 7 months ago.

I have the same problem ! the appli crash while deleting a pwmout object.

I tried also to add a destructor of PwmOut class: (mbed-src/api/PwmOut.h)

    ~PwmOut() {
        pwmout_free(&_pwm);
    }

but it still crash, any ideas ?

8 years, 7 months ago.

If there is no destructor, then there is a standard destructor which just deletes member variables (which for PwmOut are just a few variables containing pointers). So it is perfectly fine to destruct a class without a destructor. However that should of course never result in a crash.