Csound Csound-dev Csound-tekno Search About

Re: [Csnd] tracking segmentation faults / memory allocation

Date2005-08-20 13:06
FromSimon Schampijer
SubjectRe: [Csnd] tracking segmentation faults / memory allocation
Hi Istvan,
I don't change, reallocate or free the auxp pointer. But the
use of the buffer after the end of performance for a small
amount of time is possible. That would explain why the
performance went ok and the seg-fault happens at the end.
Hmm, I just call the redraw() method in the process function
and only when a new input frame is ready (fsig).  Probably
there is an Csound API function to know before Csound quit
so I could  set a value that the drawing function doesn't
access the buffer then. For your note of the changing
buffersize, would this be the safe way? 
if(p->buffer.auxp==NULL || (long) (buffersize *
sizeof(MYFLT)) > p->buffer.size)
	csound->AuxAlloc(csound, buffersize*sizeof(MYFLT),
&p->buffer);
    buffer = (MYFLT*)p->buffer.auxp;
    for(int i=0; i 
> Does your window class possibly use the buffer after the
> end of performance (even if only by a small amount of
> time) ? All memory allocated with AuxAlloc is
> automatically freed at end of performance (and also at end
> of section). Another possible error is if you change,
> reallocate, or free the auxp pointer in an AUXCH structure
> in any way other than calling AuxAlloc with a new size. In
> general, you should consider the contents of the AUXCH
> structure read-only.
> 
> Simon Schampijer wrote:
> 
> > I wonder if there is a way of tracking down segmentation
> > faults reported by csound which I get during the
> > development of plugin opcodes. Csound tidy up:
> > Segmentation fault (I get them after the performance
> > finishs correctly) One thing that could cause the
> > problem is memory allocation which is  done by using the
> > AUXCH structure.  
> > in the data structure: 
> > AUXCH buffer;
> > the init function:
> > if(p->buffer.auxp==NULL)
> >     csound->AuxAlloc(csound, buffersize*sizeof(MYFLT),
> > &p->buffer);
> 
> Note that this is not safe if a new call of the opcode
> requests a larger buffer size, assuming that the buffer
> size depends on some parameter of the opcode and is not
> constant. -- 
> Send bugs reports to this list.
> To unsubscribe, send email to
> csound-unsubscribe@lists.bath.ac.uk
-- 
Send bugs reports to this list.
To unsubscribe, send email to csound-unsubscribe@lists.bath.ac.uk

Date2005-08-20 13:22
FromIstvan Varga
SubjectRe: [Csnd] tracking segmentation faults / memory allocation
Simon Schampijer wrote:

> I don't change, reallocate or free the auxp pointer. But the
> use of the buffer after the end of performance for a small
> amount of time is possible. That would explain why the
> performance went ok and the seg-fault happens at the end.
> Hmm, I just call the redraw() method in the process function
> and only when a new input frame is ready (fsig).  Probably
> there is an Csound API function to know before Csound quit
> so I could  set a value that the drawing function doesn't
> access the buffer then.

The "perform" routine of an opcode is not called after the end
of performance; so, unless you have code that runs in a separate
thread (possibly as a callback), it is not likely that the buffer
is accessed after the end of performance.
Other than the above, is it possible that you have a simple bug
like writing past the end (or before the beginning) of the buffer,
or some other kind of memory corruption ?

> For your note of the changing buffersize, would this be the safe way?
> if(p->buffer.auxp==NULL || (long) (buffersize *
> sizeof(MYFLT)) > p->buffer.size)
> 	csound->AuxAlloc(csound, buffersize*sizeof(MYFLT),
> &p->buffer);
>     buffer = (MYFLT*)p->buffer.auxp;
>     for(int i=0; i 	buffer[i] = FL(0.0);

Actually, if you always clear the buffer to zero, you may just
call AuxAlloc always - it will not do anything other than clearing
the buffer if the size does not change.
-- 
Send bugs reports to this list.
To unsubscribe, send email to csound-unsubscribe@lists.bath.ac.uk