Opened 14 years ago

Last modified 14 years ago

#18 new enhancement

implementation of error checking for memory allocations

Reported by: bob Owned by: Paul Brossier
Priority: normal Milestone: 0.3.3
Component: corelib Version: 0.3.2
Severity: normal Keywords:
Cc:

Description


Change History (7)

comment:1 Changed 14 years ago by Paul Brossier

Hi bob,

sorry, could you elaborate on this?

Thanks, Paul

comment:2 Changed 14 years ago by Olivier Robert

Hi Paul, It would be interesting to elaborate a bit more indeed :) To put the problem in a context: While playing with the python interface, I found it was possible to "segfault" the python session if the user tries to allocate a really big fvec_t->data array with the new_fvec() function.

Here is a fix I wrote. I didn't put a lot of thought in the way it's implemented but it works.

fvec_t * new_fvec( uint_t length) {
  fvec_t * s = AUBIO_NEW(fvec_t);
  if (s!=NULL) {
    uint_t j;
    s->length = length;
    s->data = AUBIO_ARRAY(smpl_t, s->length);
    if (s->data==NULL) {
      AUBIO_FREE(s);
      s=NULL;
    } else {
      for (j=0; j< s->length; j++) {
        s->data[j]=0.;
        AUBIO_MEMSET
      } 
    }
  }
  return s;
}

And then when invoking this function: (this is an example written in cython)

cdef class Fvec:
    cdef fvec_t *pt
    def __cinit__(self, unsigned int length):
        self.pt = new_fvec(length)
        if self.pt is NULL:
            python_exc.PyErr_NoMemory()

BTW: This is an informal way to present my work, and it is still in progress, but I'm writing a cython interface for aubio. It makes possible to feed aubio with numpy arrays. If you are interested I'm wishing to create a new branch in your repository.

Olivier

comment:3 Changed 14 years ago by Paul Brossier

Hi,

have you checked the new python interface in aubio git's head, in source:interfaces/python? it generates C code for direct integration with numpy. Still very crude and undocumented though. You need to use waf, then generate the code, then run python setup.py build, something along these lines.

Thanks, Paul

comment:4 Changed 14 years ago by anonymous

Hi Paul, Thanks for pointing it. I apparently didn't look enough in the code repository! I'm testing it. It is very robust, no segfault!

Now regarding the topic of this thread, I believe it is important to check for "no-memory" errors. The new Python interface doesn't need it but it will benefit to plain C programs.

Olivier

comment:5 Changed 14 years ago by Paul Brossier

ok, it seems pretty safe to do such a thing. however, i wonder about the overhead, that's a lot of additional ifs. i will have a look. any figures would be much appreciated.

comment:6 Changed 14 years ago by anonymous

I found this :
http://vilimpoc.org/research/raii-in-c/

Here is an implementation :

fvec_t * new_fvec( uint_t length) {
  fvec_t * s = AUBIO_NEW(fvec_t);
  if (NULL==s) goto cleanup_generic;
  uint_t j;
  s->length = length;
  s->data = AUBIO_ARRAY(smpl_t, s->length);
  if (NULL==s->data) goto cleanup_s;
  for (j=0; j< s->length; j++) {
    s->data[j]=0.;
  }
  return s;
  cleanup_s:
    AUBIO_FREE(s);
  cleanup_generic:
    AUBIO_ERR ("attempt to allocate memory failed in new_fvec()");
    return NULL;
}

I found another document building on this idea even further :

http://toolchainguru.blogspot.com/2010/09/raii-in-c-too.html

comment:7 Changed 14 years ago by anonymous

fvec_t * new_fvec( uint_t length) {
  fvec_t * s = AUBIO_NEW(fvec_t);
  if (NULL==s) goto cleanup;
  uint_t j;
  s->length = length;
  s->data = AUBIO_ARRAY(smpl_t, s->length);
  if (NULL==s->data) goto cleanup;
  for (j=0; j< s->length; j++) {
    s->data[j]=0.;
  }
  return s;
  cleanup:
    AUBIO_ERR ("attempt to allocate memory failed in new_fvec()");
    if (s) AUBIO_FREE(s);
    return NULL;
}
Note: See TracTickets for help on using tickets.