Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualclb

Posted 04 October 2012 - 08:22 AM

One gotcha though related to initializer lists is that exception safety is not possible if allocating memory to raw pointers.

Consider
Bar::Bar()
{
   throw SomeException();
}

class Foo
{
   int *somePointer;
   Bar bar;
};

Foo::Foo()
:somePointer(new int[10])
{
}

Foo::~Foo()
{
   delete[] somePointer;
}

The above code has a memory leak, somePointer is never freed since construction of Bar throws an exception.

#2clb

Posted 04 October 2012 - 08:22 AM

One gotcha though related to initializer lists is that exception safety is not possible if allocating memory to raw pointers.

Consider
Bar::Bar()
{
   throw SomeException();
}

class Foo
{
   int *data;
   Bar bar;
};

Foo::Foo()
:somePointer(new int[10])
{
}

Foo::~Foo()
{
   delete[] somePointer;
}

The above code has a memory leak, somePointer is never freed since construction of Bar throws an exception.

#1clb

Posted 04 October 2012 - 08:22 AM

One gotcha though related to initializer lists is that exception safety is not possible if allocating memory to raw pointers.

Consider
Bar::Bar()
{
   throw SomeException();
}

class Foo
{
public:
   int *data;
   Bar bar;
};

Foo::Foo()
:somePointer(new int[10])
{
}

Foo::~Foo()
{
   delete[] somePointer;
}

The above code has a memory leak, somePointer is never freed since construction of Bar throws an exception.

PARTNERS