Im using C++ and SDL to program a game, and even though I get no errors whenever I try to run it - I get a segmentation fault. I ran the gdb debugger and this is what it gave me when I used the backtrack function : "#0 main () at main.cpp:10" , where the 10th line is the statement right after the try block opens. Can someone tell me what is going on here, Ive never encountered such an error before.
Maybe you are creating the SDL_Surfaces for bgSurface and fgSurface before calling SDL_Init...
That's why I have the call to SDL_Init in a class constructor, by itself (and SDL_Quit in the constructor). That way you can just make Manager a private subclass of that one:
class Manager : private SDLInitializer
{ /* */ }
And in the constructor:
Manager::Manager() :
SDLInitializer(SDL_INIT_VIDEO),
/* */
And since base classes are initialized before member variables, all goes well!
Related
So, I've been working on my GUI and it's gotten pretty large, so I decided to split it into multiple widgets that communicate with the main window (probably something that I should have done from the beginning). I've partitioned part of my GUI into a separate widget, but I'm having trouble setting up signals and slots between the main window and the new widget, and I was hoping you could help me figure out what I'm doing wrong.
The main window is called robotmainwindow and the widget is called robotTabWidget. In robotmainwindow.h, I forward declared robotTabWidget as such:
robotTabWidget* robotttabwidget;
Then, in robotmainwindow.cpp, I initialized the class:
robottabwidget = new robotTabWidget();
I have function called create connections in robotmainwindow, in which I try to connect a signal from robotTabWidget to a slot in robotmainwindow:
void robotmainwindow::createConnections()
{
connect(robottabwidget, &robotTabWidget::sigSendCartCommand, this, &robotmainwindow::slotOnSendCartCommand);
}
The signal sigSendCartCommand is defined in robottabwidget.h:
void sigSendCartCommand(double);
And emitted in robotTabWidget::on_SendCartCommand_clicked():
emit sigSendCartCommand(CartCommand);
But when I attempt to compile, I get a "no matching function for call to" for the connect function, and "robotTabWidget::sigSendCartCommand(double) is protected". Why is the signal protected? I thought you could emit a signal from anywhere. And why am I getting a "no matching function" error?
This has been giving me a lot of trouble for the last few days and I haven't been able to figure it out. I'd greatly appreciate your help!
edit: I've changed things around, and fixed a few things but I'm still getting errors. The connect function now looks like:
QObject::connect(myrobotTabWidget, robotTabWidget::sigTest(test), this, &robotmainwindow::slotOnSendCartCommand);
The error I'm now getting is "cannot call member function without object":
../RobotInterface2/robotmainwindow.cpp:102:68: error: cannot call member function 'void robotTabWidget::sigTest(QString)' without object
QObject::connect(myrobotTabWidget, robotTabWidget::sigTest(test), this, &robotmainwindow::slotOnSendCartCommand);
And the arrow is pointing at robotTabWidget::sigTest(QString). I'm not sure what to do about this. Any ideas?
I am facing issue with Release build. Application works fine in Debug build but in release build a pointer initialized to hold object of another class is getting different address allocation and thus causing corruption to its values.
My main application class is K32App
code in K32App.h file
CSheetPrintManager* m_pSheetPrintManager;
CSheetPrintManager* GetSheetPrintManager() { return m_pSheetPrintManager; }
In file K32App.cpp
K32App::K32App()
{
m_pSheetPrintManager= NULL;
}
BOOL K32App::InitInstance()
{
if(!m_pSheetPrintManager)
m_pSheetPrintManager= new CSheetPrintManager();
}
K32App::~K32App()
{
if(m_pSheetPrintManager)
delete(m_pSheetPrintManager)
}
In my file CSheetPrintManager.cpp
void CSheetPrintManager::CSheetPrintManager()
{
//Initialized all member variables to default values.
Init();
}
void CSheetPrintManager::Init()
{
m_nSheetType = SheetIllegalNone; //long
m_sBankEntry.Empty(); //CString
m_bHistorical = FALSE; //BOOL
m_bDebitDetailsSet = FALSE; //BOOL
m_mapRequested.RemoveAll(); // Type CMap<long,long,CString,CString&>
}
During application startup, when it reaches
if(!m_pSheetPrintManager)
CSheetPrintManager= new CSheetPrintManager();
and tries to create object of m_pSheetPrintManager, 'this' pointer inside CSheetPrintManager.cpp shows valid address (0x03768ce0) at breakpoint just at curly brace {, once I step further into CSheetPrintManager.Init(), 'this' gets different location and starts point to another address(0x0000000) and then further moving it starts pointing another location(0x03786ce0) and then on reaching m_mapRequested.RemoveAll();
'this' is pointing some other location.
returning back to main app file C32App.cpp , I get following for 'm_pSheetPrintManager' CXX0030 Error 'expression cannot be evaluated" in Auto window.
and appplication continues to run. See what get when hover mouse of m_pSheetPrintManager (can't post image because need 10 reputations for it :) so linking it)
studio Auto window screenshot
In debug mode, I get m_pSheetPrintManager pointing to same location during all application processing and members always remain properly initialized.
But in Release mode, m_pSheetPrintManager continues to point (address value shown in Auto window) different location. and all member variables of class CSheetPrintManager showing different garbage(Uninitialized) values with each line of processing inside CSheetPrintManager Class.
If I disable C++ compiler optimization in Release-Mode then it works fine without any issue.
Any help/guidance/suggestion is most appreciated.
Thanks in advance.
PS: This is my first question here so please excuse in case missing something to point or express properly.
You need to provide more info: Is CSheetPrintManager capsuled in a dll? How does the declaration look? This way I only can play guessing games... :-/
Consider to derive from CObject and use DECLARE DYNCREATE and IMPLEMENT_DYNCREATE for your CSheetPrintManagerclass. You can then VERIFY()in your Release version if it's a valid object using CObject::IsKindOf().
Try #pragma pack() to define how padding is done in your class, especially if you optimized for size in the Release version and having mixed-up Debug and Release dlls.
I have a program that runs a least squares fit to some data. This procedure is run in a separate thread and controlled from a dialog box. This dialog box has a QPlainTextEdit that shows fitting updates and a final report.
The dialog was created in Qt Designer, the code is run into QtCreator and my Qt version is 4.8.1.
The problem I am running into is somewhat erratic. When I run the procedure a first time, everything is fine. Then if I run it again, sometimes the program crashes with the message
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
The program has unexpectedly finished.
I tracked the problem to a call to the clear() method of a QPlainTextEdit. Here is some code.
// Snippets of the class definition
class QLSQDialog : public QDialog, public Ui_QLSQDialog
{
Q_OBJECT
public:
QLSQDialog(QWidget *parent = 0);
(...)
void UpdateDisplay(const QString &msg, int iter, double norm); // Update values of chi, etc on displays
signals:
void Run(); // Signal to run a LSQ procedure
(...)
private slots:
void on_btnRun_clicked();
(...)
private:
void Enables(bool running); // Enable and disable features depending on running state of LSQ fit
(...)
};
// Snippets of the class implementation
QLSQDialog::QLSQDialog(QWidget *parent) : QDialog(parent)
{
setupUi(this); // Set up dialog
(...)
txtInfo->clear(); // txtInfo is a QPlainTextEdit created in Designer
(...)
}
void QLSQDialog::UpdateDisplay(const QString &msg, int iter, double norm)
{
lblChi->setText(QString::number(norm,'f',12));
if (iter >= 0) lblIt->setText(QString::number(iter));
txtInfo->appendPlainText(msg);
}
void QLSQDialog::on_btnRun_clicked()
{
txtInfo->clear(); // Offending line in second run
Enables(true);
emit Run();
}
void QLSQDialog::Enables(bool running)
{
bool Idle = !running;
bool HasReport = !txtInfo->document()->isEmpty();
(...)
btnReport->setEnabled(Idle && HasReport);
}
txtInfo is the QPlainTextEdit object. I call a txtInfo->clear() when the object is
created to show an empty text edit. When I click on a 'Run' tool button its default slot emits a Run signal that will start the new thread. The txtInfo QPlainTextEdit is updated in this thread until it finishes (in fact the thread emits a signal that is caught in the main application that in turn calls the UpdateDisplay).
If I click on the run button a second time, then I get the crash and the error. If I replace txtInfo->clear(), txtInfo->document()->clear(), by txtInfo->setPlainText("") or by txtInfo->document()->setPlainText("") the problem is the same (crash at second execution). Occasionally, but not frequently, I can run a few times (of the order of 10) before crashing.
Finally, if I comment out the txtInfo->clear() line, then I can run the routine as much as I tried (in one test I got tired after running it about 80 times).
My only (almost random) guess is that the problem is somehow related to the update from the thread (which emits a signal that is caught and in turn just calls the UpdateDisplay function). The reason I think so is that if I comment out the signals and just create a new button to call the UpdateDisplay with some bogus information, everything is fine.
A qApp->processEvents() before the offending line has no effect.
I am stuck here. Any ideas are welcome. For instance, is there any test I can do to verify that calling the clear() method is ok?
I finally tracked this problem down to a nasty memory leak in my code. I "repaired" the code but I am still a little bit puzzled by why the problem was happening.
Basically, I was creating a large vector<double> somewhere and passing its address to a function that called for a vector<double> * variable. The problem was that the original vector ceased to be before the function finished working with. Classic dumb mistake. Probably the QPlainTextEdit document was allocating space in the area where the vector<double> used to be: erratic behavior expected. But I would not expect a crash.
The vector was "read-only". The function using it, only read the values and made calculations stored somewhere else. Let's now assume that the plain text creates something in the memory previously addressed by the vector<double>. In this case, when I QPlainTextEdit::clear() the plain text document, the values previously pointed by the vector change and I would expect the calculations to be non sense. I would also accept a crash when the function access the now deceased pointer to vector<double>. But I would not expect the program to crash when I clear the text, which after all is a valid pointer.
Anyway, if anybody has a though, I'd be curious to know why the crash happens. But otherwise, the problem is gone once the leak was repaired. And of course, knowing the reason is absolutely no excuse to not repair the leak.
Hey guys .. Well ever since I've started programming in Qt, I've been having problems regarding scope visibility of objects I've defined .. Till now I've managed to find ways to get around these things but now its getting annoying ..
For example, I have this class defined called Canvas:
class Canvas : public QWidget//, public MainWindow
{
Q_OBJECT
public:
void refreshFoldersList(QString inputPath);
void browseFolders();
private:
QListWidget *foldersList;
};
#endif // CANVAS_H
Now even though foldersList is private, refreshFoldersList() should be able to 'see' it, right ? But in my case it can't .. ! I first initialize foldersList in the browseFolders() function and then from within browseFolders(), I call refreshFoldersList() ... Any code inside refreshFoldersList() dealing with foldersList immediately throws a segmentation fault ... I've checked the pointer values for foldersList when the scope is in browseFolders() and refreshFoldersList() .. the values don't match .. its like either I'm trying to access something I'm not supposed to, or that I'm trying to access an object which has not been initialized yet ..
Any clues on this ?
A related issue ... I have another class MainWindow (inherited from QMainWindow) .. In this class I have an instance of the class Canvas .. this instance is named canvas .. I initialize canvas in MainWindow's constructor, and set canvas's parent to the MainWindow instance initializing it .. based on this, I used the following code to access a MainWindow function from within the Canvas class:
((MainWindow*)parent())->someFunctionDefinedInMainWindow();
Before, the above code used to work .. but then like 2-3 days ago it stopped working all of a sudden .. Now it got me to inside the MainWindow function I was calling (namely someFunctionDefinedInMainWindow() ), but from there if I tried to access any variable defined in MainWindow, I got a Segmentation Fault, again with pointer values not matching .. The way I solved this is by defining a variable as:
void * papa;
.. inside Canvas, and then when I initialized canvas, I set:
canvas->papa = this; //this being the MainWindow instance initializing canvas
Now I accessed MainWindow's functions like this:
((MainWindow*)papa)->someFunctionDefinedInMainWindow();
.. which works!
But again, I would like to know the nature of these problems .. Am I doing something wrong or what ?
The bug is here (code from your comment to liaK):
QListWidget *foldersList = new QListWidget();
You are creating a local variable instead of initializing the class member. Change the code to:
foldersList = new QListWidget();
And probably there is no need for foldersList to be a pointer at all, so your class declaration could be:
private:
QListWidget foldersList;
throws a segmentation fault
Probably some error in your initialization.. How you are initializing it?? Showing the code will be helpful.
And you are using --> instead of ->
Check out this link
Sure it is not a bug with Qt.
or that I'm trying to access an object
which has not been initialized yet
Perhaps you are trying to access an object that hasn't been initialised yet? How and where do you initialise folderList?
Now even though foldersList is private, refreshFoldersList() should be able to 'see' it, right ? But in my case it can't .. ! I first initialize foldersList in the browseFolders() function and then from within browseFolders(), I call refreshFoldersList() ... Any code inside refreshFoldersList() dealing with foldersList immediately throws a segmentation fault
If there was any problem with members visibility, your code wouldn't even compile. Your segfault must be related to something else.
I'm afraid you'll have to show more code for us to be able to help you efficiently.
Moreover, you're using C casts while Qt requires you to write C++. That can only make things worse.
So instead of:
((MainWindow*)parent())-->someFunctionDefinedInMainWindow();
You should use either dynamic_cast<> or static_cast<>, depending on what you want to achieve.
I'm just learning about gtkmm for c++.
I'm having trouble getting a simple TextBuffer to add a new line of text.
I have a class called OutputBox which is an HBox with a TextViewer (called messages) and a TextBuffer (called textBuffer) in it.
Here is a small chunck of the OutputBox class:
OutputBox::OutputBox() {
textBuffer = messages.get_buffer();
};
void OutputBox::addText( string newText) {
textBuffer->insert(textBuffer->begin(), newText);
};
Now I expect that when I pass a string into addText, the new string will be added to the buffer, but instead I get a seg fault.
After running it through gdb, I see that the error comes from the gtkmm libraries here:
template <class T_CppObject> inline
T_CppObject* RefPtr<T_CppObject>::operator->() const
{
return pCppObject_;
}
I'm not really sure what this is telling me either. I assume that I'm incorrectly using the class.
I would advise attaching a debugger to see where the fault occurs.
If it occurs within GTKmm libraries, then you are probably using the API incorrectly. If it occurs in your code then it will point you in the right direction
:)