I'm trying to create a dynamic array of pointers to structure objects. I have done this before, but never really understood it, so I'm lost now that it's failing.
My code is:
struct object {
char* alias;
char* mapInfo;
char* binaryData;
};
class ATP
{
public:
ATP();
std::vector<std::shared_ptr<object>> objects;
};
This compiles fine, but when I try to run it it says
"ATPEditor.exe has stopped working. A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available."
I'm not trying to use anything like a push_back yet, I'm just trying to create the array.
Does anyone know why this is failing? Or if there is somewhere else I might have made a mistake?
EDIT: When copying the rest of my code into this window, I noticed that I had mistyped the constructor. Once I fixed it the problem went away. Thanks.
Related
How do I use pointer in visual studio every time I declare a pointer using
int *pointer;
I get a build error leave alone other operations with them. Is there something which must be done to use pointers with VC++
Error code C4101
However when I use the pointer in the code I get an error LNK1168
Edit: issue solved restarting the IDE along with assigning a value to the pointer solves it. Thanks.
C4101 is not an error, but a warning and you get it because you just declared your pointer without referencing something.
So try to initialize the pointer like this:
// Initialize with the null pointer:
int* i = nullptr;
// Or initialize with a valid address:
int ival = 5;
int* ipoint = &ival ;
Anyways, you should think of using references or smart pointers instead of raw pointers.
I've been fighting with this all day long and I've tried passing a QString, a std::string, and a char* in many many different fashions, but if I pass it so that I can modify the parameter's value inside the library function then it SEGFAULTs. If I copy the library function, line for line, into the main app, I can pass references all day long as params and change their values inside the functions.
Here is the stripped down version of my function inside the library.
I have literally removed all code except for this line.
MySQLLib::ExecuteQuery(const QString& query, QString& results)
{
results = "Changed the value of this parameter.";
}
Here is the calling code from the main application.
bmdbTest is an instance of the above MySQLLib class...
All the other code in my library and application works. It just won't let me pass references to ANYTHING to my library.
MySQLProj::pbExecuteQuery_Click()
{
QString x = "Hello.";
bmdbTest->ExecuteQuery("SELECT ttid from test_table", x);
ui_MySQLProj1.textEdit->setText(x);
}
It SEGFAULTs on the bmdbTest->ExecuteQuery call.
I've even tried a simple int& as a parameter with no success.
I can however pass params as const QString& without issue. I just can't modify the param's value that way.
EDIT: I just figured it out. Thank you to "paxdiablo" for suggesting I check my variables for null or invalid pointers. I was really tired last night and I can't believe I missed this.
I just found the problem and I feel like a complete idiot. You mentioned about bmdbTest being null or invalid. The value of bmdbTest was fine as all my other functions worked fine, but when I was calling ExecuteQuery() I was passing the query string from the value in a QLineEdit from my GUI window like this.
bmdbTest->ExecuteQuery(leQuery->text(), resultString);
The leQuery->Text() was actually the problem as I must access leQuery like this.
bmdbTest->ExecuteQuery(ui_MySQLProj1.leQuery->text(), resultString);
You may want to check the value of bmdbTest itself. It may be null or an invalid pointer.
That seems to be indicated by the fact it's faulting on that line. If there were something suspect about the parameters, I would expect it to fault within the ExecuteQuery function.
You should be able to find out exactly where the crash is by putting suitable debug statements (with flushing) on either side of the results = ... and bmdbTest->ExecuteQuery(...) lines (or use a debugger if you have one).
I am building a JSON-object with Qt and convert it to a QString using QJson. This (normally) works fine and it does in this case but in the destructor of my Qt data structure, it crashes with an Access Violation. The Object is built fine, it is sent over my network connection and after the function ends, my application crashes.
My code looks like this:
void bar()
{
QVariantMap data;
data.insert("Id", 1);
QList<QVariant> list; //QVariantList
for (QMap<...>:ConstIterator ... ) //Loop through a Map
{
QMap<QString, QVariant> singleEntry; //QVariantMap
singleEntry.insert("LocalId", it.value());
QList<QVariant> entryList; //QVariantList
for (...) //Loop through another structure
{
entryList.append("foo");
}
singleEntry.insert("List", entryList);
list.append(singleEntry);
}
data.insert("Entries", list);
QJson::Serializer.serialize(data); // Works fine
} // Crash here
If I remove the inner loop, which builds up entryList, everything works fine. It seems that the destructor of data cannot delete the contents but I have no idea, why. The whole data structure seems to be fine while serializing it (and I hope that QJson does not change anything in the given data) but it cannot be cleaned up..
Best Regards,
Tobias
As Raiv said this can happen when mixing debug and release dlls, but in my oppinion this can also happen if the application and the Qt DLL's use different CRT libraries. Some people say that when they recompiled Qt on their machines the problem dissapears and I think this is because the CRT dlls after Qt rebuild are the same as the app's. Try to set the Runtime Library option in C/C++ Code Generation is set to Multi-threaded Debug DLL (/MDd) or Multi-threaded DLL (/MD) respectively for Debug and Release. Some Qt types as QVariantMap, QVariantList, QModelIndexList are probably allocated with /MD (in Qt's dll) and when they are deallocated with /MT (in the app) I think this causes the crash. This can also fix the crash on QString::toStdWString(). In order for this to link maybe the Ignore All Default Libraries should be set to No and Ignore Specific Library should not mention crt dlls used by Qt.
I got a little workaround, which fits my needs. I have still no idea, why this crash happens, but I know, which should be the problem.
I tried to build up a static structure like this:
QVariantMap
QVariantList
QVariantMap
QVariantList
and it crashes. If I remove the QVariantList at the bottom and add QVariantMap or anything else instead, it is working fine. I think this is a problem with the nesting level in this case.
I have now just joined my list as a comma-seperated QString and then it works fine.
If anyone of you has an idea, why the crash in destructing such a nested struct (another information: doesnt matter if a allocate the QVariants in heap and delete them myself or stack) and how to fix it, please let me know.
I am learning to program using Qt framework. When I writes some code have signals and slots involved, the events didn't seem to fire and the signals and slots didn't seem to work. It really me make me annoyed. Can you give me some cautions and warnnings about signals and slots in Qt?
slot declarations:
private slots:
void onFtpCmdFinish(int cmdId, bool error);
void onRealtimeFtpCmdsDone(bool error);
connection code:
ftpHandle = new QFtp( this );
connect(ftpHandle, SIGNAL(commandFinished(int, bool)), this, SLOT(onFtpCmdFinish(int, bool)));
connect(ftpHandle, SIGNAL(done(bool)), this, SLOT(onRealtimeFtpCmdsDone(bool)));
Thank you in advance!
In the future, if you ever happen to run into problems with your Qt signals and slots again, the contents of the following blog entry can turn out to be a real life-saver (or at least a very good starting point for your investigations):
http://samdutton.wordpress.com/2008/10/03/debugging-signals-and-slots-in-qt/
It meticulously lists 20 ways to debug/troubleshoot your signals and slots; follow this list and chances are high that you will eventually find out what's wrong.
I hope that helps.
You can only detect failed connect() at runtime.
A couple of tips:
defining QT-FATAL-WARNINGS=1 will cause Qt to assert and quit whenever it gets a connect that doesn't match.
Or wrapping each connect in:
bool ok = connect(……); QASSERT( ok);
Always check the return type, if its true then CONNECT successful else some thing wrong..
Don't forget about the fifth argument Qt::ConnectionType if you will write multithreaded applications
this generates a segmentation fault becuase of "QColor colorMap[9]";. If I remove colorMap the segmentation fault goes away. If I put it back. It comes back. If I do a clean all then build all, it goes away. If I increase its arraysize it comes back. On the other hand if I reduce it it doesnt come back. I tired adding this array to another project and
What could be happening. I am really curious to know. I have removed everything else in that class. This widget subclassed is used to promote a widget in a QMainWindow.
class LevelIndicator : public QWidget
{
public:
LevelIndicator(QWidget * parent);
void paintEvent(QPaintEvent * event );
float percent;
QColor colorMap[9];
int NUM_GRADS;
};
the error happens inside ui_mainwindow.h at one of these lines:
hpaFwdPwrLvl->setObjectName(QString::fromUtf8("hpaFwdPwrLvl"));
verticalLayout->addWidget(hpaFwdPwrLvl);
I know i am not providing much but I will give alink to the app. Im trying to see if anyone has a quick answer for this.
If I do a clean all then build all, it goes away.
This makes it sound as though your build system isn't recognizing a dependency and that a change to that class definition isn't triggering a rebuild of something that should be recompiled when the definition changes.
Make sure class LevelIndicator is defined in exactly one place (generally that would be a header file that gets included by whatever modules need to use a LevelIndicator object). Also make sure that any global/static instances of LevelIndicator objects are following the one definition rule.
Firstly it might not be QColor, that may simply be changing the memory layout enough that a buffer overrun somewhere else triggers a segfault - try a different size QColor ..[1] for example.
Can QColor be used as an array like this, does it have the correct default ctor?