I don't find anything in Protocol Buffers documentation for exception handling in C++. In Javadoc it has clearly defined ones like InvalidProtocolBufferException, but not in C++.
Sometimes I ran my program and it crashes when it finds missing fields in what it thinks a valid message, then it simply stops and throws errors like this:
[libprotobuf FATAL google/protobuf/message_lite.cc:273] CHECK failed:
IsInitialized(): Can't serialize message of type "XXX" because it is
missing required fields: YY, ZZ
unknown file: Failure
C++ exception with description "CHECK failed: IsInitialized(): Can't
serialize message of type "XXX" because it is missing required fields:
YY, ZZ" thrown in the test body.
The source code of message_lite.cc all wrapped around with "GOOGLE_DCHECK" or "InitializationErrorMessage"...
My application does not allow exceptions like this to halt the program (not sure what the term is in C++ but basically no UncheckedExceptions), so I really need a way to catch these, log errors, and return gracefully, in case some messages got severely corrupted. Is there anyway doing that? Why do I see this post indicating some sort of google::protobuf::FatalException but I couldn't find documentations around it (only FatalException probably not enough also).
Thanks!
The failure you are seeing indicates that there is a bug in your program -- the program has requested to serialize a message without having filled in all the required fields first. Think of this like a segmentation fault. You shouldn't try to catch this exception -- you should instead fix your app so that the exception never happens in the first place.
Note that the check is a DCHECK, meaning it is only checked in debug builds. In your release builds (when NDEBUG is defined), this check will be skipped and the message will be written even though it is not valid. So, you don't have to worry about this crashing your application in production, only while debugging.
(Technically you could catch google::protobuf::FatalException, but the Protobuf code was not originally designed to be exception-safe. Originally, check failures would simply abort the program. It looks like FatalException was added recently, but since the code is not exception-safe, it's likely that you'll have memory leaks any time a FatalException is thrown. So, you probably should treat it like an abort().)
I solved that
my problem was same you.
if another thread change size of proto item while you do serialization then FatalException throw
then at first I copy that in another proto item then I do serialize that.
ProtoInput item; // it is global object
.
.
.
fstream output("myfile",
ios::out | ios::trunc | ios::binary);
ProtoInput in;
in.CopyFrom(item);
size_t size = in.ByteSizeLong();
void *buffer = malloc(size);
if (in.SerializeToArray(buffer, size) == true) {
output.write((char *) buffer, size);
}
output.close();
free(buffer);
Related
I am using POCO 1.6.0. I am trying to write a service that receives a JSON message on a raw socket and parses it.
However, the only way that POCO's parser seems to work is to take an entire string as input, and either return the parsed result, or throw a "Syntax error" exception.
So this means I have to re-parse the whole message each time a new byte arrives on the socket; and also there is no way of distinguishing between an actual syntax error versus it just being an incomplete message so far.
The parseChar function looks nice but it is private. Is there any way to have the parser parse some of a message and remain in that state so that I can resume parsing by passing more data?
Also, is there any way to distinguish actual syntax errors from incomplete messages (and preferably get feedback about the exact nature of the syntax error).
Pseudocode:
Poco::JSON::Parser parser;
std::string input_buffer;
for(;;)
{
// (append byte(s) from socket into input_buffer)
// (return failure if this read times out after 5 seconds)
parser.reset();
try
{
parser.parse(input_buffer);
break;
}
catch(Poco::Exception &e)
{
// (abort, but we don't know if data incomplete or data malformed
}
}
Note: I realize that this problem could be mooted by having the client frame the entire message as described in this thread, however I was hoping to make things as simple as possible for the client by just having a correctly-formed packet be sufficient to define a frame (method 5 of that question).
There currently is no way to do either of the things you'd like to do. However, they are both reasonable requests and doable, so this was put on the TODO list for one of the upcoming releases.
im having issues with MySQL++ and desperately need help.
I'm using Visual Studio 2010, MySQL++ v3.1.0 and MySQL v5.1.59( x86 & x64 );
All Library's have been compiled correctly. This error only occurs in Debug version due to the compiler setting "Both (/RTC1, equiv. to /RTCsu) (/RTC1)" being on.
Edit: I should note that this only happens in Debug version. In Release it works like a charm
I've tracked the problem back to the mysqlpp_d.dll, the MySQL++ object are crashing on there destructors due to reference counting. It complains about not being capable of accessing the memory of the ref counter, and when it tries to decrease it, it crashes. At least thats what I think happens.
I tried this to make sure everything gets derefrenced and removed in the correct order (even tho its irrelevant, but helped me track the true problem down I hope): http://pastebin.com/Ru0uYcy9
It crashes with:
First-chance exception at 0x000007feeef5dd4c (mysqlpp_d.dll) in Launcher.exe: 0xC0000005: Access violation writing location 0x000007feeeff5148.
Unhandled exception at 0x000007feeef5dd4c (mysqlpp_d.dll) in Launcher.exe: 0xC0000005: Access violation writing location 0x000007feeeff5148.
And breakes here:
http://pastebin.com/9Mfr7NwB
This code has a serious bug:
mysqlpp::UseQueryResult res;
{
mysqlpp::Query query = conn.query();
query << "SELECT USER();";
res = query.use();
row = res.fetch_row();
}
You aren't consuming all the result sets. In MySQL, stored procedures that return data return at least two separate result sets: the first is the results you asked for, and the second is status information about the call itself. See examples/multiquery.cpp in the MySQL++ source distribution for the correct way to handle this. Also see section 3.16 in the MySQL++ user manual.
The main consequence of this is that later queries on the same connection will fail.
I think your memory corruption is actually a secondary effect, and that the primary problem stems from ignoring the MySQL C API's attempts to tell you that you're trying to run two overlapping queries on the same connection, because you didn't consume the entire first result set. From what little code you've posted, I can see that you're ignoring returned error codes, so if you've also disabled MySQL++ exceptions, your code will completely ignore this error and blithely go on to stomp all over things it shouldn't.
By the way, please lose the trailing semicolon on the query. It isn't needed with the C API, and can cause confusion, especially in the face of multi-queries. Use semicolons only to separate multiple statements in a single query.
I want to program a daemon-manager that takes care that all daemons are running, like so (simplified pseudocode):
void watchMe(filename)
{
while (true)
{
system(filename); //freezes as long as filename runs
//oh, filename must be crashed. Nevermind, will be restarted
}
}
int main()
{
_beginThread(watchMe, "foo.exe");
_beginThread(watchMe, "bar.exe");
}
This part is already working - but now I am facing the problem that when an observed application - say foo.exe - crashes, the corresponding system-call freezes until I confirm this beautiful message box:
This makes the daemon useless.
What I think might be a solution is to make the main() of the observed programs (which I control) "uncrashable" so they are shutting down gracefully without showing this ugly message box.
Like so:
try
{
char *p = NULL;
*p = 123; //nice null pointer exception
}
catch (...)
{
cout << "Caught Exception. Terminating gracefully" << endl;
return 0;
}
But this doesn't work as it still produces this error message:
("Untreated exception ... Write access violation ...")
I've tried SetUnhandledExceptionFilter and all other stuff, but without effect.
Any help would be highly appreciated.
Greets
This seems more like a SEH exception than a C++ exception, and needs to be handled differently, try the following code:
__try
{
char *p = NULL;
*p = 123; //nice null pointer exception
}
__except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ?
EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
{
cout << "Caught Exception. Terminating gracefully" << endl;
return 0;
}
But thats a remedy and not a cure, you might have better luck running the processes within a sandbox.
You can change the /EHsc to /EHa flag in your compiler command line (Properties/ C/C++ / Code Generation/ Enable C++ exceptions).
See this for a similar question on SO.
You can run the watched process a-synchronously, and use kernel objects to communicate with it. For instance, you can:
Create a named event.
Start the target process.
Wait on the created event
In the target process, when the crash is encountered, open the named event, and set it.
This way, your monitor will continue to run as soon as the crash is encountered in the watched process, even if the watched process has not ended yet.
BTW, you might be able to control the appearance of the first error message using drwtsn32 (or whatever is used in Win7), and I'm not sure, but the second error message might only appear in debug builds. Building in release mode might make it easier for you, though the most important thing, IMHO, is solving the cause of the crashes in the first place - which will be easier in debug builds.
I did this a long time ago (in the 90s, on NT4). I don't expect the principles to have changed.
The basic approach is once you have started the process to inject a DLL that duplicates the functionality of UnhandledExceptionFilter() from KERNEL32.DLL. Rummaging around my old code, I see that I patched GetProcAddress, LoadLibraryA, LoadLibraryW, LoadLibraryExA, LoadLibraryExW and UnhandledExceptionFilter.
The hooking of the LoadLibrary* functions dealt with making sure the patching was present for all modules. The revised GetProcAddress had provide addresses of the patched versions of the functions rather than the KERNEL32.DLL versions.
And, of course, the UnhandledExceptionFilter() replacement does what you want. For example, start a just in time debugger to take a process dump (core dumps are implemented in user mode on NT and successors) and then kill the process.
My implementation had the patched functions implemented with __declspec(naked), and dealt with all the registered by hand because the compiler can destroy the contents of some registers that callers from assembly might not expect to be destroyed.
Of course there was a bunch more detail, but that is the essential outline.
I use boost's binary serialization and it worked well until now. I have std::list of pointers to serialize for output (oarchive) but serialization fails inside object's serialize() function with MSVC's dialog:
R6010 -abort() has been called
and such string is printed into console window:
Assertion failed: 0 == static_cast<int>(t) || 1 == static_cast<int>(t), file c:\program files\boost\boost_1_44\boost\archive\basic_binary_oprimitive.hpp, line 91
what does it mean?
Project is pretty big, sources are distributed so I cannot post it's code here, but I tried to simulate this error within simple project - there it works fine what is strange.
P.S. I use boost 1.44 with MSVC2010EE on Windows XP. When I click "retry" on "Debug Error!" window debugger shows arrow on the code line next to serialization archive << myList; line - I mean it seems like error occurred at some destructor or something.
When I make changes inside objects serialize() function - they will be applied just when I rebuild whole project (clean before compiling) - but if I just compile it (where IDE shows that all sources which include changed header are recompiled) - no changes will happen at runtime since last version (I tried with printf()) - that's strange.
Could I occasionally set some critical definitions or something?
The line in question says:
// trap usage of invalid uninitialized boolean which would
// otherwise crash on load.
It looks like at some point you are trying to serialize a bool that hasn't been initialized. Without further code we can't help you find which one.
I am having a strange problem in my code. I have many asserts scattered around the code and all have been working fine. Whenever an assert failed I got a message giving me line number of where the failure happened.
Today I wrote another assert in a function which loads a file. Just wanted to make sure that the fie existed. A very simple assert. Here is the relevant code:
//Check that the file exists and can be opened
FILE* f = fopen(filename, "rb");
#ifdef ASSERTIONS_ON
assert(f!=NULL);//#problem For some reason while all other asserts work, this one just crashes the program without reporting line
#else
if(f == NULL)
return MODEL_LOAD_FILENOTFOUND;
#endif
fclose(f);
I know that this does not help a lot but just wanted to showcase what my problem is. My OS is Windows 7. The compiler is GCC. The error message I get from Windows is the usual runtime error but without line reporting:
"The application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information"
What could be the problem? What can possibly cause an assert failure to just request termination without reporting a line where it happens, while in every other case in the same code it works as intended? Thanks in advance for any assistance!
You most likely have FUBAR'ed the stack somewhere before the assert executes.