3

I have successfully managed to install MathGL on Mac OS and managed to get the mglGraph to generate me the sample graphs. Yet when I try to use mglData to handle my data, it crashes with an "EXC_BAD_ACCESS" error. Why? Is there anything I need to do I haven't done?

I installed mathgl with cmake, make and make install (using sudo to be on the safe side). Then I added the /usr/local/include and /usr/local/lib paths to the project and added the libmgl.dylib and libmgl-wnd.dylib references to the project. Is there anything I've missed out? I could not find anybody else with the error (well, I found many bad access errors, but none caused by mglData). The error pops up as soon as I'm trying to construct a mglData instance, so any of this triggers the exception:


mglData *data = new mglData();
mglData data;
etc...

EDIT

So, here's the bare bone of my code that compiles but crashes:


#include iostream
#include mgl2/mgl.h

int main(int argc, const char * argv[])
{
    mglData y;   
    return 0;
}

The thread's call stack when it crashes is:

Thread call stack

And the values of all variables are this:

Variable values


Semi-Solved Got it running

After having been able to compile my project with g++, I tried changing the build settings and who would have believed it, I managed to get it running! All I did was change the C++ Standard Library to libstdc++ (GNU C++ standard library). Now I'm not using the default, which is to use libc++ (LLVM C++ standard library with C++ 11 support). At the moment this seems to work fine for my Cocoa application also, but I don't know for how long :P (When using more NS* Classes I worry LLVM may be necessary?)

Still I'm very interested in how this issue can be resolved (without having to change the build settings) or whether it can't.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Max Z.
  • 801
  • 1
  • 9
  • 25
  • I don't have an answer but, if you show the stack symbols at the time of the crash, it might help someone come up with a reason. – Phillip Mills Nov 06 '12 at 12:55
  • Since you have the source code couldn't you use your debugger to find what the problem is? – john Nov 06 '12 at 13:00
  • @john: I tried debugging, but I can only access the library's header code since it is a precompiled library. And when I did step through the program, I reached a line saying: `0x7fff92849e8f: movq -24(%rax), %rdx` which is no help at all (and already highlighted as the cause of the exception by XCode) :( – Max Z. Nov 06 '12 at 13:11
  • @MaxZ. OK sorry. I thought when you said you'd installed with make and make install you must have the source. – john Nov 06 '12 at 13:12
  • @john I do have the code for mathGL, yes, but I do not have the code for `clear()` in `std::string:clear()`. Apparently that's the library where the error occurs. – Max Z. Nov 06 '12 at 13:14
  • @MaxZ. I think you can reasonably assume that `std::string::clear()` is bug free. The issue would seem to be heap corruption that has occurred before `std::string::clear()` was called. – john Nov 06 '12 at 13:16
  • haha, once again I had the exact same problem, but this answer works brilliantly! I'm so surprised there isn't a more stable OSX version. – Sam Palmer Apr 15 '13 at 10:20

1 Answers1

0

I'm working with mathgl 2.1.2 and ran into the same problem. There's a mismatch between the run-time libraries. Mathgl uses compiler default but when creating a new commas-libe project it uses the llvm. Since the string is allocated in your executable via LLVM, but then passed to Mathgl which used a different run-time library. Memory operations can fail when a pointer allocated in one library is passed to another to be freed. There could be structure differences as well.

enb081
  • 3,831
  • 11
  • 43
  • 66