简体   繁体   中英

g++ malloc error when linking with libmx and using std::stringstream

The simplest example is the following program, linked with libmx ( -lmx ) (for MATLAB support) on OS X Yosemite, compiled with g++4.9.1 from macports:

#include <sstream>

int main()
{
    std::ostringstream ostr;
    // ostr << " "; // if I un-comment this line no more malloc error
}

I realized that at ostr destruction (just before exiting the program), I get a malloc runtime error of the form:

testcpp(5362,0x7fff7d2af300) malloc: *** error for object 0x1055b6270: 
pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug

If, on the other hand, I uncomment the second line in main() and write something to the ostringstream , then the program exits happily with no more error. This happens only with g++, clang++ compiles and runs it without any problems. Anyone knows if this is a g++ -related issue or a libmx one?

PS: if I am not using -lmx flag on linking, then there is no more malloc error.

I guess it's better to comment on this separately. As I said, I'm having troubles reproducing your error. I'm on Ubuntu Linux 14.04 64 bits with g++ 4.8.2 and I downloaded the Matlab Compiler Runtime v83 and v84 for Linux 64 bits.

The source I am compiling is the following:

#include <sstream>

int main()
{
    std::ostringstream ostr;
    // ostr << " "; // if I un-comment this line no more malloc error
}

And the following commands run fine (2014 a and b):

g++ -L/opt/MATLAB/MCR/v83/runtime/glnxa64 -Wall malloc_gcc_matlab.cpp -o malloc_gcc_matlab && ./malloc_gcc_matlab
g++ -L/opt/MATLAB/MCR/v84/runtime/glnxa64 -Wall malloc_gcc_matlab.cpp -o malloc_gcc_matlab && ./malloc_gcc_matlab

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM