473,396 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dynamic Allocation in Constructors

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).
Sep 13 '08 #1
9 3284
On Sep 13, 8:39*am, dani...@mail.ru wrote:
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..,
Sep 13 '08 #2
da*****@mail.ru wrote:
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?

--
Ian Collins.
Sep 13 '08 #3
On Sep 12, 11:44*pm, Pranav <pranav...@gmail.comwrote:
On Sep 13, 8:39*am, dani...@mail.ru wrote:
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..,
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.
Sep 13 '08 #4
On Sep 12, 11:48*pm, Ian Collins <ian-n...@hotmail.comwrote:
dani...@mail.ru wrote:
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?

--
Ian Collins.
Nothing is wrong with auto_ptr. However I want to see how other people
deal with this issue.
Sep 13 '08 #5
da*****@mail.ru wrote:
On Sep 12, 11:48 pm, Ian Collins <ian-n...@hotmail.comwrote:
>dani...@mail.ru wrote:
>>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?

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.

--
Ian Collins.
Sep 13 '08 #6
da*****@mail.ru wrote:
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();
}
Sep 13 '08 #7
On 2008-09-13 07:28, da*****@mail.ru wrote:
On Sep 12, 11:44 pm, Pranav <pranav...@gmail.comwrote:
>On Sep 13, 8:39 am, dani...@mail.ru wrote:
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..,

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.

--
Erik Wikström
Sep 13 '08 #8
On Sep 12, 11:39 pm, dani...@mail.ru wrote:
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<Tm_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
*/
Sep 13 '08 #9
da*****@mail.ru kirjutas:
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.
This shows that you are allocating more than one resource per class. If
this is needed, each resource should be packed in its own class which takes
care of proper deallocation. In case of dynamic memory std::vector is the
obvious candidate.

hth
Paavo
Sep 13 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
8
by: Marcus Kwok | last post by:
Given a user defined class with a default constructor (let's call the class C), is there any semantic difference between C* c = new C; and C* c = new C(); ? I am thinking they are...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
5
by: subramanian100in | last post by:
Consider the following classes without ctors. class Sample { private: double d_val; double* d_ptr; }; class Test
10
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.