tbeu / matio

MATLAB MAT File I/O Library
https://matio.sourceforge.io
BSD 2-Clause "Simplified" License
342 stars 96 forks source link

C++ #32

Closed papadop closed 8 years ago

papadop commented 8 years ago

This is another "just for discussion" pull request, even though it is already somewhat complete... In OpenMEEG, we had complains of "strange" error messages and crashes when allocating big matrices. The problem is that OpenMEEG being a C++ program, we rely on exceptions to capture errors and fail gracefully. But matio uses plain malloc and justs exits (sometimes) when a memory allocation failure arises. In this branch, I replaced all memory allocations by macros that expand (or are equivalent to) the C code when compiled with C and to C++ allocation routines when compiling with C++. This is tested to work fine in both C and C++ compilations.... So now, if compiling in C++ mode, allocation failure now result in exceptions emitted.

The drawback is that in C++ mode, no cleanup is made (because the if (var==NULL) blocks are not executed. To deal with that, I need to uglify the code with macros simulating TRY {} CATCH() {}, which will become try {} catch(...) { CLEANUP } blocks in C++ mode and traditional if (var==NULL) {CLEANUP} blocks in C mode.

One nice side effect of this work is that it is easy to identify all allocation points by grepping NEW. And if the TRY {}CATCH {} work is done, then all memory allocation failures will be caught, whereas this seems not to be the case currently.

Among other C++ improvements I might be interested to create are

Of course the first and second points need to be done without breaking the C interface. I have ideas, but I'm not totally sure if this can be done without uglifying the code too much. The last point is fairly easy (at least with the cmake build system I use, which will be the subject of another "just for discussion" pull request I will make once all the test reorganisation stuff will be integrated. With autoconf, this is doable but may need two separate builds (as it works currently, but we need to detect the C++ compiler and change the final library name).

papadop commented 8 years ago

Cannot understand the travis failure. For me the test is skipped (as is the uncompressed case), so I do not understand why the test is not skipped there... Used valgrind to check for memory errors and found nothing.... I can understand that the patch could have introduced a uninitialized memory problem (forgot to say that some calloc where replaced by mallocs and proper memsets to initialize the memory where needed).

Is there a way to access the travis build ???

tbeu commented 8 years ago

One test failed: mat5_compressed_write.at:573 What is exit value, stdout and stderr for command test_mat -v 5 -z write_2d_logical?

Furthermore, the Linux test error and the OSX test errors are not identical.

papadop commented 8 years ago

sipadan-> ./test_mat -v 5 -z write_2d_logical sipadan-> echo $status 0

papadop commented 8 years ago

So no stdout or stderr.

sipadan-> ls -la test_write_2d_logical.mat -rw-r--r-- 1 papadop athena 405 Mar 29 16:16 test_write_2d_logical.mat sipadan-> more test_write_2d_logical.mat MATLAB 5.0 MAT-file, Platform: x86_64-unknown-linux-gnu, Created by: libmatio v1 .5.6 on Tue Mar 29 16:16:30 2016 [...]

papadop commented 8 years ago

The test failure is very very probably due to the missing \n. The other pull request should correct that... I did not made the change in this branch to avoid creating conflicts. The mystery is more why this test is not skipped...

papadop commented 8 years ago

Remember that this PR is more for you to see where I'm going and provide you (and me) a chance of providing comments early. This is WIP. Note that mat4.c is now mostly totally converted to try/catch. So you can see from this branch what the code will look like in the end if this idea is pursued. Some breakage is to be expected for the time being. Will remove them for the actual (real) PR.

agramfort commented 8 years ago

FYI https://github.com/openmeeg/matio-openmeeg/pull/4/files here is working travis setup using cmake for building and testing on mac os + linux

tbeu commented 8 years ago

Matio is a pure C library and not designed as C++ library. See the replicated ReadData function that only differ on the data type and could be very conveniently written using C++ templates. E.g. the msgpack-c project has a true C and a true C++ implementation that not just differ on mallo/free vs. new/delete or file I/O. Therefore I propose to keep Matio as C-only library. And I know there are plenty of malloc's with unchecked return values within Matio that need to be tackled.

agramfort commented 8 years ago

ok fair enough but is there a proper/clean way for us to catch malloc issues from a c++ project?

tbeu commented 8 years ago

Usually malloc errors should be caught within the library. Do you call Mat_LogInit in your application such that allocation errors are reported/dealt with?

landscape-bot commented 8 years ago

Code Health Code quality remained the same when pulling cf50037 on papadop:C++ into ac0565c on tbeu:master.

tbeu commented 8 years ago

Closing...