Dynamic Allocation in Constructors

D

danil52

I have dynamic memory allocation in my constructor and deallocation in
my destructor.
However if there is exception being thrown in constructor, the
destructor does not get called and memory leaks ensue.

What are some solutions to this problem (besides using auto_ptr).
 
P

Pranav

I have dynamic memory allocation in my constructor and deallocation in
my destructor.
However if there is exception being thrown in constructor, the
destructor does not get called and memory leaks ensue.

What are some solutions to this problem (besides using auto_ptr).

Free the memory by adding of code for freeing the allocated memory in
your exception handling code..,
 
I

Ian Collins

I have dynamic memory allocation in my constructor and deallocation in
my destructor.
However if there is exception being thrown in constructor, the
destructor does not get called and memory leaks ensue.

What are some solutions to this problem (besides using auto_ptr).

What's wrong with using auto_ptr?
 
D

danil52

Free the memory by adding of code for freeing the allocated memory in
your exception handling code..,

There is no way of knowing where exactly exception was thrown. So I
cannot know which memory was allocated and which didn't get to.
 
I

Ian Collins

Nothing is wrong with auto_ptr. However I want to see how other people
deal with this issue.

By using auto_ptr or some equivalent means of releasing the resource.
 
J

Juha Nieminen

I have dynamic memory allocation in my constructor and deallocation in
my destructor.
However if there is exception being thrown in constructor, the
destructor does not get called and memory leaks ensue.

What are some solutions to this problem (besides using auto_ptr).

I think it's pretty simple:

void YourClass::deleteAll()
{
delete ptr1; ptr1 = 0;
delete ptr2; ptr2 = 0;
delete ptr3; ptr3 = 0;
}

YourClass::YourClass():
ptr1(0), ptr2(0), ptr3(0)
{
try
{
ptr1 = new whatever;
ptr2 = new whatever;
ptr3 = new whatever;
}
catch(...)
{
deleteAll();
}
}

YourClass::~YourClass()
{
deleteAll();
}
 
E

Erik Wikström

There is no way of knowing where exactly exception was thrown. So I
cannot know which memory was allocated and which didn't get to.

Initialise all the pointers to 0 in the initialisation list and free all
the pointers in the exception-handling code.
 
S

Salt_Peter

I have dynamic memory allocation in my constructor and deallocation in
my destructor.
However if there is exception being thrown in constructor, the
destructor does not get called and memory leaks ensue.

What are some solutions to this problem (besides using auto_ptr).

To answer your question we need to know what you are allocating and
why.

The best scenario is not to use heap allocations directly in the first
place. However, that depends of a bunch of factors (ie: is the type
stored copyable and assigneable).

You can use templates to inject a constant std::size_t and common
containers to store a dynamic dataset. A silly templated class can
then store anything and, depending on the container type chosen, that
dataset can be stored in a variety of different arrangements (ie:
stored in contiguous space, sequenced on insertion, etc).

Its much, much simpler than allocating yourself and a lot less work, a
breeze to maintain, yet extremely resilient and easy to use.

#include <iostream>
#include <deque>

template< typename T, const std::size_t Size >
class Storage
{
std::deque<T> m_v;
public:
Storage() : m_v(Size) { }
Storage(const T& t_) : m_v(Size, t_) { }
// accessors
T& at(const std::size_t index)
{
return m_v.at(index);
}
void push_back(const T& t)
{
m_v.push_back(t);
}
};

int main()
{
try
{
Storage< int, 10 > integers;
std::cout << integers.at(9) << std::endl;

Storage< double, 10 > doubles(1.1);
std::cout << doubles.at(9) << std::endl;

Storage< char, 10 > chars('b');
std::cout << chars.at(9) << std::endl;
chars.push_back('c');
std::cout << chars.at(10) << std::endl;
// std::cout << another.at(11) << std::endl; // oops
}
catch(const std::exception& e)
{
std::cout << "Error: ";
std::cout << e.what() << std::endl;
}
}

/*
0
1.1
b
c
// Error: deque::_M_range_check // oops
*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top