Exception 0xC0000409 (stack buffer overflow) when using dynamic_pointer_cast [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm experiencing a crash when trying to dynamic_pointer_cast a shared point of type A to type B.
Type B is not related to type A and I'd expect an empty shared_ptr but instead, the exception above is raised.
Is there any scenario where it isn't safe to use dynamic_pointer_cast?
Exception raised here:
Using MSVC 14.16.27023

std::dynamic_pointer_cast requires that the conversion of U* (source) to T* (destination) is well formed. If it isn't then you have undefined behavior. If you want to get a null pointer like you would from dynamic_cast then you are going to have to write your own version that will do this.
Another option is test the result of
dynamic_cast<decltype(destination_ptr.get())>(source_ptr.get())
And if that succeeds then call std::dynamic_pointer_cast else return a null pointer.

Related

What is the correct syntax for std::vector<std::unique_ptr<>>::push_back()? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have read that I need to define a deleter for a unique_ptr if I want to use push_back from std::vector<std::unique_ptr<T>>. Is that true? If yes what's the correct syntax to do that?
Thank you for your answers
No, you don't need a custom deleter in the normal case. For an existing unique_ptr:
vector.push_back(std::move(ptr));
works fine (transferring ownership of the pointer to the vector); for a new pointer:
vector.push_back(std::make_unique<MyType>(args, go, here));
is the safest (since it ensures the pointer is always managed, even if push_back throws an exception), with:
vector.emplace_back(new MyType(args, go, here));
being perhaps a little faster, at the expense of potentially leaking memory if the vector raises an exception (e.g. due to a failed attempt to expand the underlying storage; though in that scenario, your program is likely doomed anyway, so it may not be worth defending against).
You only need to write deleter if default one doesn't fit your need, so for specific resource.
Syntax to use push_back with movable non-copiable type is:
std::vector<std::unique_ptr<T>> vs;
vs.push_back(std::make_unique<T>(42));
std::unique_ptr<T> p = std::make_unique<T>(42);
vs.push_back(std::move(p));

pointer being freed was not allocated, without any new usage [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm going through a piece of code here. For testing purposes it shows a window (QWidget) when executed.
When I hit close it returns:
my_object(7082,0x7fff7a538000) malloc: *** error for object 0x7fff5199b9b8: pointer being freed was not allocated
Where my_object is an instance of the class instantiated at a QMainWindow.
There is no new usage in all code I wrote. And also no delete call. How is that (pointer being freed) possible? I though not using explicit new I'll be outside the dangerous zone.
What's the best way to approach this (pointer being freed was not allocated) issue. I'll go by disabling some parts or, in other words, try/error approach.
Boy,
Check the order of member declaration in all classes involved.

boost::optional has changed an implicit function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am using VS2008 and has just changed from boost 1.51 to 1.61. Unfortunately boost decided to change the boost::optional<T> and make the cast from optional to bool explicit i.e. the snippet below is not valid anymore
boost::optional<TYPE> optType;
TYPE t = default;
if (optType)
t = optType;
You must explicitly cast the optType to bool to make this work.
Even more unfortunate is the fact that this still compiles (with the wrong result ofc), and I am unable to detect where it is used. Does anyone know of any (even intrusive) methods to detect those places.
I tried to mark the implicit cast as deprecated in boosts 1.51 hpp-file but for some reason the compiler does not show any warnings.
The amount of code I have to look through is several hundred thousand rows that's why I need this.
In your particular snippet, you could simply write
boost::optional<TYPE> optType;
TYPE t = default;
if (optType)
t = true;
However, did you mean to get the TYPE value?
if (optType)
t = *optType;
You can also simply to
t = optType.get_value_or(default);

Accessing struct member through unique_ptr gives segmentation fault [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
This is how I set pointer to struct. At runtime segmentation fault is thrown at second line. LoadedPDFInfo is struct in Canvas namespace
struct std::unique_ptr<Canvas::LoadedPDFInfo> pdfInfo;
pdfInfo->handle = ++currentPDFHandle;
pdfInfo->totalPageNum = FPDF_GetPageCount(doc);
First, std::unique_ptr is a class not a struct, so get rid of the struct prefix on the pdfInfo variable declaration. You were probably thinking of this instead:
std::unique_ptr<struct Canvas::LoadedPDFInfo> pdfInfo;
But even when declaring variables (or type-casting) using actual struct types, you still do not need the struct prefix. C needs that, C++ does not.
Second, your segfault is happening because you have merely declared the pdfInfo variable, but it is not actually pointing at a valid LoadedPDFInfo object, so using the -> operator is not a valid operation. Just like a regular pointer, std::unique_ptr (and std::auto_ptr, and std::shared_ptr) have to point at something in order to access that something's members. For example:
std::unique_ptr<Canvas::LoadedPDFInfo> pdfInfo(new Canvas::LoadedPDFInfo);

C++ badly initialized std::set [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a public variable inside a class, declared as
std::set<int> test;
and never explicitly initialized. When I try to access it from a shared pointer c of an instance of the object:
std::set<int>& myset = c->test;
I see in the debugger that myset is badly initialized: it has both fields _Myhead and _Mysize null. Could you please explain why that happens?
You are using std::set<>, and it has own constructor which initialize inner data. So null is OK.
The issue was that the pointer was not correctly initialised, although I thought it was.
I got confused since I come from Java where a null pointer would have generated an exception without letting me inspect the object's inner variables, so I ruled out that option too early!