“malloc():内存损坏”

时间:2011-01-14 03:25:02

标签: java c++ java-native-interface malloc glibc

我的jni代码中有一个带有malloc的问题。该代码旨在让我访问java中的exiv2库。我试过使用sun和openjdk 1.6.0 vms以及1.7.0(beta)vm来破坏代码。

错误是:

    *** glibc detected *** /usr/lib/jvm/java-6-sun/bin/java: malloc(): memory corruption: 0x00000000418a48f0 ***
======= Backtrace: =========
/lib/libc.so.6(+0x774b6)[0x7f84b8aef4b6]
/lib/libc.so.6(+0x7b55f)[0x7f84b8af355f]
/lib/libc.so.6(__libc_malloc+0x6e)[0x7f84b8af438e]
/usr/lib/libstdc++.so.6(_Znwm+0x1d)[0x7f8469fc3ded]
/home/hjed/libExiv2LJB-C__.so(_ZN9__gnu_cxx13new_allocatorIPN5Exiv25ImageEE8allocateEmPKv+0x49)[0x7f846a601c83]
/home/hjed/libExiv2LJB-C__.so(_ZNSt12_Vector_baseIPN5Exiv25ImageESaIS2_EE11_M_allocateEm+0x2f)[0x7f846a601ab5]
/home/hjed/libExiv2LJB-C__.so(_ZNSt6vectorIPN5Exiv25ImageESaIS2_EE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPS2_S4_EERKS2_+0x115)[0x7f846a60169f]
/home/hjed/libExiv2LJB-C__.so(_ZNSt6vectorIPN5Exiv25ImageESaIS2_EE6insertEN9__gnu_cxx17__normal_iteratorIPS2_S4_EERKS2_+0xcf)[0x7f846a601349]
/home/hjed/libExiv2LJB-C__.so(_Z13addExiv2ImagePN5Exiv25ImageE+0x2b)[0x7f846a601115]
/home/hjed/libExiv2LJB-C__.so(Java_Exiv2_ImageFactory_ImageFactory_1Open+0x193)[0x7f846a60214b]
[0x7f84b3dbcc88]
======= Memory map: ========
...
Java Result: 134

似乎导致错误的代码是(Nb。此代码不在堆栈跟踪中,但在添加此代码之前未发生错误):

Exiv2::FileIo::AutoPtr io (new Exiv2::FileIo(env->GetStringUTFChars(str,false)));
Exiv2::JpegImage * img  =  ( new Exiv2::JpegImage::JpegImage(io, false));

在添加此代码之前,我使用:

分配img的值
image *  img= Exiv2::ImageFactory::open(env->GetStringUTFChars(str,false)).get();

并没有任何malloc问题(但Exiv2 :: ImageFactory :: open()返回一个autoPtr,我不能使用autoPtr来做我想做的事。)

此代码中出现错误:

jint addExiv2Image(image * i) {
    vec.push_back(i);
    return vec.size();
}

使用gdb运行时,错误发生在其他位置:

gdb backtrace:

#0  0x00007ffff724eba5 in raise (sig=<value optimised out>) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
#1  0x00007ffff72526b0 in abort () at abort.c:92
#2  0x00007ffff728843b in __libc_message (do_abort=<value optimised out>, fmt=<value optimised out>) at ../sysdeps/unix/sysv/linux/libc_fatal.c:189
#3  0x00007ffff72924b6 in malloc_printerr (action=3, str=0x7ffff7362eab "malloc(): memory corruption", ptr=<value optimised out>) at malloc.c:6283
#4  0x00007ffff729655f in _int_malloc (av=0x7fffa4000020, bytes=24) at malloc.c:4396
#5  0x00007ffff729738e in __libc_malloc (bytes=24) at malloc.c:3660
#6  0x00007ffff6cadbe7 in os::malloc(unsigned long) () from /usr/lib/jvm/java-6-openjdk/jre/lib/amd64/server/libjvm.so
#7  0x00007ffff68f6bb1 in CHeapObj::operator new(unsigned long) () from /usr/lib/jvm/java-6-openjdk/jre/lib/amd64/server/libjvm.so
#8  0x00007ffff6cef347 in PlaceholderTable::find_and_add(int, unsigned int, symbolHandle, Handle, PlaceholderTable::classloadAction, symbolHandle, Thread*) ()
   from /usr/lib/jvm/java-6-openjdk/jre/lib/amd64/server/libjvm.so
#9  0x00007ffff6d89a6c in SystemDictionary::resolve_instance_class_or_null(symbolHandle, Handle, Handle, Thread*) ()
   from /usr/lib/jvm/java-6-openjdk/jre/lib/amd64/server/libjvm.so
#10 0x00007ffff6d8a243 in SystemDictionary::resolve_or_fail(symbolHandle, Handle, Handle, bool, Thread*) () from /usr/lib/jvm/java-6-openjdk/jre/lib/amd64/server/libjvm.so
#11 0x00007ffff6b4d537 in find_class_from_class_loader(JNIEnv_*, symbolHandle, unsigned char, Handle, Handle, unsigned char, Thread*) ()
   from /usr/lib/jvm/java-6-openjdk/jre/lib/amd64/server/libjvm.so
#12 0x00007ffff6b1fc39 in jni_FindClass () from /usr/lib/jvm/java-6-openjdk/jre/lib/amd64/server/libjvm.so
#13 0x00007fffa36f32cb in JNIEnv_::FindClass (this=0x6131c8, name=0x7fffa36f3628 "Exiv2/Exiv2Image") at /usr/lib/jvm/default-java/include/jni.h:794
#14 0x00007fffa36f3161 in Java_Exiv2_ImageFactory_ImageFactory_1Open (env=0x6131c8, cls=0x7ffff7fd6938, str=0x7ffff7fd6948) at src/ImageFactory.cpp:20
#15 0x00007ffff21d9cc8 in ?? ()
#16 0x0000000000613000 in ?? ()
#17 0x00007ffff7fd68e8 in ?? ()
#18 0x00007fffaa104998 in ?? ()
#19 0x00007ffff7fd6948 in ?? ()
#20 0x00007fffaa104d30 in ?? ()
#21 0x0000000000000000 in ?? ()
运行w / gdb时

和vm崩溃报告回溯:

/lib/libc.so.6(+0x774b6)[0x7ffff72924b6]
/lib/libc.so.6(+0x7b55f)[0x7ffff729655f]
/lib/libc.so.6(__libc_malloc+0x6e)[0x7ffff729738e]
/usr/lib/jvm/java-6-openjdk/jre/lib/amd64/server/libjvm.so(+0x5cfbe7)[0x7ffff6cadbe7]
/usr/lib/jvm/java-6-openjdk/jre/lib/amd64/server/libjvm.so(+0x218bb1)[0x7ffff68f6bb1]
/usr/lib/jvm/java-6-openjdk/jre/lib/amd64/server/libjvm.so(+0x611347)[0x7ffff6cef347]
/usr/lib/jvm/java-6-openjdk/jre/lib/amd64/server/libjvm.so(+0x6aba6c)[0x7ffff6d89a6c]
/usr/lib/jvm/java-6-openjdk/jre/lib/amd64/server/libjvm.so(+0x6ac243)[0x7ffff6d8a243]
/usr/lib/jvm/java-6-openjdk/jre/lib/amd64/server/libjvm.so(+0x46f537)[0x7ffff6b4d537]
/usr/lib/jvm/java-6-openjdk/jre/lib/amd64/server/libjvm.so(+0x441c39)[0x7ffff6b1fc39]
/home/hjed/libExiv2LJB-C__.so(_ZN7JNIEnv_9FindClassEPKc+0x2b)[0x7fffa36f32cb]
/home/hjed/libExiv2LJB-C__.so(Java_Exiv2_ImageFactory_ImageFactory_1Open+0x1a9)[0x7fffa36f3161]
[0x7ffff21d9cc8]

感谢您的帮助,
HJED

修改:将vec从数组更改为向量,如评论中所示 铌。我在任何代码中都不使用malloc。

更新
我尝试使用valgrind进行调试,但使用:

valgrind --show-emwarns=yes --smc-check=all /usr/bin/java -jar Exiv2LJB-test.jar

程序运行但没有提供额外信息并使用

valgrind --show-emwarns=yes --smc-check=all --trace-children=yes /usr/bin/java -jar Exiv2LJB-test.jar

我收到有关SSE2不受支持的虚假错误。我已经阅读了关于将它与java一起使用的valgrind常见问题,我确实遵循了这些说明。

3 个答案:

答案 0 :(得分:1)

std :: auto_ptr是作用域指针包装器,它在离开作用域后销毁包装对象。如果要将返回的对象保留在向量中并手动管理其生命周期,请更改此:

image *  img= Exiv2::ImageFactory::open(env->GetStringUTFChars(str,false)).get();

image *  img= Exiv2::ImageFactory::open(env->GetStringUTFChars(str,false)).release();

然后不要忘记在不再需要时在矢量中释放对象。

另外,如果您可以使用C ++ 11,请将std :: vector与std :: unique_ptr结合使用,他们将为您管理内存:

std::vector<std::unique_ptr<image>> vec;

jint addExiv2Image(image * i) {
    vec.push_back(std::unique_ptr<image>(i));
    return vec.size();
}

答案 1 :(得分:0)

这意味着内存已损坏。 通常与您引用的代码无关 - 它早就发生了。只是你的代码碰巧在同一个内存上运行并发现了腐败。在gdb中运行会给出不同的跟踪进一步证明这一点。

您可以尝试valgrind。阅读FAQ,您需要一些特殊的java。

答案 2 :(得分:0)

在此电话会议中:

Exiv2::ImageFactory::open(env->GetStringUTFChars(str,false)).get();

确保打开操作成功,并且您有一个指向有效状态的图像对象的指针。

此外,在向量插入操作中,确保作为参数传递的指针指向有效状态的现有对象(例如,您确定对象在范围内吗?)。

我怀疑加载图片时内存出现问题(而不是插入本身 - 你正确地初始化你的向量,不是吗?)。

附加提示:对类使用大写起始名称(例如:Image而不是image),因为这会提高代码的可读性。