Terminate called without an active exception (throw in catch all expression)
up vote
0
down vote
favorite
I have a code block like this:
namespace bi = boost::intrusive;
struct Container {
struct Item {
bi::set_member_hook<> link;
int x;
};
struct Cmp {
bool opeartor()(Item const& it1, Item const &it2) const {
return it1.x < it2.x;
}
};
using Set = typename bi::set<Item,
bi::member_hook<Item, bi::set_member_hook<>, &Item::link>,
bi::compare<Cmp>,
bi::constant_time_size<false>
>;
Container(Container const& o) {
auto cloner = (const Item &x) {
return current_allocator().construct<Item>(x);
};
with_allocator(_alloctor, [&] {
new (&_data) Set;
_data.clone_from(o._data, cloner, current_deleter<Item>());
}
}
private:
Set _data;
};
The program sometime terminated with this message:
terminate called without an active exception
I found an answer from stackoverflow that because of throw was called without an active exception. But if without an active exception, then it can not reach the catch block.
I'm sorry for my bad English
EDIT: The code example was updated.
EDIT 2: Correct the code as @:RemyLebeau recommended, and the bug still happen.
c++ exception-handling
add a comment |
up vote
0
down vote
favorite
I have a code block like this:
namespace bi = boost::intrusive;
struct Container {
struct Item {
bi::set_member_hook<> link;
int x;
};
struct Cmp {
bool opeartor()(Item const& it1, Item const &it2) const {
return it1.x < it2.x;
}
};
using Set = typename bi::set<Item,
bi::member_hook<Item, bi::set_member_hook<>, &Item::link>,
bi::compare<Cmp>,
bi::constant_time_size<false>
>;
Container(Container const& o) {
auto cloner = (const Item &x) {
return current_allocator().construct<Item>(x);
};
with_allocator(_alloctor, [&] {
new (&_data) Set;
_data.clone_from(o._data, cloner, current_deleter<Item>());
}
}
private:
Set _data;
};
The program sometime terminated with this message:
terminate called without an active exception
I found an answer from stackoverflow that because of throw was called without an active exception. But if without an active exception, then it can not reach the catch block.
I'm sorry for my bad English
EDIT: The code example was updated.
EDIT 2: Correct the code as @:RemyLebeau recommended, and the bug still happen.
c++ exception-handling
2
Please provide a Minimal, Complete, and Verifiable example showing how you are throwing the initial exception. And FYI, this code won't compile sinceptris not in scope inside thecatchblock to pass tofree_memory(). In this example,allocate_memory()should be above thetryblock, there is no point in callingfree_memory()ifallocate_memory()fails:complex_struct *ptr = allocate_memory(); try { clone(ptr, other); } catch(...) { free_memory(ptr); throw; }
– Remy Lebeau
Nov 20 at 3:14
1
Why are you declaring a non-pointerSetmember and then callingplacement-newon it for the same type? That is completely wrong, and totally unnecessary. Get rid of thenew, as the_datamember is already fully constructed by the compiler before your constructor's body is entered. And as such, you don't need to call the member'd destructor manually if your constructor throws, the compiler will handle that for you, so get rid of yourtry/catch, too. And remove yournoexceptspecifier, since your constructor can clearly throw an exception
– Remy Lebeau
Nov 20 at 4:14
@RemyLebeau I'm sorry, I used a custom allocator, and did not put the code here. I want items will be allocated using_allocator. The functionwith_allocatorimpl RAII idiom, then the functioncurrent_allocator()will return exactly_allocator.
– Phạm Văn Thông
Nov 20 at 4:25
Container(Container const& o) { auto cloner = ...; _data.clone_from(o._data, cloner, current_deleter<Item>()); }That is all your copy constructor needs. Assuming simplyContainer(Container const& o) : _data(o._data) {}or even justContainer(Container const& o) = default;is not sufficient to copy abi::setto another (I don't use boost)
– Remy Lebeau
Nov 20 at 4:26
1
Whether you use a custom allocator or not, callingplacement-newon a previously constructed object is still wrong.Set _data;is a fully constructed object, sonew (&_data) Set;risks leaking or even corrupting memory. If you want to useplacement-new, you need to change_datatochar _data[sizeof(Set)]or equivalent instead, and then save and use theSet*pointer thatplacement-newreturns so you can access theSetobject correctly.
– Remy Lebeau
Nov 20 at 4:30
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a code block like this:
namespace bi = boost::intrusive;
struct Container {
struct Item {
bi::set_member_hook<> link;
int x;
};
struct Cmp {
bool opeartor()(Item const& it1, Item const &it2) const {
return it1.x < it2.x;
}
};
using Set = typename bi::set<Item,
bi::member_hook<Item, bi::set_member_hook<>, &Item::link>,
bi::compare<Cmp>,
bi::constant_time_size<false>
>;
Container(Container const& o) {
auto cloner = (const Item &x) {
return current_allocator().construct<Item>(x);
};
with_allocator(_alloctor, [&] {
new (&_data) Set;
_data.clone_from(o._data, cloner, current_deleter<Item>());
}
}
private:
Set _data;
};
The program sometime terminated with this message:
terminate called without an active exception
I found an answer from stackoverflow that because of throw was called without an active exception. But if without an active exception, then it can not reach the catch block.
I'm sorry for my bad English
EDIT: The code example was updated.
EDIT 2: Correct the code as @:RemyLebeau recommended, and the bug still happen.
c++ exception-handling
I have a code block like this:
namespace bi = boost::intrusive;
struct Container {
struct Item {
bi::set_member_hook<> link;
int x;
};
struct Cmp {
bool opeartor()(Item const& it1, Item const &it2) const {
return it1.x < it2.x;
}
};
using Set = typename bi::set<Item,
bi::member_hook<Item, bi::set_member_hook<>, &Item::link>,
bi::compare<Cmp>,
bi::constant_time_size<false>
>;
Container(Container const& o) {
auto cloner = (const Item &x) {
return current_allocator().construct<Item>(x);
};
with_allocator(_alloctor, [&] {
new (&_data) Set;
_data.clone_from(o._data, cloner, current_deleter<Item>());
}
}
private:
Set _data;
};
The program sometime terminated with this message:
terminate called without an active exception
I found an answer from stackoverflow that because of throw was called without an active exception. But if without an active exception, then it can not reach the catch block.
I'm sorry for my bad English
EDIT: The code example was updated.
EDIT 2: Correct the code as @:RemyLebeau recommended, and the bug still happen.
c++ exception-handling
c++ exception-handling
edited Nov 21 at 1:14
asked Nov 20 at 2:48
Phạm Văn Thông
14710
14710
2
Please provide a Minimal, Complete, and Verifiable example showing how you are throwing the initial exception. And FYI, this code won't compile sinceptris not in scope inside thecatchblock to pass tofree_memory(). In this example,allocate_memory()should be above thetryblock, there is no point in callingfree_memory()ifallocate_memory()fails:complex_struct *ptr = allocate_memory(); try { clone(ptr, other); } catch(...) { free_memory(ptr); throw; }
– Remy Lebeau
Nov 20 at 3:14
1
Why are you declaring a non-pointerSetmember and then callingplacement-newon it for the same type? That is completely wrong, and totally unnecessary. Get rid of thenew, as the_datamember is already fully constructed by the compiler before your constructor's body is entered. And as such, you don't need to call the member'd destructor manually if your constructor throws, the compiler will handle that for you, so get rid of yourtry/catch, too. And remove yournoexceptspecifier, since your constructor can clearly throw an exception
– Remy Lebeau
Nov 20 at 4:14
@RemyLebeau I'm sorry, I used a custom allocator, and did not put the code here. I want items will be allocated using_allocator. The functionwith_allocatorimpl RAII idiom, then the functioncurrent_allocator()will return exactly_allocator.
– Phạm Văn Thông
Nov 20 at 4:25
Container(Container const& o) { auto cloner = ...; _data.clone_from(o._data, cloner, current_deleter<Item>()); }That is all your copy constructor needs. Assuming simplyContainer(Container const& o) : _data(o._data) {}or even justContainer(Container const& o) = default;is not sufficient to copy abi::setto another (I don't use boost)
– Remy Lebeau
Nov 20 at 4:26
1
Whether you use a custom allocator or not, callingplacement-newon a previously constructed object is still wrong.Set _data;is a fully constructed object, sonew (&_data) Set;risks leaking or even corrupting memory. If you want to useplacement-new, you need to change_datatochar _data[sizeof(Set)]or equivalent instead, and then save and use theSet*pointer thatplacement-newreturns so you can access theSetobject correctly.
– Remy Lebeau
Nov 20 at 4:30
add a comment |
2
Please provide a Minimal, Complete, and Verifiable example showing how you are throwing the initial exception. And FYI, this code won't compile sinceptris not in scope inside thecatchblock to pass tofree_memory(). In this example,allocate_memory()should be above thetryblock, there is no point in callingfree_memory()ifallocate_memory()fails:complex_struct *ptr = allocate_memory(); try { clone(ptr, other); } catch(...) { free_memory(ptr); throw; }
– Remy Lebeau
Nov 20 at 3:14
1
Why are you declaring a non-pointerSetmember and then callingplacement-newon it for the same type? That is completely wrong, and totally unnecessary. Get rid of thenew, as the_datamember is already fully constructed by the compiler before your constructor's body is entered. And as such, you don't need to call the member'd destructor manually if your constructor throws, the compiler will handle that for you, so get rid of yourtry/catch, too. And remove yournoexceptspecifier, since your constructor can clearly throw an exception
– Remy Lebeau
Nov 20 at 4:14
@RemyLebeau I'm sorry, I used a custom allocator, and did not put the code here. I want items will be allocated using_allocator. The functionwith_allocatorimpl RAII idiom, then the functioncurrent_allocator()will return exactly_allocator.
– Phạm Văn Thông
Nov 20 at 4:25
Container(Container const& o) { auto cloner = ...; _data.clone_from(o._data, cloner, current_deleter<Item>()); }That is all your copy constructor needs. Assuming simplyContainer(Container const& o) : _data(o._data) {}or even justContainer(Container const& o) = default;is not sufficient to copy abi::setto another (I don't use boost)
– Remy Lebeau
Nov 20 at 4:26
1
Whether you use a custom allocator or not, callingplacement-newon a previously constructed object is still wrong.Set _data;is a fully constructed object, sonew (&_data) Set;risks leaking or even corrupting memory. If you want to useplacement-new, you need to change_datatochar _data[sizeof(Set)]or equivalent instead, and then save and use theSet*pointer thatplacement-newreturns so you can access theSetobject correctly.
– Remy Lebeau
Nov 20 at 4:30
2
2
Please provide a Minimal, Complete, and Verifiable example showing how you are throwing the initial exception. And FYI, this code won't compile since
ptr is not in scope inside the catch block to pass to free_memory(). In this example, allocate_memory() should be above the try block, there is no point in calling free_memory() if allocate_memory() fails: complex_struct *ptr = allocate_memory(); try { clone(ptr, other); } catch(...) { free_memory(ptr); throw; }– Remy Lebeau
Nov 20 at 3:14
Please provide a Minimal, Complete, and Verifiable example showing how you are throwing the initial exception. And FYI, this code won't compile since
ptr is not in scope inside the catch block to pass to free_memory(). In this example, allocate_memory() should be above the try block, there is no point in calling free_memory() if allocate_memory() fails: complex_struct *ptr = allocate_memory(); try { clone(ptr, other); } catch(...) { free_memory(ptr); throw; }– Remy Lebeau
Nov 20 at 3:14
1
1
Why are you declaring a non-pointer
Set member and then calling placement-new on it for the same type? That is completely wrong, and totally unnecessary. Get rid of the new, as the _data member is already fully constructed by the compiler before your constructor's body is entered. And as such, you don't need to call the member'd destructor manually if your constructor throws, the compiler will handle that for you, so get rid of your try/catch, too. And remove your noexcept specifier, since your constructor can clearly throw an exception– Remy Lebeau
Nov 20 at 4:14
Why are you declaring a non-pointer
Set member and then calling placement-new on it for the same type? That is completely wrong, and totally unnecessary. Get rid of the new, as the _data member is already fully constructed by the compiler before your constructor's body is entered. And as such, you don't need to call the member'd destructor manually if your constructor throws, the compiler will handle that for you, so get rid of your try/catch, too. And remove your noexcept specifier, since your constructor can clearly throw an exception– Remy Lebeau
Nov 20 at 4:14
@RemyLebeau I'm sorry, I used a custom allocator, and did not put the code here. I want items will be allocated using
_allocator. The function with_allocator impl RAII idiom, then the function current_allocator() will return exactly _allocator.– Phạm Văn Thông
Nov 20 at 4:25
@RemyLebeau I'm sorry, I used a custom allocator, and did not put the code here. I want items will be allocated using
_allocator. The function with_allocator impl RAII idiom, then the function current_allocator() will return exactly _allocator.– Phạm Văn Thông
Nov 20 at 4:25
Container(Container const& o) { auto cloner = ...; _data.clone_from(o._data, cloner, current_deleter<Item>()); } That is all your copy constructor needs. Assuming simply Container(Container const& o) : _data(o._data) {} or even just Container(Container const& o) = default; is not sufficient to copy a bi::set to another (I don't use boost)– Remy Lebeau
Nov 20 at 4:26
Container(Container const& o) { auto cloner = ...; _data.clone_from(o._data, cloner, current_deleter<Item>()); } That is all your copy constructor needs. Assuming simply Container(Container const& o) : _data(o._data) {} or even just Container(Container const& o) = default; is not sufficient to copy a bi::set to another (I don't use boost)– Remy Lebeau
Nov 20 at 4:26
1
1
Whether you use a custom allocator or not, calling
placement-new on a previously constructed object is still wrong. Set _data; is a fully constructed object, so new (&_data) Set; risks leaking or even corrupting memory. If you want to use placement-new, you need to change _data to char _data[sizeof(Set)] or equivalent instead, and then save and use the Set* pointer that placement-new returns so you can access the Set object correctly.– Remy Lebeau
Nov 20 at 4:30
Whether you use a custom allocator or not, calling
placement-new on a previously constructed object is still wrong. Set _data; is a fully constructed object, so new (&_data) Set; risks leaking or even corrupting memory. If you want to use placement-new, you need to change _data to char _data[sizeof(Set)] or equivalent instead, and then save and use the Set* pointer that placement-new returns so you can access the Set object correctly.– Remy Lebeau
Nov 20 at 4:30
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
I think the message
terminate called without an active exception
actually takes to mean
terminate called without an active C++ exception
terminate() may be called for many different reasons, not limited to unhandled C++ exceptions. The catch (...) clause only catches C++ exceptions. In your particular case, terminate() may be called within allocate_memory() or clone() directly, or by runtime due to unhandled non-C++ exceptions (like some low-level Windows exceptions that should be captured with SEH). In either case, the catch block is never entered.
I used a framework which print backtrace when program was terminated. The last function in the reported call stack is this function. theclone()function used in my function isboost::intrusive::set::clone_from(I can not copy and pasted the code here because of security reason). I don't think that the compiler inline this function, then it should show up in the call stack if there is an exception.
– Phạm Văn Thông
Nov 20 at 3:30
add a comment |
up vote
-1
down vote
You have called throw from catch but the catch was supposed to catch it. The throw should have been in try instead if I understood it correctly.
2
Acatchblock can re-throwthe exception it caught.
– Remy Lebeau
Nov 20 at 3:08
@RemyLebeau Yes, it surely can but in this case, it may cause the crash the application if it is not caught further. And that is my point. So how my statement is wrong?
– Harsh
Nov 20 at 4:02
your statement implies thatthrowcannot be called inside ofcatch, which is wrong. Regardless of whether the exception is caught higher up the call stack or not.
– Remy Lebeau
Nov 20 at 4:27
I said "The throw should have been in try instead if I understood it correctly.". Now how it implies that throw can't be in catch?
– Harsh
Nov 20 at 4:33
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I think the message
terminate called without an active exception
actually takes to mean
terminate called without an active C++ exception
terminate() may be called for many different reasons, not limited to unhandled C++ exceptions. The catch (...) clause only catches C++ exceptions. In your particular case, terminate() may be called within allocate_memory() or clone() directly, or by runtime due to unhandled non-C++ exceptions (like some low-level Windows exceptions that should be captured with SEH). In either case, the catch block is never entered.
I used a framework which print backtrace when program was terminated. The last function in the reported call stack is this function. theclone()function used in my function isboost::intrusive::set::clone_from(I can not copy and pasted the code here because of security reason). I don't think that the compiler inline this function, then it should show up in the call stack if there is an exception.
– Phạm Văn Thông
Nov 20 at 3:30
add a comment |
up vote
0
down vote
I think the message
terminate called without an active exception
actually takes to mean
terminate called without an active C++ exception
terminate() may be called for many different reasons, not limited to unhandled C++ exceptions. The catch (...) clause only catches C++ exceptions. In your particular case, terminate() may be called within allocate_memory() or clone() directly, or by runtime due to unhandled non-C++ exceptions (like some low-level Windows exceptions that should be captured with SEH). In either case, the catch block is never entered.
I used a framework which print backtrace when program was terminated. The last function in the reported call stack is this function. theclone()function used in my function isboost::intrusive::set::clone_from(I can not copy and pasted the code here because of security reason). I don't think that the compiler inline this function, then it should show up in the call stack if there is an exception.
– Phạm Văn Thông
Nov 20 at 3:30
add a comment |
up vote
0
down vote
up vote
0
down vote
I think the message
terminate called without an active exception
actually takes to mean
terminate called without an active C++ exception
terminate() may be called for many different reasons, not limited to unhandled C++ exceptions. The catch (...) clause only catches C++ exceptions. In your particular case, terminate() may be called within allocate_memory() or clone() directly, or by runtime due to unhandled non-C++ exceptions (like some low-level Windows exceptions that should be captured with SEH). In either case, the catch block is never entered.
I think the message
terminate called without an active exception
actually takes to mean
terminate called without an active C++ exception
terminate() may be called for many different reasons, not limited to unhandled C++ exceptions. The catch (...) clause only catches C++ exceptions. In your particular case, terminate() may be called within allocate_memory() or clone() directly, or by runtime due to unhandled non-C++ exceptions (like some low-level Windows exceptions that should be captured with SEH). In either case, the catch block is never entered.
answered Nov 20 at 3:15
Lingxi
8,2691953
8,2691953
I used a framework which print backtrace when program was terminated. The last function in the reported call stack is this function. theclone()function used in my function isboost::intrusive::set::clone_from(I can not copy and pasted the code here because of security reason). I don't think that the compiler inline this function, then it should show up in the call stack if there is an exception.
– Phạm Văn Thông
Nov 20 at 3:30
add a comment |
I used a framework which print backtrace when program was terminated. The last function in the reported call stack is this function. theclone()function used in my function isboost::intrusive::set::clone_from(I can not copy and pasted the code here because of security reason). I don't think that the compiler inline this function, then it should show up in the call stack if there is an exception.
– Phạm Văn Thông
Nov 20 at 3:30
I used a framework which print backtrace when program was terminated. The last function in the reported call stack is this function. the
clone() function used in my function is boost::intrusive::set::clone_from (I can not copy and pasted the code here because of security reason). I don't think that the compiler inline this function, then it should show up in the call stack if there is an exception.– Phạm Văn Thông
Nov 20 at 3:30
I used a framework which print backtrace when program was terminated. The last function in the reported call stack is this function. the
clone() function used in my function is boost::intrusive::set::clone_from (I can not copy and pasted the code here because of security reason). I don't think that the compiler inline this function, then it should show up in the call stack if there is an exception.– Phạm Văn Thông
Nov 20 at 3:30
add a comment |
up vote
-1
down vote
You have called throw from catch but the catch was supposed to catch it. The throw should have been in try instead if I understood it correctly.
2
Acatchblock can re-throwthe exception it caught.
– Remy Lebeau
Nov 20 at 3:08
@RemyLebeau Yes, it surely can but in this case, it may cause the crash the application if it is not caught further. And that is my point. So how my statement is wrong?
– Harsh
Nov 20 at 4:02
your statement implies thatthrowcannot be called inside ofcatch, which is wrong. Regardless of whether the exception is caught higher up the call stack or not.
– Remy Lebeau
Nov 20 at 4:27
I said "The throw should have been in try instead if I understood it correctly.". Now how it implies that throw can't be in catch?
– Harsh
Nov 20 at 4:33
add a comment |
up vote
-1
down vote
You have called throw from catch but the catch was supposed to catch it. The throw should have been in try instead if I understood it correctly.
2
Acatchblock can re-throwthe exception it caught.
– Remy Lebeau
Nov 20 at 3:08
@RemyLebeau Yes, it surely can but in this case, it may cause the crash the application if it is not caught further. And that is my point. So how my statement is wrong?
– Harsh
Nov 20 at 4:02
your statement implies thatthrowcannot be called inside ofcatch, which is wrong. Regardless of whether the exception is caught higher up the call stack or not.
– Remy Lebeau
Nov 20 at 4:27
I said "The throw should have been in try instead if I understood it correctly.". Now how it implies that throw can't be in catch?
– Harsh
Nov 20 at 4:33
add a comment |
up vote
-1
down vote
up vote
-1
down vote
You have called throw from catch but the catch was supposed to catch it. The throw should have been in try instead if I understood it correctly.
You have called throw from catch but the catch was supposed to catch it. The throw should have been in try instead if I understood it correctly.
answered Nov 20 at 3:02
Harsh
1438
1438
2
Acatchblock can re-throwthe exception it caught.
– Remy Lebeau
Nov 20 at 3:08
@RemyLebeau Yes, it surely can but in this case, it may cause the crash the application if it is not caught further. And that is my point. So how my statement is wrong?
– Harsh
Nov 20 at 4:02
your statement implies thatthrowcannot be called inside ofcatch, which is wrong. Regardless of whether the exception is caught higher up the call stack or not.
– Remy Lebeau
Nov 20 at 4:27
I said "The throw should have been in try instead if I understood it correctly.". Now how it implies that throw can't be in catch?
– Harsh
Nov 20 at 4:33
add a comment |
2
Acatchblock can re-throwthe exception it caught.
– Remy Lebeau
Nov 20 at 3:08
@RemyLebeau Yes, it surely can but in this case, it may cause the crash the application if it is not caught further. And that is my point. So how my statement is wrong?
– Harsh
Nov 20 at 4:02
your statement implies thatthrowcannot be called inside ofcatch, which is wrong. Regardless of whether the exception is caught higher up the call stack or not.
– Remy Lebeau
Nov 20 at 4:27
I said "The throw should have been in try instead if I understood it correctly.". Now how it implies that throw can't be in catch?
– Harsh
Nov 20 at 4:33
2
2
A
catch block can re-throw the exception it caught.– Remy Lebeau
Nov 20 at 3:08
A
catch block can re-throw the exception it caught.– Remy Lebeau
Nov 20 at 3:08
@RemyLebeau Yes, it surely can but in this case, it may cause the crash the application if it is not caught further. And that is my point. So how my statement is wrong?
– Harsh
Nov 20 at 4:02
@RemyLebeau Yes, it surely can but in this case, it may cause the crash the application if it is not caught further. And that is my point. So how my statement is wrong?
– Harsh
Nov 20 at 4:02
your statement implies that
throw cannot be called inside of catch, which is wrong. Regardless of whether the exception is caught higher up the call stack or not.– Remy Lebeau
Nov 20 at 4:27
your statement implies that
throw cannot be called inside of catch, which is wrong. Regardless of whether the exception is caught higher up the call stack or not.– Remy Lebeau
Nov 20 at 4:27
I said "The throw should have been in try instead if I understood it correctly.". Now how it implies that throw can't be in catch?
– Harsh
Nov 20 at 4:33
I said "The throw should have been in try instead if I understood it correctly.". Now how it implies that throw can't be in catch?
– Harsh
Nov 20 at 4:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385499%2fterminate-called-without-an-active-exception-throw-in-catch-all-expression%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Please provide a Minimal, Complete, and Verifiable example showing how you are throwing the initial exception. And FYI, this code won't compile since
ptris not in scope inside thecatchblock to pass tofree_memory(). In this example,allocate_memory()should be above thetryblock, there is no point in callingfree_memory()ifallocate_memory()fails:complex_struct *ptr = allocate_memory(); try { clone(ptr, other); } catch(...) { free_memory(ptr); throw; }– Remy Lebeau
Nov 20 at 3:14
1
Why are you declaring a non-pointer
Setmember and then callingplacement-newon it for the same type? That is completely wrong, and totally unnecessary. Get rid of thenew, as the_datamember is already fully constructed by the compiler before your constructor's body is entered. And as such, you don't need to call the member'd destructor manually if your constructor throws, the compiler will handle that for you, so get rid of yourtry/catch, too. And remove yournoexceptspecifier, since your constructor can clearly throw an exception– Remy Lebeau
Nov 20 at 4:14
@RemyLebeau I'm sorry, I used a custom allocator, and did not put the code here. I want items will be allocated using
_allocator. The functionwith_allocatorimpl RAII idiom, then the functioncurrent_allocator()will return exactly_allocator.– Phạm Văn Thông
Nov 20 at 4:25
Container(Container const& o) { auto cloner = ...; _data.clone_from(o._data, cloner, current_deleter<Item>()); }That is all your copy constructor needs. Assuming simplyContainer(Container const& o) : _data(o._data) {}or even justContainer(Container const& o) = default;is not sufficient to copy abi::setto another (I don't use boost)– Remy Lebeau
Nov 20 at 4:26
1
Whether you use a custom allocator or not, calling
placement-newon a previously constructed object is still wrong.Set _data;is a fully constructed object, sonew (&_data) Set;risks leaking or even corrupting memory. If you want to useplacement-new, you need to change_datatochar _data[sizeof(Set)]or equivalent instead, and then save and use theSet*pointer thatplacement-newreturns so you can access theSetobject correctly.– Remy Lebeau
Nov 20 at 4:30