COCKOS
CONFEDERATED FORUMS
Cockos : REAPER : NINJAM : Forums
Forum Home : Register : FAQ : Members List : Search :

Go Back   Cockos Incorporated Forums > Cockos and Friends Software Discussion > WDL users forum

Reply
 
Thread Tools Display Modes
Old 08-08-2017, 05:59 AM   #1
mibes
Human being with feelings
 
Join Date: Apr 2017
Posts: 30
Default confusing malloc error problem with chunks example

I've been messing around with the stepper tool in the chunks example of WDL-ol. I've made it so the number of steps displayed is dynamically adjustable. Fine.

But when the destructor runs (closing the plugin) I get an error about trying to delete an unallocated pointer. I don't understand why because I am testing for nulls before deleting.


this is the significant part of the constructor:

Code:
  {
  ...

    float sliderWidth = ((float) mDrawRECT.W() / (float) mStepsDisplayed);

    mSteps = new double[numSliders];
    for(int i=0; i<numSliders; i++)
    {
        mSteps[i] = 0.;
    }

    for(int i=0; i<mStepsDisplayed; i++)
    {
      int lpos = (i * sliderWidth);
      mSteps[i] = 1.;

      mSliderBounds[i] = new IRECT(mDrawRECT.L + lpos , mDrawRECT.T, mDrawRECT.L + lpos + sliderWidth, mDrawRECT.B);
    }

    mHandleWidth = handleWidth;
  }

...

and this is the destructor:

Code:
  ~MultiSliderControlV()
  {
    delete [] mSteps;
      

    for(int i=0; i<mNumSliders; i++)
    {
        if (mSliderBounds[i] != NULL)
        {
            delete mSliderBounds[i];
        }
    }
  }
mibes is offline   Reply With Quote
Old 08-08-2017, 06:40 AM   #2
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 6,603
Default

Checking for null pointers won't help if you have pointers that were not initialized to begin with. (C++ generally does not initialize anything automatically to zeros, so maybe you have garbage pointers initialized to some random values. Attempting to use delete on such pointers is not going to end well.)

In any case, you should redesign the code to use safer C++ idioms. (Use objects like IRECT as values instead of using them via pointers/heap allocations and use container classes instead of new[n]/delete[].)
__________________
For info on SWS Reaper extension plugin (including Xenakios' previous extension/actions) :
http://www.sws-extension.org/
https://github.com/Jeff0S/sws
--
Xenakios blog (about HourGlass and λ) :
http://xenakios.wordpress.com/
Xenakios is offline   Reply With Quote
Old 08-08-2017, 06:46 AM   #3
mibes
Human being with feelings
 
Join Date: Apr 2017
Posts: 30
Default

Gotcha! Thanks

I'll have a crack at redesigning it. I was just reworking WDL-ol example, so wasn't my chosen technique to start with really!

If I did want to just patch this up I guess initialising all the pointers to null in the constructor would do the trick though.
mibes is offline   Reply With Quote
Old 08-11-2017, 09:24 AM   #4
earlevel
Human being with feelings
 
Join Date: Dec 2015
Posts: 236
Default

In addition to the remarks from Xenakis (and yes, I'd just make it an array of IRECTs instead of an array of pointers to IRECTs, then there is no delete needed):

In the constructor, you're looking through the mSliderBounds array from 0 to mStepsDisplayed (and not zeroing out the rest). In the destructor, you're loop 0 to mNumSliders—why not mStepsDisplayed? Anyway, the combination of those two problems ensures you'll try to deallocate garbage.

But if you use an array of IRECTs (not pointers), you won't need to deallocate them.
earlevel is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 11:56 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2017, vBulletin Solutions Inc.