QVideoProbe control destroyed while it's still being referenced - c++

I created a QCamera, and a QVideoProbe, which will allow me to access each frame of the video.
The problem is that every time I close the app, I get this message in Qt Creator: 'QVideoProbe control destroyed while it's still being referenced!!!'.
I'm using Qt 5.11 on Windows 8.1. My compilator is MSVC 2015 (32-bit).
Here's my code:
Window::Window()
{
if(!checkCameraAvailability())
{
QMessageBox::critical(this, "Error", "No camera is available");
return;
}
m_camera = new QCamera;
m_camera->setCaptureMode(QCamera::CaptureVideo);
m_videoProbe = new QVideoProbe(this);
if(!m_videoProbe->setSource(m_camera))
{
QMessageBox::critical(this, "Error", "setSource");
return;
}
connect(m_videoProbe, SIGNAL(videoFrameProbed(QVideoFrame)), this, SLOT(cameraFrameProbed(QVideoFrame)));
// For now the slot 'cameraFrameProbed' is empty
m_camera->start();
}
What did I miss?

Problem solved! I replaced this line:
m_videoProbe = new QVideoProbe(this);
with this one:
m_videoProbe = new QVideoProbe;
But I still don't understand what was wrong with the first line.
EDIT: The previous solution has a memory leak! Here's the correct one:
I forgot to mention the parent of the QCamera when constructing it, there were actually 2 memory leaks...
So the code becomes:
m_camera = new QCamera(this);
m_videoProbe = new QVideoProbe(this);
And now it works!
Thank you so much Jeremy Friesner!

Related

How to change the image of a MenuSpriteItem Cocos2d-x C++

I've got a button that I need to be a toggle button for the sound of a game. I'm using the MenuSpriteItem class.
auto menuSoundOn = Sprite::createWithSpriteFrameName("soundOn.png");
auto menuSoundOff = Sprite::createWithSpriteFrameName("soundOff.png");
auto menuSoundBtn = MenuItemSprite::create(menuSoundOn, menuSoundOff, CC_CALLBACK_1(LevelsLayer::shutSound, this));
menuSoundBtn->setTag(0);
_mainMenu = Menu::create(menuSoundBtn, nullptr);
this->addChild(_mainMenu);
//Then in my shutSound method
auto menuSoundBtn = _mainMenu->getChildByTag(0);
if (_ifSound){
_ifSound = false;
//Do some stuff to shut the sound
menuSoundBtn->setSelectedImage("noSound.png");
}
else{
_ifSound = true;
//Do some stuff to bring the sound back
menuSoundBtn->setSelectedImage("sound.png");
}
The problem is that getting the Btn from his parent with getChildByTag(0) method I receive a Node according with the documentation, but setSelectedImage is not part of the Node class and there is an error telling me so, so what is the right way to access MenuSpriteItems from their Parents and then manipulate them as in this case by changing the Normal Image?
Greetings.
I've got the answer and It's really powerful and simple.
auto menuSoundBtn = dynamic_cast<MenuItemSprite*>(_mainMenu->getChildByTag(0));
This is the explanation from the guy:
This code will get the child with tag 0 and turn it into a MenuItemSprite* object if it is a MenuItemSprite* object, or it returns null if the object was not a MenuItemSprite*.
Hope it helps someone. Greetings.

Application crashes when opening a window qt

I would like to create a program that shows a question with some answers to the user. My application uses 3 forms: Main Menu, Login Menu and Game Form, and all inherit from an abstract class called Form; I do this because it allows the use of the factory method, which it create a new window when a signal GoFw is emitted by the actual form.
The "loop" that shows the windows is the following: MainMenu -> LoginMenu -> GameForm -> MainMenu...
The problem is when the game is finished (e.g. the count of remaining questions is zero) the GameForm emits the signal GoFw but the application crashes after the show() method (I could see a white window with no buttons before the crash).
The debugger show a messagebox with this error:
The inferior stopped because it triggered an exception.
Stopped in thread 0 by: Exception at 0x723f7b93, code: 0xc0000005: read access
violation at: 0x0, flags=0x0 (first chance).
and QtCreator opens a file called: Disassembler(QHash::findNode)
This is the code of the factory method:
void FormFactory::Init(int formType)
{
///if formType == 1 MainMenu is showed
if(formType == MAIN_MENU_TYPE)
{
//inizializza il puntatore
actualForm = new MainMenu();
}
///else if is == 2 show ProfileMenu
else if(formType == PROFILE_MENU_TYPE)
{
actualForm = new LoginMenu();
}
///else if == 3 show GameForm
else if(formType == GAME_FORM_TYPE)
{
actualForm = new GameForm();
}
///else if there is no match launch an exception
else
throw UnexpectedIdEx();
connect(actualForm, SIGNAL(GoFw(int)), this, SLOT(DisplayForm(int)));
}
void FormFactory::DisplayForm(int i)
{
Reset();
Init(i);
///show the form pointed by actualform
actualForm->show();
}
void FormFactory::Reset()
{
disconnect(actualForm, SIGNAL(GoFw(int)), this, SLOT(DisplayForm(int)));
///if actualform is actually pointing to a form, delete it and set actualForm to zero
if(actualForm!=0)
delete actualForm;
actualForm = 0;
}
And the code of MainMenu.cpp is
MainMenu::MainMenu()
{
setUpGui();
}
void MainMenu::setUpGui()
{
playButton = new QPushButton(tr("Play"));
infoButton = new QPushButton(tr("Info"));
quitButton = new QPushButton(tr("Exit"));
///connect the clicked signal to the related slot
connect(infoButton, SIGNAL(clicked()), this, SLOT(info()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(quit()));
connect(playButton, SIGNAL(clicked()), this, SLOT(emitSig()));
///create the vertical layout
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(playButton);
layout->addWidget(infoButton);
layout->addWidget(quitButton);
setLayout(layout);
setWindowTitle(tr("Menu Principale"));
}
void MainMenu::emitSig()
{
emit GoFw(2);
}
Thank you all for your help,
Luca
I'd suggest rethink your solution, it seems you overcomplicated it with the factory method.
Just use 3 variables for the forms, do the "new" operation once for each, and use show() / hide() methods depending on your signals.
To answer to the crash problem, one reason i see is because you do "delete" in the slot.
From Qt doc:
Warning: Deleting a QObject while pending events are waiting to be delivered can cause a crash. You must not delete the QObject directly if it exists in a different thread than the one currently executing. Use deleteLater() instead, which will cause the event loop to delete the object after all pending events have been delivered to it.

VS 2005 C++ edit content of the CRichEditCtrl instance

I've installed a Windows XP Professional SP3 on a VMWare image and the Visual Studio 2005 on it. I've created a new dialog based C++ MFC project with /clr support. I've put a RichEdit 2.0 control onto the auto-generated dialog and I'm trying to read up a text file and put its content into this RichEdit 2.0 control by button click without formatting. I've added a variable to the RichEdit 2.0 called pCRichEditCtrl and here is my code which doesn't work.
CWinApp inheritance:
BOOL CTextFormatterApp::InitInstance()
{
...
AfxInitRichEdit2();
CWinApp::InitInstance();
...
}
CDialog inheritance:
void CTextFormatterDlg::OnBnClickedButton1()
{
StreamReader^ objReader = gcnew StreamReader("c:\\text.txt");
String ^sLine = "";
sLine = objReader->ReadLine();
while (sLine != nullptr)
{
pCRichEditCtrl.SetSel(pCRichEditCtrl.GetTextLength(), -1);
pCRichEditCtrl.ReplaceSel(CString(sLine));
sLine = objReader->ReadLine();
}
objReader->Close();
}
I don't know whether it counts but I get the following warnings at linking:
TextFormatterDlg.obj : warning LNK4248: unresolved typeref token (01000016) for 'AFX_CMDHANDLERINFO'; image may not run
TextFormatter.obj : warning LNK4248: unresolved typeref token (01000012) for 'AFX_CMDHANDLERINFO'; image may not run
TextFormatterDlg.obj : warning LNK4248: unresolved typeref token (01000015) for 'IAccessibleProxy'; image may not run
I'm not sure what I'm doing because I'm familiar only with newer frameworks and I don't know either Windows.
Input file exists, I can see the read text if I debug the application but I can't see any changes in the edit box. I've tried to call pCRichEditCtrl.UpdateData(true); but nothing has changed.
Is it enough to add a variable for getting the controller of the box (pCRichEditCtrl)? It seems to the pointer doesn't point to the proper control item.
Do you have any idea what is missing?
There's no need to use CLI to just read text files, try something like:
void CTextFormatterDlg::OnBnClickedButton1()
{ CStdioFile f1;
CString sLine;
if (!f1.Open(_T("c:\\text.txt"), CFile::modeRead | CFile::typeText))
return;
while (f1.ReadString(sLine))
{ pCRichEditCtrl.SetSel(pCRichEditCtrl.GetTextLength(), -1);
pCRichEditCtrl.ReplaceSel(sLine);
}
f1.Close();
}
EDIT: control variable pCRichEditCtrl
a) should be declared in the dialog class as CRichEditCtrl pCRichEditCtrl;
b) should be connected to the ID of the control (e.g.: IDC_RICHEDIT21), like
void CTextFormatterDlg::DoDataExchange(CDataExchange* pDX)
{ CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_RICHEDIT21, pCRichEditCtrl);
}
c) I have tested the following code and it works for me (adds "aa" to the control window on each button click)
pCRichEditCtrl.SetSel(pCRichEditCtrl.GetTextLength(), -1);
pCRichEditCtrl.ReplaceSel(TEXT("aa"));
I share the final solution with the community to be available for those who faces with the same issue. I don't know why do I have to use Update(FALSE); on the CWinApp inheritance two times but it solves everything. If someone has an idea or a better (nicer) solution don't hesitate to share it with us, I'll move the accepted flag to that version (if it is possible, I haven't tried it before).
void CTextFormatterDlg::OnBnClickedButton1()
{
StreamReader^ objReader = gcnew StreamReader("c:\\text.txt");
String ^sLine = objReader->ReadLine();
UpdateData(FALSE); //this is the first unexpected first aid
while (sLine != nullptr)
{
pCRichEditCtrl.SetSel(pCRichEditCtrl.GetTextLength(), -1);
pCRichEditCtrl.ReplaceSel(CString(sLine + "\r\n"));
UpdateData(FALSE); //this is the second unexpected first aid
sLine = objReader->ReadLine();
}
objReader->Close();
}

Qt 4.7.4 QPropertyAnimation not working

I'm trying to have animation on a button click event. But somehow the animation is not working. I have referred the Qt reference docs, but could not find the root cause which is causing the issue
Below is sample code :
void MainWindow::AnimationClick()
{
// define toolbar y movement positions for animation
TOOLBAR_Y_SHOWN = 0;
TOOLBAR_Y_HIDDEN = -m_AnimatedWidget->height();
m_AnimatedWidget = new AnimatedWidget(this);
QPropertyAnimation *m_ani = new QPropertyAnimation(m_AnimatedWidget, "pos", this);
m_ani->setDuration(500);
m_ani->setEndValue(QPoint(m_AnimatedWidget->pos().x(), TOOLBAR_Y_HIDDEN));
m_ani->setEasingCurve(QEasingCurve::InBack);
m_ani->start();
}
With the above implementation nothing is happening on the click event.
Any suggestions , Thanks.
This looks wrong:
TOOLBAR_Y_HIDDEN = -m_AnimatedWidget->height();
m_AnimatedWidget = new AnimatedWidget(this);
First you access m_AnimatedWidget then you allocate it?
When you get a crash, such as segmentation fault, always run your program in a debugger. It would have helped you find this error quite easy as it would have stopped on the line of the error.
m_ani->setDuration(500);
setDuration() argument is expressed in milliseconds. You should probably put more than half a second when you are testing.
I got it. I was not allowing the m_AnimatedWidget to show upon the screen.
Below is the edited snippet.
void MainWindow::AnimationClick()
{
// define toolbar y movement positions for animation
TOOLBAR_Y_SHOWN = 0;
m_AnimatedWidget = new AnimatedWidget(this);
TOOLBAR_Y_HIDDEN = -m_AnimatedWidget->height();
QPropertyAnimation *m_ani = new QPropertyAnimation(m_AnimatedWidget, "pos", this);
m_ani->setDuration(5000);
m_ani->setEndValue(QPoint(m_AnimatedWidget->pos().x(), TOOLBAR_Y_HIDDEN));
m_ani->setEasingCurve(QEasingCurve::InBack);
m_ani->start();
m_AnimatedWidget->show();
}

QTime->addSecs cause segmentation fault

I'm writing simple pomodoro application, which is basically countdown timer. Right now, I've got countdown working, but the weird thing is that when I add another attribute to my class (arbitrary), I get Sedmentation fault error.
Using gdb, the problem should be here:
void Status::showPomodoroTime() {
QTime time = pomodoroTime->addSecs(elapsed);
activeTime->display(time.toString("mm:ss"));
}
where activeTime is QLCDNumber widget and elapsed is int.
More context:
void Status::createDefaultIntervals()
{
pomodoroInterval = new QTime(0, 25);
pomodoroBreak = new QTime(0, 5);
pomodoroLongBreak = new QTime(0, 15);
}
void Status::run()
{
if (pomodoroActive == STOP) {
pomodoroTime = pomodoroInterval;
showPomodoroTime();
}
pomodoroActive = RUN;
updateStatusArea();
timerTick();
}
CreateDefaultInterval definitely runs before showPomodoroTime.
What bugs me, that whole application works fine. Just when I add another attribute, it starts to throw sedfault.
How can variable declaration in *.h file cause segfault in *.cpp?
If you want more code, I can put it anywhere. I just don't know, what place is persistent enough. Don't want to post it here (about 300 lines of code).
check if(pomodoro!= NULL) and then do addSecs().
pomodoroTime is probably uninitialized or deleted