使用libmx链接并使用std::stringstream时发生g++malloc错误

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

本文关键字:stringstream 错误 g++malloc std 链接 libmx 使用      更新时间:2023-10-16

最简单的例子是下面的程序,链接到OS X Yosemite上的libmx(-lmx)(用于MATLAB支持),使用g++4.9.1从macports:编译

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

我意识到,在ostr销毁时(就在退出程序之前),我得到了一个malloc运行时错误,格式为:

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

另一方面,如果我取消对main()中的第二行的注释,并向ostringstream写入一些内容,那么程序将愉快地退出,不再出现错误。这种情况只发生在g++中,clang++编译并运行它没有任何问题。有人知道这是与g++相关的问题还是与libmx相关的问题吗?

PS:如果我在链接时没有使用-lmx标志,那么就不会再有malloc错误了。

我想最好单独评论一下。正如我所说,我很难重现你的错误。我使用的是带有g++4.8.2的Ubuntu Linux 14.04 64位,我下载了Linux 64位的Matlab编译器运行时v83和v84。

我正在编译的源代码如下:

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

以下命令运行良好(2014 a和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