我使用ngrams实现了一个语言检测器,到目前为止一切正常。为了检测一堆语言,我为探测器在实际检测开始之前需要读入的每种支持语言都有一套与语言相关的ngrams文件。
为了读取这些文件,我设置系统默认语言环境(在我的ubuntu机器上是en_US.UTF-8),就像这样。这些代码段位于我的language_identifier
构造函数中:
std::locale def_lc(""); // --- line 37 (see valgrind)
const utf8_codecvt_t &utf8_codecvt = std::use_facet<utf8_codecvt_t>(def_lc);
std::locale utf8_locale(def_lc, &utf8_codecvt);
在打开流之前,我通过imbue设置了语言环境:
wifstream is;
is.open(filename.c_str(), ios::in);
is.imbue(utf8_locale);
if (is.is_open()) {
// ...
}
执行我的探测器,valgrind给我以下输出:
==21669== Memcheck, a memory error detector
==21669== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==21669== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==21669== Command: ./language_identifier
==21669==
==21669== Invalid read of size 8
==21669== at 0x56E5E08: wcscmp (wcscmp.S:479)
==21669== by 0x4EA2113: std::moneypunct<wchar_t, false>::~moneypunct() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==21669== by 0x4EA2198: std::moneypunct<wchar_t, false>::~moneypunct() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==21669== by 0x4E96A79: std::locale::_Impl::~_Impl() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==21669== by 0x4E96C4C: std::locale::~locale() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==21669== by 0x40429E: language_identifier::language_identifier() (language_identifier.cpp:137)
==21669== by 0x409802: Singleton<language_identifier>::instance() (Singleton.h:29)
==21669== by 0x4050C1: main (language_identifier.cpp:270)
==21669== Address 0x5a07248 is 0 bytes after a block of size 8 alloc'd
==21669== at 0x4C2AC27: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==21669== by 0x4EA1DED: std::moneypunct<wchar_t, false>::_M_initialize_moneypunct(__locale_struct*, char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==21669== by 0x4E9911E: std::locale::_Impl::_Impl(char const*, unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==21669== by 0x4E9965E: std::locale::locale(char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==21669== by 0x403C36: language_identifier::language_identifier() (language_identifier.cpp:37)
==21669== by 0x409802: Singleton<language_identifier>::instance() (Singleton.h:29)
==21669== by 0x4050C1: main (language_identifier.cpp:270)
==21669==
==21669== Invalid read of size 8
==21669== at 0x56E5E08: wcscmp (wcscmp.S:479)
==21669== by 0x4EA2003: std::moneypunct<wchar_t, true>::~moneypunct() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==21669== by 0x4EA2088: std::moneypunct<wchar_t, true>::~moneypunct() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==21669== by 0x4E96A79: std::locale::_Impl::~_Impl() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==21669== by 0x4E96C4C: std::locale::~locale() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==21669== by 0x40429E: language_identifier::language_identifier() (language_identifier.cpp:137)
==21669== by 0x409802: Singleton<language_identifier>::instance() (Singleton.h:29)
==21669== by 0x4050C1: main (language_identifier.cpp:270)
==21669== Address 0x5a07478 is 0 bytes after a block of size 8 alloc'd
==21669== at 0x4C2AC27: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==21669== by 0x4EA17FD: std::moneypunct<wchar_t, true>::_M_initialize_moneypunct(__locale_struct*, char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==21669== by 0x4E9916B: std::locale::_Impl::_Impl(char const*, unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==21669== by 0x4E9965E: std::locale::locale(char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==21669== by 0x403C36: language_identifier::language_identifier() (language_identifier.cpp:37)
==21669== by 0x409802: Singleton<language_identifier>::instance() (Singleton.h:29)
==21669== by 0x4050C1: main (language_identifier.cpp:270)
==21669==
--- lang is en
--- lang is zh
--- lang is de
--- lang is ja
--- lang is ja
--- lang is zh
--- lang is zh
--- T1: lang is de
--- T2: lang is de
==21669==
==21669== HEAP SUMMARY:
==21669== in use at exit: 0 bytes in 0 blocks
==21669== total heap usage: 366,286 allocs, 366,286 frees, 17,016,689 bytes allocated
==21669==
==21669== All heap blocks were freed -- no leaks are possible
==21669==
==21669== For counts of detected and suppressed errors, rerun with: -v
==21669== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 2 from 2)
我无法完全记住并且没有机会检查,但在我的ubuntu 10.04 LTS机器上我没有上面的valgrind错误,但我可能会弄错。
我也可以用这个简单的程序重现这个:
#include <iostream>
#include <locale>
int main (int argc, char **argv) {
try {
std::locale * l1 = new std::locale("de_DE.UTF-8");
delete l1;
std::locale l2("de_DE.UTF-8");
} catch(...) {
return 0;
}
return 0;
};
有人知道这里发生了什么吗?我想念一下吗?
代码基于ubuntu 12.04LTS构建,使用gcc版本4.6.3(Ubuntu / Linaro 4.6.3-1ubuntu5)
$ /lib/x86_64-linux-gnu/libc.so.6
GNU C Library (Ubuntu EGLIBC 2.15-0ubuntu10.3) stable release version 2.15, by Roland McGrath et al.
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 4.6.3.
Compiled on a Linux 3.2.30 system on 2012-10-05.
Available extensions:
crypt add-on version 2.1 by Michael Glad and others
GNU Libidn by Simon Josefsson
Native POSIX Threads Library by Ulrich Drepper et al
BIND-8.2.3-T5B
libc ABIs: UNIQUE IFUNC
感谢您的提示!