invalid pointer...

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

invalid pointer...

Alan O Cinneide
Hello lads,

I'm getting an error with an opcode I'm struggling to
write.  It's working but I get an error after
performance.  It has something to do with the way I'm
allocating space for function tables or arrays within
the opcode:

 *** internal error: mfree() called with invalid
pointer (0x0x8148fb0)

Anyone have any clues about this one?

Thanks,
Alan


               
___________________________________________________________
How much free photo storage do you get? Store your holiday
snaps for FREE with Yahoo! Photos http://uk.photos.yahoo.com
--
Send bugs reports to this list.
To unsubscribe, send email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: invalid pointer...

Istvan Varga
Alan O Cinneide wrote:

> I'm getting an error with an opcode I'm struggling to
> write.  It's working but I get an error after
> performance.  It has something to do with the way I'm
> allocating space for function tables or arrays within
> the opcode:
>
>  *** internal error: mfree() called with invalid
> pointer (0x0x8148fb0)
>
> Anyone have any clues about this one?

It would be easier to find out what the problem is with some
code, as the message above is a generic error caused by freeing
the same memory twice, or freeing something that was allocated
with a different interface.
--
Send bugs reports to this list.
To unsubscribe, send email to [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: invalid pointer...

Alan O Cinneide
Here's the init and the process functions of the code
I've written.  It's quite short and shouldn't take to
long to sort through.

It's a opcode called TUNE that takes frequency values
and "tunes" them to a more acceptable intervals.

It seems you always have the answers to my questions,
Istvan, thanks!

Alan
--- Istvan Varga <[hidden email]> wrote:

> Alan O Cinneide wrote:
>
> > I'm getting an error with an opcode I'm struggling
> to
> > write.  It's working but I get an error after
> > performance.  It has something to do with the way
> I'm
> > allocating space for function tables or arrays
> within
> > the opcode:
> >
> >  *** internal error: mfree() called with invalid
> > pointer (0x0x8148fb0)
> >
> > Anyone have any clues about this one?
>
> It would be easier to find out what the problem is
> with some
> code, as the message above is a generic error caused
> by freeing
> the same memory twice, or freeing something that was
> allocated
> with a different interface.
> --
> Send bugs reports to this list.
> To unsubscribe, send email to
> [hidden email]
>

       
       
               
___________________________________________________________
Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voicemail http://uk.messenger.yahoo.com
int tune_init(ENVIRON *csound, tune *p)
{
        // Get the function table and function table length, error if invalid.
        if (!(csound->GetTable(csound, (int) *p->ifn, &p->ftlen)))
        {
                csound->Message(csound, "Unable to find function table, defaulting to equal-tempered scale...\n");

                p->ftlen = 12;

                csound->AuxAlloc(csound, sizeof(MYFLT)*p->ftlen, &p->ftable);
        }
        else
        {
                csound->AuxAlloc(csound, sizeof(MYFLT)*p->ftlen, &p->ftable);
                p->ftable.auxp = csound->GetTable(csound, (int) *p->ifn, &p->ftlen);
        }

        if (p->distance.auxp==NULL)
        {
                csound->AuxAlloc(csound, sizeof(MYFLT)*p->ftlen, &p->distance);
        }

        return OK;
}

int tune_process(ENVIRON *csound, tune *p)
{
        MYFLT freq = *p->kfreq;
        MYFLT tonic = *p->itonic;
        MYFLT *ftable = (MYFLT *) p->ftable.auxp;
        MYFLT *distance = (MYFLT *) p->distance.auxp;
        int ftlen = p->ftlen;

        int oct_cnt=0, i, j;
        MYFLT interval;

        // Function table contains intervals in cents relative
        // to pitches within octave of scale built on tonic.
        if (ftlen==12)
        {
                for (i=0; i<p->ftlen; i++)
                {
                        ftable[i] = i*100.f;
                }
        }

        // Bring tonic frequency below 20 Hz so all intervals are positive
        if (tonic==0.f) tonic = 13.75;
        else while (tonic > 20) tonic /= 2;

        // Ascertain interval
        interval = (MYFLT) 1200.f*log(freq/tonic)/log(2.f);

        while (interval >= 1200)
        {
                interval -= 1200;
                oct_cnt++;
        }

        // Find the closest function table entry to interval
        for (i=0; i<ftlen; i++)
        {
                distance[i]=fabs(interval-ftable[i]);
        }

        // Search for closest interval...
        for (i=1, j=0; i<ftlen; i++)
        {
                j = (distance[i] < distance[j] ? i : j);
        }

        // Proper interval is the function table entry the closest to the given
        // frequency, just add on missing octaves...
        interval = ftable[j] + 1200*oct_cnt;

        // Calculate and output tuned frequency
        *p->ktune = (MYFLT) tonic*pow(2.f, interval/1200.f);

        return OK;
}
Reply | Threaded
Open this post in threaded view
|

Re: invalid pointer...

Istvan Varga
Alan O Cinneide wrote:

> else
> {
> csound->AuxAlloc(csound, sizeof(MYFLT)*p->ftlen, &p->ftable);
> p->ftable.auxp = csound->GetTable(csound, (int) *p->ifn, &p->ftlen);
> }

This is a bug. You should not change, free, or reallocate the auxp pointer
in an AUXCH structure, because it is managed internally by Csound. Try using
a separate pointer in the opcode structure that is either a copy of the auxp,
or points to the function table, depending on whether the table is found.
In fact, you do not need AuxAlloc at all, as both the size and contents of the
default table are always the same, so you may just add something like

static const MYFLT default_table[12] = {
     FL(0.0), FL(100.0), FL(200.0), FL(300.0), FL(400.0), FL(500.0),
     FL(600.0), FL(700.0), FL(800.0), FL(900.0), FL(1000.0), FL(1100.0)
};

> // Find the closest function table entry to interval
> for (i=0; i<ftlen; i++)
> {
> distance[i]=fabs(interval-ftable[i]);
> }
>
> // Search for closest interval...
> for (i=1, j=0; i<ftlen; i++)
> {
> j = (distance[i] < distance[j] ? i : j);
> }

It is possible to find the closest table entry without using a
temporary array, thus eliminating the need for the second AuxAlloc
as well:

         MYFLT dst_min;

  // Search for closest interval...
        dst_min = fabs(interval - ftable[0]);
  for (i=1, j=0; i<ftlen; i++)
  {
                MYFLT dst = fabs(interval - ftable[i]);
                if (dst < dst_min) {
                        j = i;
                        dst_min = dst;
                }
  }
--
Send bugs reports to this list.
To unsubscribe, send email to [hidden email]