[haiku-development] Re: object on the hype, using it as parameter and then use delete

  • From: Ingo Weinhold <ingo_weinhold@xxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Fri, 12 Sep 2008 15:38:08 +0200

On 2008-09-12 at 15:13:24 [+0200], Fredrik Modéen <fredrik@xxxxxxxxx> wrote:
> 
> > On Fri, 12 Sep 2008 13:56:21 +0200 (CEST), Fredrik Modéen wrote
> >> this are how it's setup to work, it's only part of it but it showas what
> >> i'm talkning about.
> >>
> >> ...
> >>
> >> void
> >> IconView::_SetIcon(BBitmap* mini)
> >> {
> >>  BAppFileInfo info();
> >>  if (mini != NULL || force)
> >>   info.SetIconForType(fType.Type(), mini, B_MINI_ICON);
> >> }
> >
> > In that case you would just track down what SetIconForType() does. If it
> > copies the bitmap, then you're fine. Otherwise this is probably a false
> > positive.
> >
> > It seems that you are not subscribed to the commits mailing list. Please
> > make
> > sure you are subscribed, as we are sometimes replying to commits there
> > like I
> > did for your change in r27438:
> Ah yes in deed I need to subscribe to that list (done now..)
> 
> >
> > https://lists.berlios.de/pipermail/haiku-commits/2008-September/016893.html
> >
> > You make the same assumption that new cannot fail in r27448 which I find a
> > bad
> > idea. You could say that it is true that new cannot return NULL, since it
> > should actually throw an error and not return NULL. However this is not a
> > reason to strip the NULL checks, but to make new into new(std::nothrow)
> > which
> > actually returns NULL on a failed allocation (which can always happen). I
> > thought we had the general consensus that we use new(std::nothrow) and
> > always
> > check the result, but I'm not sure how the other see it.
> 
> So it would be better to try to make a new new when I found a null like this
> 
> if (_fDevice == NULL)
>  _fDevice = new BList();
> 
> if(_fDevice != NULL){
>  continue..
> }
> 
> or some thing similar.

Nope, what Michael is referring to is, that you should never use the new 
operator like this:

        Foo* x = new Foo(...);
        x->DoSomething();

Because this operator version will throw an exception when the memory cannot 
be allocated. Instead use the nothrow version and check the result explicitly:

        Foo* x = new(std::nothrow) Foo(...);
        if (x == NULL)
                return B_NO_MEMORY;

        x->DoSomething();

CU, Ingo

Other related posts: