List:MySQL++« Previous MessageNext Message »
From:Jonathan Wakely Date:June 11 2009 8:43am
Subject:Re: Why dynamic allocation for Option objects?
View as plain text  
2009/6/11 Kevin Regan:
>
> Let's take as an example, the function in question:
>
> std::string
> DBDriver::set_option(Option* o)
> {
>        std::ostringstream os;
>        std::auto_ptr<Option> cleanup(o);
>
> Now, let's imagine that the constructor for std::ostringstream can throw an exception
> (maybe it allocates a working buffer).  In that event, the memory for the option will
> not be reclaimed when a std::bad_alloc exception is thrown.

The ostringtream default constructor shouldn't throw, but it wouldn't
hurt to swap those two lines so that the pointer is guarded
immediately.

I detest raw pointers to dynamically allocated memory, and would
advise using an auto_ptr in the interface, so it is explicit that
ownership is being transferred and that the address of a static or
local variable should not be passed in.

std::string
DBDriver::set_option(std::auto_ptr<Option> o);

The existing function could then be changed to simply forward to that
one, and ideally deprecated.

Jonathan
Thread
Why dynamic allocation for Option objects?Kevin Regan6 Jun
  • Re: Why dynamic allocation for Option objects?Warren Young6 Jun
    • RE: Why dynamic allocation for Option objects?Kevin Regan11 Jun
      • Re: Why dynamic allocation for Option objects?Jonathan Wakely11 Jun
        • RE: Why dynamic allocation for Option objects?Kevin Regan13 Jun
          • RE: Why dynamic allocation for Option objects?Kevin Regan13 Jun
          • Re: Why dynamic allocation for Option objects?Jonathan Wakely13 Jun
            • RE: Why dynamic allocation for Option objects?Kevin Regan14 Jun
              • Re: Why dynamic allocation for Option objects?Jonathan Wakely14 Jun
                • RE: Why dynamic allocation for Option objects?Kevin Regan14 Jun
                  • Re: Why dynamic allocation for Option objects?Jonathan Wakely15 Jun
                    • RE: Why dynamic allocation for Option objects?Kevin Regan15 Jun
                      • Re: Why dynamic allocation for Option objects?Jonathan Wakely15 Jun
              • Re: Why dynamic allocation for Option objects?Warren Young16 Jun
        • Re: Why dynamic allocation for Option objects?Warren Young16 Jun