pointer being freed was not allocated, without any new usage [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 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.

Related

I wanted to use Smart Pointers, but not sure where to use and when to use [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
Improve this question
The stack class will maintain a dynamically allocated internal buffer of type unsigned long* which will be created in the constructor and is used to store the elements. This internal buffer must be wrapped in a smart pointer. The buffer is always increased or decreased in blocks and not with each element that comes in or goes out. The increment/decrement block size is passed as a parameter in the constructor.
If you want to learn how to use smart pointers then forget this code for a moment. Start a new small program that just makes sure you understand how they work.
Define a test class with just a string and an int in it. Make them public
Add a constructor so you can see when its created.
Add a Destructor so you can see when its deleted.
Create an instance using std::make_unique
Set things in the instance, write a function that accepts the unique_ptr as an argument and cout the fields in the test object
repeat with std::shared_ptr
Make sure you understand what is going on, dont just try to blindly nail stuff into your stack implementation. If you get stuck post a new question just on this.
The code above has so many other errors in it you will not get a clean set of answers to your specific 'smart pointers' question. Plus you wont be able to make it work reliably

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));

Free up memory immediately by swap trick (C++) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a very large vector as follows.
vector<vector<int>> a(100000, vector<int>(100000);
// do some work using a here
......
After the work is done, I use
vector<vector<int>>().swap(a);
in order to free the memory used by "a" immediately. I add
sleep(100)
after the swap and use "top" command to see whether memory usage is reduced immediately. But I observe that nothing has changed. What happens here?
To immediately clear vector out from the memory you should do :
v.clear();
v.shrink_to_fit();
It should work you might have error elsewhere in the code or you don't judge correctly whether the memory is freed.
You could make the deallocation more general - so it'll work on anything without performing any unnecessary operations in the swap. Swap consists of three moves. You can achieve the same result with a move and default reinitialization.
{auto x=std::move(a);a={};}
x steals a's memory and gets destroyed after leaving the scope. The a={}; isn't be necessary in most cases but according to standart after moving from object is allowed to be in a rather weird state.

Why would you use pointers for Low Memory Allocation? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
So I was looking at the source code of unreal engine and I came across this:
class USphereComponent* CollectionSphere;
There are using pointer for something that is going to be initialized and spawned once when the game begins and some objects will be overlapping it,I thought pointers are mainly used for more mid-high memory allocation purposes
So why are they using it now?
Keep in mind Im not really good at pointers so Im just trying learn and gather some information about it.
Whether to use pointers or not has almost nothing to do with "low" or "mid-high memory allocation purposes".
You use a pointer when you need a pointer, and often that's when the pointee (the thing being pointed to) will live for longer than an "automatic" object would in any of the program's existing, conveniently-accessible scopes.
Basically, *CollectionSphere will be a global (or something similar), but we can't construct it right away. The author has decided that they need fine control over the lifetime of the object. So, they have created a pointer to point to that object when the time comes.

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!