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 5 years ago.
Improve this question
I'm working on a small release manager that will be used to delete objects once they are old.
I'm using a std::queue to hold the age & pointer to the object.
This is the method that I'm using to push values into the queue:
ID3D12Resource* texture; // declaration
renderPlat->PushToReleaseManager(texture);
std::queue<std::pair<int,void*>> mResourceBin; // declaration
void RenderPlatform::PushToReleaseManager(ID3D12Resource* res)
{
if (!res)
return;
mResourceBin.push(std::pair<int, void*>(0, res));
}
But this is causing an Exception thrown: read access violation / std::_Deque_alloc<std::_Deque_base_types<std::pair<int,void * __ptr64>,std::allocator<std::pair<int,void * __ptr64> > > >::_Myoff(...) returned 0x6B0 :
void push_back(value_type&& _Val)
{ // insert element at end
this->_Orphan_all();
_PUSH_BACK_BEGIN; // <--- The exception is thrown here!!!
this->_Getal().construct(
_Unfancy(this->_Map()[_Block] + _Newoff % _DEQUESIZ),
_STD forward<value_type>(_Val));
_PUSH_BACK_END;
}
The object that I'm trying to delete, is an ID3D12Resource it inherits from IUnknown
Edit:
I'm using: Visual Studio 2015 (v140).
Edit 2:
The ID3D12Resource* object passed to the PushToReleaseManager() is created using ID3D12Device::CreateCommittedResource
I found the problem.
I was getting the RenderPlatform which has the PushToReleaseManager() method like this:
auto rPlat = (dx11on12::RenderPlatform*)(renderPlatform);
This cast was failing because renderPlatform was invalid and it was returning a null pointer. The thing is that I was allowing me to call the method no problem, I guess because it had some junk memory around.
Thanks for the answers!
Try to use smart pointers. They are much better then explicitly try to release memory.
Related
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 currently getting this error:
'User::User(const User&)': attempting to reference a deleted function
The program passes a string value of an object into the class.
User Constructor:
User::User(string s){
int size = s.length() + 1;
char* cstr = new char[size];
strcpy_s(cstr, size, s.c_str());
user.Parse(cstr);
}
Main Loop:
int main(){
//json equals something similar to this: "{\"body:{[{"user":1},{"user":1},{"user":1}]}\"}";
const char * json;
Document d;
d.Parse(json);
if (d.HasMember("body")) {
if (d["body"].IsArray()) {
for (SizeType i = 0; i < d["body"].Size(); i++) {
string json = getJsonString(d["body"][i]);
User u = User(json); \\this is where the error point to
this->users.push_back(u);
}
}
}
}
getJsonString function:
string getJsonString(Value& value) {
StringBuffer buffer;
buffer.Clear();
Writer<StringBuffer> writer(buffer);
value.Accept(writer);
return string(buffer.GetString());
}
I search for a lot of explanation on this error but nothing seems to make sense to me. I think it has something to do with the vector array however it doesn't make sense to me as I'm not using a pointer or reference for any of the user value. It seems to point to the constructor however no pointer or reference is being passed through. I through by returning the string of the json, I wasn't using a pointer but maybe I actually am? I would be grateful for any explanation to what I am doing wrong.
User is not copyable; this means that:
User::User(const User&) (copy constructor) is private
or deleted (= delete;)
or deleted implicitly (e.g. class has non-copyable members, or inherits from a non-copyable class). Thank you Yksisarvinen for the comment
This means you are not allowed to create a new User from another one.
Then, if you want to store User objects in a vector (as you are "apparently" trying to do in your post, I say "apparently" because the posted code does not compile, I dunno who is this) you cannot store them by value, as the vector contained needs an accessible copy constructor.
See std::vector:
T must meet the requirements of CopyAssignable and CopyConstructible.
However, you can store them as pointers:
std::vector<std::shared_ptr<User>> users;
users.push_back( std::shared_ptr<User>( new User( json ) );
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
Whenever I try to initialise/assign or insert a lot of elements at once it won't insert the items and then will skip the rest of the function.
My map is of types int and a struct that holds 6 const char*'s, there are ~70 elements that I want to initialise my map with. Ive tried using less elements (~50) but it still bugs out.
Ive tried debugging it line by line and it will run for the first ~20 elements then after that it will start skipping them, e.g. insert(elem_20) then will skip to insert(elem_22) and {pair20, pair21, pair22} would skip pair21 and go straight to pair22, it seems random as far as I can tell.
I have tried the following :
// global scope, won't initialise the map
unordered_map<int, struct> my_map = {std::make_pair(...), ...};
// in a function
void init_map()
{
// my_map is defined in the global scope
// won't be assigned
my_map = { std::make_pair(...), ... };
// this will be skipped
other_func();
...
}
// in a function
void init_map_insert()
{
// will insert the first 20 or so then gets buggy after that
// my_map is defined in the global scope
my_map.insert(std::make_pair(...));
my_map.insert(std::make_pair(...));
my_map.insert(std::make_pair(...));
...
// this will be skipped
other_func();
...
}
Video of the problem : https://www.youtube.com/watch?v=pIg6bn6fB6E
You can see the first breakpoint getting triggered but then the breakpoints at the end of the of the function aren't triggered, as well as the breakpoint right after the function call.
I am using Xcode on MacOS 10.14.
Any help would be appreciated
Cheers
Edit : sorry, I'm not that good at explaining
Edit 2 : Added video
I'm going to leave this as an answer even though it shouldn't be because you didn't provide any reproducible code or enough information so I can only guess here. When using a map of any kind, including of course unordered_map, it will only allow one entry per key. You did not specify how your key is determined but my guess is that you have cases where a new value is entered but overwrites an existing key.
Your unordered_map is initialized here:
unordered_map<int, struct> my_map = {std::make_pair(...), ...};
then you re-initialized the map in init_map(), which wipes out the what was previously initialized in the list initialization.
// in a function
void init_map()
{
// my_map is defined in the global scope
// won't be assigned
my_map = { std::make_pair(...), ... };
Note that my_map = { blah } is equivalent to
my_map = unordered_map<int, struct>{blah}
which invokes assignment operator of my_map.
This is probably the bug you're talking about.
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 am trying to get a window size from a pointer on sf::RenderWindow, but when I call a method getSize() it gives me a segmentation fault:
sf::RenderWindow* winHandle;
void createHandle(sf::RenderWindow *rw, {...}){
winHandle = rw;
}
sf::Vector2i getWindowSize() const {
return static_cast<sf::Vector2i>(winHandle->getSize());
}
createHandle is acting like a constructor here, just sets the value of winHandle as a pointer to the RenderWindow.
Update:
after some research and debugging I determined that my problem was because of winHandle beeing null, but I still can't understand why does it work like that.
Well, I have two base classes UIHandle and UIElement, UIElement inherits UIHandle and any other UI element uses UIElement and releases It's functions.
like:
class UIHandle {
sf::RenderWindow* winHandle;
void createHandle({...});
{...}
};
class UIElement : public UIHandle {
void setHandle(UIHandle handle);
{...}
}
class anyOtherElement : public UIElement {
{...}
}
(The releasation might be questionable)
every element works the same way(which means it has the handle pointer), but for some reason not for UITitleBar
in main() firstly I create a Handle and then link this handle to every element:
sl::UIHandle testHandle;
testHandle.createHandle(&window, sf::Vector2i(0, 0), sf::Vector2f(800, 600));
testHandle.e = &e;
sl::TestButton buttonA("Test", 20, 20, 100, 20);
buttonA.setHandle(&testHandle);
sl::UIButton buttonB("Test", 60, 60, 100, 20);
buttonB.setHandle(&testHandle);
sl::UITitleBar TitleBar("Test titlebar");
TitleBar.setHandle(&testHandle);
Oh, well, even though the pointer is not null it still doesnt work as intented and causes a segfault with other UIElements.
My suggestion is to check whether the pointer is NULL or not before trying to access the content of the pointer. winHandle might be NULL or is not a valid pointer. It is very difficult to know the exact reason with the code you posted.
Segmentation fault happen in many cases as given below.
When pointer is NULL
When you try to alter the contents of readonly memory
When you try to use dangling pointer
You can read more on segmentation fault using this question on stack overflow
What is a segmentation fault?
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 4 years ago.
Improve this question
I am building a project that is composed of Vehicle, Showroom, and Dealership. I've built the classes, and I am testing out my method GetAveragePrice()
float Dealership::GetAveragePrice()
This method was working perfectly:
Dealership dealership("COP3503 Vehicle Emporium", 3);
dealership.AddShowroom(&showroom);
dealership.AddShowroom(&secondary);
dealership.AddShowroom(&third);
cout << "Using just the GetAveragePrice() function\n\n";
cout << "Average price of the cars in the dealership: $" << std::fixed << std::setprecision(2);
cout << dealership.GetAveragePrice();
The output would be
Using just the GetAveragePrice() function
Average price of the cars in the dealership: $27793.60
This is the expected output I wanted, but I was told I have memory leaks and must include a destructor to deallocate my *Showroom showroomList pointer (which I initialized as the following in the Dealership constructor):
this->showroomList = new Showroom[maxNumOfShowrooms];
So I write my destructor as the following:
Dealership::~Dealership()
{
delete [] showroomList;
}
Now, there aren't any memory leaks, but I don't get the expected output and an exit code 11:
Using just the GetAveragePrice() function
Process finished with exit code 11
Does anyone know why this destructor is messing up my output?
This version would delete only once by the last instance standing, in its destructor.
std::unique_ptr<ShowRoom> Dealership::showroomList;
Dealership::Dealership(size_t maxNumOfShowrooms)
:showroomList(std::unique_ptr<ShowRoom>(new Showroom[maxNumOfShowrooms]))
{
}
Dealership::~Dealership()
{
// auto deleted here, with reverse order of initialization
}
but you have a new and delete pair so you should check for deletion only once. This would need some global counter outside of class (or its static variable) and this may not be as readable as smart pointer.
If you are using multiple threads with this, then you could be better with shared_ptr and a custom deleter ([](T * ptr){delete [] ptr;}) as its second constructor parameter.
At least this way you can know if error is about new and delete.
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 7 years ago.
Improve this question
How might I be able to perform the above operation in Boost? I am using the PCL Library where I have a function as such:
void pcl_helper_functions::performRangeThresholding(
pcl::PointCloud<pcl::PointXYZRGB>::Ptr inputCloud,
std::string axis, double startRange, double endRange
){
pcl::PointCloud<pcl::PointXYZRGB>::Ptr rangedCloud(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PassThrough<pcl::PointXYZRGB> passthroughFilter;
passthroughFilter.setInputCloud(inputCloud);
passthroughFilter.setFilterFieldName(axis);
passthroughFilter.setFilterLimits(startRange, endRange);
passthroughFilter.filter(*rangedCloud);
inputCloud = rangedCloud;
// return rangedCloud;
}
I want to set/copy inputCloud (the one that I passed in) to rangedCloud, then delete rangedCloud, so that the cloud that i pass into the function basically gets "updated"
Just pass by reference:
void pcl_helper_functions::performRangeThresholding(
pcl::PointCloud<pcl::PointXYZRGB>::Ptr& inputCloud,
std::string axis, double startRange, double endRange
){
pcl::PointCloud<pcl::PointXYZRGB>::Ptr rangedCloud(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PassThrough<pcl::PointXYZRGB> passthroughFilter;
passthroughFilter.setInputCloud(inputCloud);
passthroughFilter.setFilterFieldName(axis);
passthroughFilter.setFilterLimits(startRange, endRange);
passthroughFilter.filter(*rangedCloud);
inputCloud = rangedCloud;
}
inputCloud will be deleted as soon as the last reference to it is release (it's a shared pointer).