Debug Assertion Fails When Trying to Write to Log4cpp Stream - c++

I'm trying to enable logging with log4cpp in the following way.
class Foo
{
private:
log4cpp::Appender* _logAppender;
log4cpp::Layout* _logAppenderLayout;
}
Foo::Foo()
{
_logAppender = new log4cpp::FileAppender("foo", "logs/bar.log"));
_logAppenderLayout = new log4cpp::BasicLayout();
_logAppender.setLayout(_logAppenderLayout);
log4cpp::Category::getRoot().setPriority(log4cpp::Priority::DEBUG);
log4cpp::Category::getRoot().addAppender(_logAppender);
// Crash on line below.
log4cpp::Category::getRoot().debugStream() << "test";
}
When I get to the line where I try to write "test" to the log, I get a crash that says "Debug Assertion Failed!" The assertion is in f:\dd\vctools\crt_bld_self_64_amd64\crt\src\write.c Line 67. The assert that fails is
fh >= 0 && (unsigned)fh < (unsigned)_nhandle
I have created the logs directory and the bar.log file to make sure it exists. I have also confirmed that both my application and the library were built as 64-bit multithreaded debug DLLs. There was no 64 bit build in the log4cpp source, so I created one based on the 32-bit build configuration. I'm using the latest version of log4cpp.

It is old post, but I guess solution for this problem may be useful for somebody.
Most probably you just forgot to create directory "logs" that is in your code.
This is the problem of closed stream: logger does not auto-create directories for your logs, so, no directory -> no file -> open file failed -> invalid file handler -> exception. You should create directories manually. Macros assertions and no more info is sad.

Just discovered this question and tried to get this assertion.
I have built log4cpp (ver 1.1) library and user1229080's test for Win32 and x64 platforms in MSVC2010 and have got no assertion.
I added few lines to get it compiled, and removed "logs" dir from the file path just to get rid of issues related to absent directories:
#include "stdafx.h"
#include <log4cpp/Category.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/BasicLayout.hh>
class Foo
{
private:
log4cpp::Appender* _logAppender, *_conAppender;
log4cpp::Layout* _logAppenderLayout;
public:
Foo();
};
Foo::Foo()
{
_conAppender = new log4cpp::OstreamAppender("con", &std::cout);
_logAppender = new log4cpp::FileAppender("foo", "bar.log");
_logAppenderLayout = new log4cpp::BasicLayout();
_logAppender->setLayout(_logAppenderLayout);
log4cpp::Category::getRoot().setPriority(log4cpp::Priority::DEBUG);
log4cpp::Category::getRoot().addAppender(_logAppender);
log4cpp::Category::getRoot().addAppender(_conAppender);
// Crash on line below. (but not in msvc2010)
log4cpp::Category::getRoot().debugStream() << "test" << log4cpp::eol;
}
int main(int argc, char* argv[]) {
Foo f;
return 0;
}
Which version of visual c++ you encountered the assertion on?

Related

VS 2017 C++ - "cannot open source file 'sqlite3.h' "

I am starting to learn how to use Sqlite and build databases however I would like to be able to work with these databases in C++. Whenever I start I am unable to accomplish anything because Visual Studio gives me the error in the title. when I try:
#include <sqlite3.h>
I have tried moving all of the code from sqlite's amalgamation into my project file and it still did not work. I tried using:
"sqlite3.h"
instead of
<sqlite3.h>
I also added the 'amalgamation' folder as a directory which makes the first error away but I am given a linker error on the build.
I feel as though I missed something in the setup that is not allowing me to do continue, but I have searched everything I can imagine and found no answers. Any help would be greatly appreciated. Thank you.
UPDATE: I am in a CLR project because I will need to attach a GUI to this Database and I am unable to compile the .c files from the amalgamation. Those files seem to be the solution to my problem though, so is there any way to get around that issue of a C file not being able to be compiled in a CLR project?
The following method demonstrate how to link with sqlite3.dll.
Download and copy the following folder to your Visual C++ solution folder.
https://github.com/mcychan/DNAssist/tree/master/sqlite3
The sqlite3 folder contains both x86 and x64 version of sqlite dll and lib.
You may upgrade to latest version of sqlite.dll.
Download and copy the following files to your Visual C++ project folder, add reference for them.
https://github.com/mcychan/DNAssist/blob/master/DNAssist/CppSQLite3.cpp
https://github.com/mcychan/DNAssist/blob/master/DNAssist/CppSQLite3.h
The following is the sample code to query the database.
#include "CppSQLite3.h"
#include <iostream>
#include <string>
using namespace std;
CppSQLite3DB db;
bool GetDatabase(const string& dbPath)
{
try {
db.open(dbPath.c_str());
return true;
}
catch (CppSQLite3Exception& e)
{
cout << _T("Cannot open database: ") << dbPath << _T("\n");
return false;
}
}
void IssueQuery(const string& querystring, const string& field1)
{
try {
CppSQLite3Query q = db.execQuery(querystring.c_str());
while (!q.eof()) {
CString temp2(q.fieldValue(field1.c_str()));
TRACE(temp2 + _T("\n"));
q.nextRow();
}
}
catch (CppSQLite3Exception& e)
{
cout << _T("Cannot execute query: ") << querystring << _T("\n");
}
}
void main()
{
if(GetDatabase("C:\\test.sqlite"))
IssueQuery("SELECT * FROM DUAL", "X");
}

Error Loading Enclave: Couldn't open file with CreateFile()

I'm trying to write a simple SGX project for a start. So I have this main host application routine that I've pretty much copied from Lars Richter's blog:
#define ENCLAVE_FILE _T("Enclave.signed.dll")
#include <tchar.h>
#include <cstdio>
#include "sgx_urts.h"
#include "Enclave_u.h"
int main()
{
sgx_enclave_id_t eid;
sgx_status_t ret = SGX_SUCCESS;
sgx_launch_token_t token = { 0 };
int updated = 0;
ret = sgx_create_enclave(ENCLAVE_FILE, SGX_DEBUG_FLAG, &token, &updated, &eid, NULL);
if (ret != SGX_SUCCESS) {
printf("\nApp: error %#x, failed to create enclave.\n", ret);
}
scanf("\n");
return 0;
}
It compiles fine (I'm using the Intel C++ 17.0 compiler with Visual Studio 2015) but it doesn't load the enclave. I get the following error message:
[sgx_create_enclavew ..\urts\win\urts.cpp:195] Couldn't open file with CreateFile()
App: error 0x200f, failed to create enclave.
Go to app_test_save project setting. Under Debugging, change working directory to $(SolutionDir)Debug. This answer assumes that both projects app_test_save and enclave_test_save belong to the same solution.
As Neil pointed out, sgx_create_enclave couldn't find the dll when the program was being run from within Visual Studio's debugger. It worked fine when I directly ran the executable in the "Debug" folder.
So a simple trick to make it work in both settings is to do the following:
#define ENCLAVE_FILE _T("../Debug/Enclave.signed.dll")
According to this : https://software.intel.com/en-us/forums/intel-software-guard-extensions-intel-sgx/topic/623738
If you are using the Local SGX Debugger, Please make sure change the "current working directory" pointing to $(OutDir) instead of $(ProjectDir).
Configuration Properties --> Debugging --> Working Directory --> $(OutDir).
Error is basically means it could not locate your .dll file.
Do dir /a/s to find Enclave.signed.dll then change the name appropriately.
When you create enclave it will generate signed.dll file. If your enclave name is Enclave12 then the DLL name is Enclave12.signed.dll. You fix this then you should be good to go.

Wt c++ critical error when creating WApplication object

I have problem with my Wt web application. It's rather simple app, I do not need to deploy it on any external server (only localhost), so built in whttpd server provided by framework is sufficient for my needs. I create an executable file in release mode (Visual Studio 2015), run it, and when I open localhost:8080 in browser to access application, I get an error. In debug mode however everything works well.
Debug console shows this:
Exception thrown at 0x00007FFB08477788 in
Neural_network_visualisation.exe: Microsoft C++ exception:
std::runtime_error at memory location 0x00000025E30FB408.
Critical error detected c0000374
Neural_network_visualisation.exe has triggered a breakpoint.
main.cpp
#include "MyApplication.h"
#include "MyContainerWidget.h"
WApplication *createApplication(const WEnvironment& env) //exception is thrown here
{
Wt::WApplication *app = new Wt::WApplication(env); //error c0000374
app->setCssTheme("polished");
new MyContainerWidget(app->root());
return app;
}
int main(int argc, char **argv)
{
return WRun(argc, argv, &createApplication);
}
The exception is thrown just after entering createApplication function, but the program doesn't crash there. After executing first line critical error is shown and application stops.
The code is so simple that I can't see any problem with it. My guess is that release mode expects some special configuration to work with Wt, but official documentation doesn't mention anything more is needed while using built-in http server. Can anybody with Wt experience help me with this?
Edit 1:
I changed my code so it looks like this:
#include "MyApplication.h"
#include "MyContainerWidget.h"
WApplication *createApplication(const WEnvironment& env)
{
//Wt::WApplication *app =
// app->setCssTheme("polished");
//new MyContainerWidget(app->root());
return new Wt::WApplication(env);;
}
int main(int argc, char **argv)
{
return WRun(argc, argv, &createApplication);
}
Now the error is different and states:
HEAP[Neural_network_visualisation.exe]: Invalid address specified to
RtlValidateHeap( 000002A04F550000, 000002A05117F060 )
So this is a memory management problem. In debug mode this management works quite different than in release, that's why I do not get any error while in debug. Unfortunately, I still do not know how to fix this. Any ideas?
Problem solved. I still can't exactly tell what was the cause, but I recompiled Wt and Boost libraries and changed project properties to match with the Wt examples. I also had to change build type from x64 to x86. It works now.

Using Qt to configure OpenCV library failed

I'm new to Qt creator. Yesterday, I followed the official instructions to configure OpenCV library but it failed. I tried everything on the Internet but it just didn't work. Detailed are listed as belows:
test code is simple, I only want to ensure whether the library works:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
int main(int argc, char *argv[])
{
cout << "Hello World!" << endl;
return 0;
}
My project configuration is like below :
I thought there might be a problem in debugger. I configure the debugger and I'm quite sure it's ok. The picture is here:
But it just doesn't work. When I click build and run, it says:
C1083: cannot open containing files: "opencv2/opencv.hpp": No such
file or directory.
What's strange is when I include <files> in the editor, the automatic code completion can detect the existence of the OpenCV library and hint after <opencv2/> that there are opencv.hpp, core.hpp .etc. and in the Include Hierarchy, the opencv.hpp exists.
So what might be the problem?

The system cannot find the file specified. in Visual Studio

I keep getting this error with these lines of code:
include <iostream>
int main()
{
cout << "Hello World" >>;
system("pause");
return 0;
}
"The system cannot find the file specified"
The system cannot find the file specified usually means the build failed (which it will for your code as you're missing a # infront of include, you have a stray >> at the end of your cout line and you need std:: infront of cout) but you have the 'run anyway' option checked which means it runs an executable that doesn't exist. Hit F7 to just do a build and make sure it says '0 errors' before you try running it.
Code which builds and runs:
#include <iostream>
int main()
{
std::cout << "Hello World";
system("pause");
return 0;
}
The code should be :
#include <iostream>
using namespace std;
int main() {
cout << "Hello World";
return 0;
}
Or maybe :
#include <iostream>
int main() {
std::cout << "Hello World";
return 0;
}
Just a quick note: I have deleted the system command, because I heard it's not a good practice to use it. (but of course, you can add it for this kind of program)
I had a same problem and this fixed it:
You should add:
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib\x64 for 64 bit system
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib for 32 bit system
in Property Manager>Linker>General>Additional Library Directories
Another take on this that hasn't been mentioned here is that, when in debug, the project may build, but it won't run, giving the error message displayed in the question.
If this is the case, another option to look at is the output file versus the target file. These should match.
A quick way to check the output file is to go to the project's property pages, then go to Configuration Properties -> Linker -> General (In VS 2013 - exact path may vary depending on IDE version).
There is an "Output File" setting. If it is not $(OutDir)$(TargetName)$(TargetExt), then you may run into issues.
This is also discussed in more detail here.
This is because you have not compiled it. Click 'Project > compile'. Then, either click 'start debugging', or 'start without debugging'.
I resolved this issue after deleting folder where I was trying to add the file in Visual Studio. Deleted folder from window explorer also. After doing all this, successfully able to add folder and file.
I was getting the error because of two things.
I opened an empty project
I didn't add #include "stdafx.h"
It ran successfully on the win 32 console.