Q_ASSERT - "Incorrect format specifier" - c++

I have a Q_ASSERT():
Q_ASSERT(QString("%1").arg(0) != "0");
When it fails, it shows an Abort/Retry/Ignore message box, but in the text it says:
File: f:\dd\vctools\crt_bld\self_x86\crt\src\output.c
Line: 1120
Expression: ("Incorrect format specifier", 0)
With this call stack:
0 _output_s_l output.c 1120 0x5dbee38d
1 _vsnprintf_helper vsprintf.c 140 0x5dbb00e8
2 _vsnprintf_s_l vsprintf.c 288 0x5dbb0610
3 _vsnprintf_s vsprintf.c 340 0x5dbb0880
4 _VCrtDbgReportA dbgrptt.c 301 0x5dc2571e
5 _CrtDbgReportV dbgrpt.c 241 0x5dc24992
6 _CrtDbgReport dbgrpt.c 258 0x5dc2494b
7 qt_message_output qglobal.cpp 2232 0x5d43bacf
8 qt_message qglobal.cpp 2298 0x5d43bc69
9 qFatal qglobal.cpp 2481 0x5d43c059
10 qt_assert qglobal.cpp 1999 0x5d43b629
But if I change it to:
Q_ASSERT(QString("0").arg(0) != "0");
It works as expected, showing
File: global\qglobal.cpp
Line: 2232
ASSERT: "QString("0").arg(0) != "0"" in file ..\MyProject\tester.cpp, line 132
So why this strange behavior, is it a bug in Qt?

Turned out to be a bug in Qt, in qt_message_output they have this line:
int ret = _CrtDbgReport(_CRT_ERROR, __FILE__, __LINE__, QT_VERSION_STR, buf);
Should have been
int ret = _CrtDbgReport(_CRT_ERROR, __FILE__, __LINE__, QT_VERSION_STR, "%s", buf);
They are passing the entire line contained in Q_ASSERT() as a format specifier.

Related

QT Crash on exit with QtWebEngine

So I am using WebEngineView in my QML like this:
...
Loader {
// some properties
sourceComponent: WebEngineView {
...
}
}
In the c++ logic I am using QQuickWebEngineProfile::defaultProfile() in the constructor and destructor
MainViewModel(QObject* parent) : QObject(parent)
{
// using QQuickWebEngineProfile::defaultProfile();
// getting cookieStore of the profile and connect it to some slots
}
~MainViewModel()
{
// using QQuickWebEngineProfile::defaultProfile();
// getting cookieStore of the profile and disconnect it from MainViewModel
}
So it's working perfectly, but when I am trying to close my app (calling qApp->quit()), it crashes. If I remove WebEngineView from QML it works, if I remove using of defaultProfile() in c++ it works. But I need these things.
Dump:
1 _threadid ucrtbased 0x7ffb48687c75
2 _threadid ucrtbased 0x7ffb48687e13
3 abort ucrtbased 0x7ffb4869e01d
4 `anonymous namespace'::messageHandler application.cpp 46 0x7ff7c2d2b1bf
5 qt_message_print qlogging.cpp 1844 0x7ffb08317fdf
6 qt_message qlogging.cpp 379 0x7ffb08318657
7 QMessageLogger::fatal qlogging.cpp 890 0x7ffb08316612
8 qt_assert qglobal.cpp 3354 0x7ffb08307a48
9 QAccessible::registerAccessibleInterface qaccessible.cpp 747 0x7ffb01df22bd
10 QAccessible::uniqueId qaccessible.cpp 767 0x7ffb01df2247
11 QQuickWebEngineViewPrivate::widgetChanged qquickwebengineview.cpp 982 0x7ffb4b2b5bda
12 QQuickWebEngineViewPrivate::bindViewAndWidget qquickwebengineview.cpp 972 0x7ffb4b2b5b6e
13 QQuickWebEngineViewPrivate::releaseProfile qquickwebengineview.cpp 200 0x7ffb4b2b2349
14 QtWebEngineCore::ProfileAdapter::~ProfileAdapter profile_adapter.cpp 127 0x7ffad4237f20
15 QtWebEngineCore::ProfileAdapter::`vector deleting destructor' Qt5WebEngineCored 0x7ffad4238678
16 std::default_delete<QtWebEngineCore::DevToolsFrontendQt::NetworkResourceLoader>::operator() memory 1758 0x7ffad41d9a75
17 std::unique_ptr<QtWebEngineCore::WebChannelIPCTransportHost,std::default_delete<QtWebEngineCore::WebChannelIPCTransportHost>>::reset memory 1910 0x7ffad4287857
18 QtWebEngineCore::WebEngineContext::destroy web_engine_context.cpp 339 0x7ffad4295a87
19 QtWebEngineCore::WebEngineContext::destroyContextPostRoutine web_engine_context.cpp 425 0x7ffad4295bf8
20 qt_call_post_routines qcoreapplication.cpp 336 0x7ffb087276ef
21 QApplication::~QApplication qapplication.cpp 714 0x7ffb07767046
...
30 main main.cpp 63 0x7ff7c21d4640
It tried to access main window which at that moment has already been deleted. To resolve this problem I had to delay destruction of my main window. I've done it with
QObject::deleteLater()

Getting "No EOF Marker was found in the PDF file." after using Tesseract C++ API but Tesseract command line utility works fine

I am working on some images using Tesseract & then trying to merge all of the generated PDFs using PoDoFo C++ library.
Have tried 2 approaches (1st one is what I require) :
Using Tesseract C++ API & PoDoFo C++ library
My code is somewhat like this:
For OCR part (run for 001.jpg & 002.jpg):
const char* input_image = "001.jpg";
const char* output_base = "001";
const char* datapath = "/home/test/Desktop/Example2";
int timeout_ms = 5000;
const char* retry_config = nullptr;
bool textonly = false;
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
if (api->Init(datapath, "eng")) {
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
tesseract::TessPDFRenderer *renderer = new tesseract::TessPDFRenderer(
output_base, api->GetDatapath(), textonly);
bool succeed = api->ProcessPages(input_image, retry_config, timeout_ms, renderer);
if (!succeed) {
fprintf(stderr, "Error during processing.\n");
return EXIT_FAILURE;
}
api->End();
return EXIT_SUCCESS;
For PDF merging part:
void mergePDF(std::vector<char*> inputfiles,char* outputfile) {
try {
/*Reading first PDF */
fprintf(stdout,"Reading file: %s\n",inputfiles[0]);
PoDoFo::PdfMemDocument doc1;
doc1.Load(inputfiles[0]);
/*Reading Second PDF */
fprintf(stdout,"Reading file: %s\n",inputfiles[1]);
PoDoFo::PdfMemDocument doc2;
doc2.Load(inputfiles[1]);
/* Appending doc1 to doc1 */
doc1.Append(doc2);
fprintf(stdout,"Writing files to %s\n ",outputfile);
doc1.Write(outputfile);
}
catch(const PoDoFo::PdfError& e) {
throw e;
}
}
int main(int argc,char* argv[]) {
if (argc < 2) {
printHelp();
exit(EXIT_FAILURE);
}
PoDoFo::PdfError::EnableDebug(false);
std::vector<char*> inputfiles;
char* outputfile;
inputfiles.emplace_back(argv[1]);
inputfiles.emplace_back(argv[2]);
outputfile = argv[3];
try {
mergePDF(inputfiles,outputfile);
}
catch(const PoDoFo::PdfError &e) {
fprintf(stderr,"Error %i occured!\n",e.GetError());
e.PrintErrorMsg();
return e.GetError();
}
exit(EXIT_SUCCESS);
}
Output:
Warning: Invalid resolution 0 dpi. Using 70 instead.
Warning: Invalid resolution 0 dpi. Using 70 instead.
Reading file: /home/test/Desktop/Example2/001.pdf
Error 17 occured!
PoDoFo encountered an error. Error: 17 ePdfError_NoEOFToken
Error Description: No EOF Marker was found in the PDF file.
Callstack:
#0 Error Source: /home/test/podofo/src/podofo/doc/PdfMemDocument.cpp:263
Information: Handler fixes issue #49
#1 Error Source: /home/test/podofo/src/podofo/base/PdfParser.cpp:272
Information: Unable to load objects from file.
#2 Error Source: /home/test/podofo/src/podofo/base/PdfParser.cpp:310
Information: EOF marker could not be found.
#3 Error Source: /home/test/podofo/src/podofo/base/PdfParser.cpp:1528
Using Tesseract command line utility & PoDoFo C++ library
For OCR part, I use Tesseract CLI tool as follows:
tesseract 001.jpg 001 pdf
tesseract 002.jpg 002 pdf
For PDF merging part, the code is same as in point no. 1) above
Output:
Reading file: /home/test/Desktop/Example2/001.pdf
Reading file: /home/test/Desktop/Example2/002.pdf
Fixing references in 13 0 R by 12
Fixing references in 14 0 R by 12
Fixing references in 15 0 R by 12
Fixing references in 16 0 R by 12
Fixing references in 17 0 R by 12
Fixing references in 18 0 R by 12
Fixing references in 19 0 R by 12
Fixing references in 20 0 R by 12
Fixing references in 21 0 R by 12
Fixing references in 22 0 R by 12
Fixing references in 23 0 R by 12
Fixing references in 24 0 R by 12
Reading file: /home/test/Desktop/Example2/output.pdf
I wonder why I am getting the EOF marker issues after using Tesseract C++ API but no such issue after using Tesseract CLI tool.
Am I missing something in the OCR code part in point no. 1) above?

SFML program crashes with non-ASCII character in window title

So i just install SFML and was trying to do a simple programme, and the compilation works fine, but it's when I try to run the executable that it fails.
First I needed to do a trick to authorize the application to launch because it wasn't recognize by apple I follow those instruction to install SFML https://www.sfml-dev.org/tutorials/2.5/start-osx.php
After I managed to authorize the application when I run it I got this error message:
2020-11-24 10:20:55.638 exemple-graphisme1[2768:29657] *** Assertion failure in -[SFWindow setTitle:], NSWindow.m:2490
2020-11-24 10:20:55.640 exemple-graphisme1[2768:29657] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: aString != nil'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff204956af __exceptionPreprocess + 242
1 libobjc.A.dylib 0x00007fff201cd3c9 objc_exception_throw + 48
2 CoreFoundation 0x00007fff204be512 +[NSException raise:format:arguments:] + 88
3 Foundation 0x00007fff212776c9 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
4 AppKit 0x00007fff22c7da75 -[NSWindow setTitle:] + 142
5 libsfml-window.2.5.dylib 0x000000010e5bf4fd _ZN2sf4priv15WindowImplCocoaC2ENS_9VideoModeERKNS_6StringEmRKNS_15ContextSettingsE + 269
6 libsfml-window.2.5.dylib 0x000000010e5b0e03 _ZN2sf4priv10WindowImpl6createENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE + 67
7 libsfml-window.2.5.dylib 0x000000010e5b02e5 _ZN2sf6Window6createENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE + 469
8 libsfml-graphics.2.5.dylib 0x000000010e61e823 _ZN2sf12RenderWindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE + 99
9 exemple-graphisme1 0x000000010e582849 main + 233
10 libdyld.dylib 0x00007fff2033e631 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
[1] 2768 abort ./exemple-graphisme1
My code is very simple and only draw a point on the window
#include <SFML/Graphics.hpp>
using namespace sf;
using Point = Vector2f;
void draw_point(RenderWindow& w, Point pos, Color color) {
Vertex p[] = { Vertex(pos, color) };
w.draw(p, 1, sf::Points);
}
int main()
{
// ed: The line below originally contained "Hello World"
RenderWindow window(VideoMode(640, 480), "Ma super fenêtre");
window.clear(Color::White);
draw_point(window, {120, 5}, Color::Red);
window.display();
sleep(seconds(10));
return 0;
}
And I compiled my programme like this:
g++ -std=c++11 exemple-graphisme1.cpp -o exemple-graphisme1 -lsfml-system -lsfml-window -lsfml-graphics
Thanks for reading !
So the problem was very simple, I edited my code to translate it in english but in the beginning the first line was
RenderWindow window(VideoMode(640, 480), "Ma super fenêtre");
And the error occured because of the character 'ê'.
Thanks for your answer and sorry for this disappointing answer !

Macro meaning in c++

I happened to come across a macro definition as shown below in a c++ code(https://github.com/LairdCP/UwTerminalX).
#define SpeedModeInactive 0b00
#define SpeedModeRecv 0b01
#define SpeedModeSend 0b10
#define SpeedModeSendRecv 0b11
When I tried to compile it using visual studio 2010, it giving me the following error.
Error 14 error C2059: syntax error : ')'
What is the meaning of this macro?
The usage of macro is shown below.
if ((gchSpeedTestMode == SpeedModeSendRecv || gchSpeedTestMode == SpeedModeRecv) && (gintSpeedBytesReceived10s > 0 || ui->edit_SpeedBytesRec10s->text().toInt() > 0))
{
//Data has been received in the past 10 seconds: start a timer before stopping to catch the extra data packets
gchSpeedTestMode = SpeedModeRecv;
gtmrSpeedTestDelayTimer = new QTimer();
gtmrSpeedTestDelayTimer->setSingleShot(true);
connect(gtmrSpeedTestDelayTimer, SIGNAL(timeout()), this, SLOT(SpeedTestStopTimer()));
gtmrSpeedTestDelayTimer->start(5000);
//Show message that test will end soon
ui->statusBar->showMessage("Waiting 5 seconds for packets to be received... Click cancel again to stop instantly.");
}
It is because visual studio doesn't recognize binary values (starting with 0bXXX) but, you can simply define as a hex values just like that:
#define SpeedModeInactive 0x0
#define SpeedModeRecv 0x1
#define SpeedModeSend 0x2
#define SpeedModeSendRecv 0x3
The use of binary literals was first introduced in C++14 which is not supported by VS2010, https://en.wikipedia.org/wiki/C%2B%2B14#Binary_literals

C++ file cfx_pmta.cxx won't compile

I am trying to compile the PMTA cfx_pmta.cxx file so I can make some changes but I can't get it to compile before I have even made any changes.
Here are the includes at the to of the file:
#if defined(sun)
#define _POSIX_PTHREAD_SEMANTICS
#endif
#ifdef _WIN32
#include <windows.h>
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#if defined(UNIT_TEST)
#define __dllexport
#else
#define __dllexport __declspec(dllexport)
#endif
#else
#define __dllexport
#define __cdecl
#endif
#ifdef __unix__
#include <signal.h>
#include <errno.h>
#endif
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#if defined _WIN32
#include <time.h>
#endif
#include "stdafx.h"
#include "cfx.h"
#include "include/submitter/PmtaMsg.h"
#include "include/submitter/PmtaConn.h"
#include "include/submitter/PmtaRcpt.h"
229 Error C2079 'tmp' uses undefined class 'std::basic_ostringstream,std::allocator>'
230 Error C2297 '<<': illegal, right operand has type 'const char [9]'
230 Error C3861 'time': identifier not found
230 Warning C4552 '<<': operator has no effect; expected operator with side-effect
231 Error C2228 left of '.str' must have class/struct/union
229 std::ostringstream tmp;
230 tmp << "boundary" << time(0);
231 std::string boundary = tmp.str();
617 Error C2079 'f' uses undefined class 'std::basic_ifstream>'
617 Error C2440 'initializing': cannot convert from 'const char *' to 'int'
625 Error C2228 left of '.read' must have class/struct/union
626 Error C2228 left of '.gcount' must have class/struct/union
629 Error C2228 left of '.close' must have class/struct/union
621 Error C4996 'strerror': This function or variable may be unsafe. Consider using strerror_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
604 void
605 attachFile(const std::string& boundary, const char* fileName) {
606 addBoundary(boundary);
607 addData("Content-Type: application/octet-stream\n");
608 addData("Content-Disposition: attachment; filename=\"");
609 addData(fileName); addData("\"\n");
610 addData("Content-Transfer-Encoding: base64\n\n");
611
612 if (!PmtaMsgSetEncoding(_message, PmtaMsgENCODING_BASE64)) {
613 die("error setting base64 encoding", PmtaMsgGetLastError(_message));
614 }
615
616 char buffer[8192];
617 std::ifstream f(fileName);
618
619 if (!f) {
620 std::string error = std::string("error opening ") + fileName;
621 die(error, strerror(errno));
622 }
623
624 while (f) {
625 f.read(buffer, sizeof(buffer));
626 addData(buffer, static_cast<int>(f.gcount()));
627 }
628
629 f.close();
630
631 if (!PmtaMsgSetEncoding(_message, PmtaMsgENCODING_8BIT)) {
632 die("error setting 8-bit encoding", PmtaMsgGetLastError(_message));
633 }
634 }
736 Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
736 Error C2144 syntax error: 'void' should be preceded by ';'
733 //------------------------------------------------------------------------------
734 // externally visible tag processor
735//------------------------------------------------------------------------------
736 extern "C" __dllexport void __cdecl
737 ProcessTagRequest(CCFXRequest* request) {
738 try {
739 TagProcessor proc(request);
740 proc.process();
741 }
742 catch (std::bad_alloc&) {
743 request->ThrowException(TAG_ERROR, "out of memory");
744 }
745 catch (char* text) {
746 // this may be thrown by some dummy classes
747 request->ThrowException(TAG_ERROR, text);
748 }
749 catch (CCFXException* e) {
750 request->ReThrowException(e);
751 }
752 catch (...) {
753 // original ColdFusion example comment:
754 // "Catch ALL other exceptions and throw them as Cold Fusion exceptions
755 // (DO NOT REMOVE! -- this prevents the server from crashing in case of
756 // an unexpected exception)"
757 request->ThrowException(TAG_ERROR, "Unexpected error occurred "\
758 "while processing tag.");
759 }
760 };
Any help would be a great!! Just need to be able to get it to compile the DLL and then I can make the changes needed.
Also I'm not a C++ dev so trying to get my head round it, so go easy.
std::cout is a output stream which is defined within the <iostream> header.
in order to use std::string objects you have to include the <string> header
Try adding:
#include<iostream>
#include<string>
In your cxx file.
After some reading I decided to try the Command Line compiler and to my amazement it compiled fine.
So if anyone is intrested in how to recompile the cfx_pmta.cxx file for PMTA to change the Cold Fusion custom tag code run the following command (obviously changing path to match your file locat
cl /LD /MT /EHsc /IC:\path\api\include /IC:\ColdFusion8\include /D "_WIN32" C:\path\api\cfx_pmta.cxx /link C:\path\api\lib\pmta.lib ws2_32.lib advapi32.lib