Re: Why it is not good code for constructor



Hi Ben,


Since the memory was allocated in C::C, it is most likely C::~C that is
responsible for freeing it,

How can C's destructor free the memory? There is no member variable in class
C pointed to the memory. :-)

Please feel free to correct me if I am wrong.


regards,
George

"Ben Voigt [C++ MVP]" wrote:


"Alexander Grigoriev" <alegr@xxxxxxxxxxxxx> wrote in message
news:OgcmXl%23RIHA.5976@xxxxxxxxxxxxxxxxxxxxxxx
Are you sure argument of B() is evaluated no immediately before B() is
called? If, suppose, the base classes are constructed in B, A order, then
if A() throws, ~B() will be called.

Since the memory was allocated in C::C, it is most likely C::~C that is
responsible for freeing it, but C::~C will not be called, and a function-try
handler cannot access the pointer, while a catch handler in local scope will
not be executed.

If B::~B is freeing the pointer, then that would be correct usage of RAII
and safe. But the example being discussed makes it clear this isn't the
case, C is responsible for freeing the memory.


"Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx> wrote in message
news:%23K3hjF9RIHA.5524@xxxxxxxxxxxxxxxxxxxxxxx

"George" <George@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:75F1E950-242F-46F3-9500-5BAA7FAC2AB6@xxxxxxxxxxxxxxxx
Hello everyone,


Here is a sample from Dr. Dobb C++. In the analysis, the code is bad
below.

But I do not think the code is bad,

1. if bad_alloc is thrown in new int[], we just catch it and write some
log;
2. if there are any exception in B's constructor, we will also be in
catch
block and we could also write some log.

Why it is bad code? Any comments?

(I do not agree that there is resource leak, since if we met with
bad_alloc
in new int[], there is no memory allocated at all, so no root of
memory/resource leak).

The code you provided isn't sufficient to guarantee that.

What if:

class C : public B, public A
{
...
};

now if new int[n] completes ok, but the constructor for A throws, the
array is leaked with no chance to free it.



http://www.ddj.com/cpp/184401297

[Code]
C::C(int)
try
: B(new int[n]) // horrible!
{
...
}
catch(Error &e)
{

}
[/Code]



thanks in advance,
George







.



Relevant Pages

  • Re: when can pass by value be dangerous?
    ... the object's constructor function is not called. ... > used as an argument allocates memory and frees that memory when it is ... Suppose the object has a pointer inside it to another, ... pointers to the SAME sub-object instead of to separate copies of that ...
    (comp.lang.cpp)
  • Re: Why it is not good code for constructor
    ... If B::~B is freeing the pointer, then that would be correct usage of RAII ... C is responsible for freeing the memory. ... (I do not agree that there is resource leak, ... now if new intcompletes ok, but the constructor for A throws, the ...
    (microsoft.public.vc.language)
  • Re: double free
    ... > Does anyone know of a good website that actually describes and ... > demonstrates WHY freeing a pointer more than once is a problem. ... of memory. ...
    (comp.lang.c)
  • Re: Setting malloc pointer to NULL?
    ... After you free a pointer that was created with malloc (which set aside memory in the heap for a struct) should we set that pointer to NULL? ... If you need to free the memory ASAP and then you need to know whether it's been freed, then the NULL is a very good indicator. ... If you're concerned with calling 'free' more than once, then, in most cases, it's better to eliminate even the possibility of double freeing by writing *correct code*. ...
    (microsoft.public.vc.language)
  • Re: *** glibc detected *** ./a.out: double free or corruption
    ... Sometimes i have problems when freeing the memory of certain ... NULL invokes undefined behavior. ... returned from malloc and again when it is received by removeFPage. ...
    (comp.lang.c)