Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 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
I'm building a Qt app and it's crashing because of a segmentation fault. After investigation, I found out that the cause of the segfault is that "this" is NULL and I try to access a member variable in the readInputFile(QString path) method.
In this line input += line;
I don't understand why this is happening. how can "this" be NULL ?
Here's where the object is created
void MainWindow::on_inpFileCheck_clicked()
{
if (ui->inpFileCheck->isChecked()) {
QString filePath = QFileDialog::getOpenFileName(this,tr("Open CSV file"), "/home", tr("CSV (*.csv)"));
myAlgo->readInputFile(filePath);
ui->inputEdit->clear();
ui->inputEdit->appendPlainText(myAlgo->getInput());
}
}
Here's the BaseAlgorithm header
#include "qstring.h"
#include "qmainwindow.h"
class BaseAlgorithm
{
public:
BaseAlgorithm();
QString readInputFile(QString);
int lenArr;
private:
QString input;
QString output;
};
And here's the implementation and where the problem happens
#include "basealgorithm.h"
#include "qfile.h"
#include "qtextstream.h"
BaseAlgorithm::BaseAlgorithm() {
numComparisons = 0;
input = "";
output = "";
intArr = NULL;
}
QString BaseAlgorithm::readInputFile(QString path) {
QFile inpFile(path);
if (inpFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&inpFile);
while (!in.atEnd()) {
QString line = in.readLine();
input += line; // crash happens here
}
return input;
}
else {
return "ERROR";
}
}
In C++, it is perfectly possible to call a method via a null pointer to an object. As long as this is not actually dereferenced, the function would work fine.
UPDATE: The behavior is what is often exhibited by implementations, as this is usually treated as just another parameter in the compiled code. However, as #manni66 points out, the standard doesn't actually mandate the result of calling a method on a nullptr.
It is pretty obvious that this is NULL because myAlgo is NULL where it is called. So the question asked in the title here is answered.
Why is myAlgo NULL? We don't know because you didn't show that code.
I fixed it. The object was being created late.
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 2 years ago.
Improve this question
int __declspec(naked) Testing(int _byte, int _byte_size) {
int byte_size = _byte_size;
BYTE* bytec = new BYTE[byte_size];
memcpy(reinterpret_cast<void*>(bytec),
reinterpret_cast<void*>(_byte), byte_size);
int RE_format = 0;
}
This is whats cause the error, How would I go about fixing this? I have other code that I don't want to disclose but these are the main variables that are causing the error C2489.
Well, the error message is pretty self-explanatory, and the following at least compiles, although I'm not sure what use it is:
int __declspec(naked) Testing(int _byte, int _byte_size) {
int byte_size;
byte_size = _byte_size;
BYTE* bytec;
bytec = new BYTE[byte_size];
memcpy(reinterpret_cast<void*>(bytec),
reinterpret_cast<void*>(_byte), byte_size);
int RE_format;
RE_format = 0;
}
Live demo
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
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 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.
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 XML-file holding among other things some groups with name and userlists. In my code in constructor I have set a dictionary for this list:
dictGroups= QMap<QString, QList<QString>>() ;
In headerfile it is declared as
public:
QMap<QString, QList<QString>> dictGroups;
Then I read the file: ReadConfig();
void AppConfig::ReadConfig(void)
{
...
while(!reader.atEnd())
{
ReadGroups(reader);
if (dictGroups.isEmpty()) qDebug()<<"ReadConfig_isEmpty";
}
...
This is my ReadGroups:
void AppConfig::ReadGroups(QXmlStreamReader &reader)
{
dictGroups.clear();
while(!reader.atEnd())
{
reader.readNext();
if (reader.error())
{
...
}
else
{
if (reader.isStartElement())
{
if (reader.name().toString().toLower()=="group"){
ReadGroup(reader);
if (dictGroups.isEmpty()) qDebug()<<"ReadGroups_isEmpty";
}
}
else if (reader.isEndElement())
{
if (reader.name().toString().toLower() == "groups")
{
if(dictGroups.count()<=0){
QList<QString> users= QList<QString>();
users.append(this->GetUsername());
dictGroups.insert("admin", users);
}
return;
}
}
}
}
}
My problem is, that the items inserted in dictGroups while ReadGroups get lost. I get the debug output
ReadConfig_isEmpty
but in ReadGroups seems everything is ok.
I'm at a loss, puzzling around for hours, can anybody help to find the reason?
You have this code:
dictGroups.clear();
Why do you expect the dictGroups to persist when you clear them on every iteration of the outer loop? Don't do that.
The clear statement belongs perhaps at the beginning of ReadConfig.
Your method name capitalizations are very much out of place in Qt code, though: capitalized names are by convention reserved for groups.