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

  • From: "Stephan Assmus" <superstippi@xxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Fri, 12 Sep 2008 17:20:53 +0200

-------- Original-Nachricht --------
> Datum: Fri, 12 Sep 2008 15:34:16 +0200 (CEST)
> Von: "Fredrik Modéen" <fredrik@xxxxxxxxx>
> An: haiku-development@xxxxxxxxxxxxx
> Betreff: [haiku-development] Re: object on the hype,      using it as 
> parameter and then use delete

> 
> > 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:
> >
> >
> 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.
> Had to look up std::nothrow
> http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=170
> 
> need we write it like this?
> 
> //standard C++ code
> #include <new> //required for nothrow new
> 
> CWindow p;
> p = new(std::nothrow) DerivedWind;
> if (!p) //fine now
> {
>  cout<<"allocation failure!"
>  exit(1);
> }

Yes, this code would actually work and detect the allocation failure. However, 
please write "if (p == NULL)" and please think twice about ever using "exit()". 
For example *never* use this in library or add-on code. I have fixed such code 
for example in the PNG and JPG translators. These translators were simply 
exiting the application when confronted with certain corrupted JPGs or PNGs. 
That was just great when the translators were used in a mission critical 
application. All library code is supposed to handle error conditions to the 
best they can manage and forward the problem to the application code that uses 
them. In application code, such as preflets and applications, it is mostly 
acceptable to be less strict, depending on the situation. Obviously in 
DriveSetup, one needs to be more careful than in the Mouse preflet.

Best regards,
-Stephan

Other related posts: