zhuwt2008 2015-04-02 14:44 采纳率: 0%
浏览 2087
已采纳

高手指教,为什么这个简单的函数会报错呢??

#include
#include
#include
using namespace std;

wstring w2chs3(const char s1) {
size_t len = strlen(s1);
// wchar_t *ws2 = new wchar_t[len];
wchar_t *ws2 = (wchar_t
)malloc(len*sizeof(wchar_t));
unique_ptr wstr(ws2);
mbstowcs(ws2, s1, 100);
return wstring(ws2);
}

int main() {
auto p2 = w2chs3("hello,world!");
wcout<<p2<<endl;

return 0;

}

不用内存检测工具是运行是正确的,但是用valgrind运行报错,内容:
[root@localhost basecheck]# valgrind --leak-check=yes ./wchartest3
==3086== Memcheck, a memory error detector
==3086== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==3086== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==3086== Command: ./wchartest3
==3086==
==3086== Invalid write of size 4
==3086== at 0x5677532: __gconv_transform_ascii_internal (in /usr/lib64/libc-2.17.so)
==3086== by 0x56FCF45: __mbsrtowcs_l (in /usr/lib64/libc-2.17.so)
==3086== by 0x568A4A0: mbstowcs (in /usr/lib64/libc-2.17.so)
==3086== by 0x400CBF: w2chs3(char const*) (wchartest3.cpp:11)
==3086== by 0x400D47: main (wchartest3.cpp:16)
==3086== Address 0x5a12070 is 0 bytes after a block of size 48 alloc'd
==3086== at 0x4C2845D: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==3086== by 0x400C90: w2chs3(char const*) (wchartest3.cpp:9)
==3086== by 0x400D47: main (wchartest3.cpp:16)
==3086==
==3086== Invalid read of size 4
==3086== at 0x56FCF83: __mbsrtowcs_l (in /usr/lib64/libc-2.17.so)
==3086== by 0x568A4A0: mbstowcs (in /usr/lib64/libc-2.17.so)
==3086== by 0x400CBF: w2chs3(char const*) (wchartest3.cpp:11)
==3086== by 0x400D47: main (wchartest3.cpp:16)
==3086== Address 0x5a12070 is 0 bytes after a block of size 48 alloc'd
==3086== at 0x4C2845D: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==3086== by 0x400C90: w2chs3(char const*) (wchartest3.cpp:9)
==3086== by 0x400D47: main (wchartest3.cpp:16)
==3086==
==3086== Invalid read of size 4
==3086== at 0x4C2E404: wcslen (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==3086== by 0x4EF4074: std::basic_string, std::allocator >::basic_string(wchar_t const*, std::allocator const&) (in /usr/lib64/libstdc++.so.6.0.19)
==3086== by 0x400CE2: w2chs3(char const*) (wchartest3.cpp:12)
==3086== by 0x400D47: main (wchartest3.cpp:16)
==3086== Address 0x5a12070 is 0 bytes after a block of size 48 alloc'd
==3086== at 0x4C2845D: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==3086== by 0x400C90: w2chs3(char const*) (wchartest3.cpp:9)
==3086== by 0x400D47: main (wchartest3.cpp:16)
==3086==
==3086== Mismatched free() / delete / delete []
==3086== at 0x4C29E41: operator delete (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==3086== by 0x4010AE: std::default_delete::operator()(wchar_t*) const (unique_ptr.h:99)
==3086== by 0x401002: std::unique_ptr >::~unique_ptr() (unique_ptr.h:377)
==3086== by 0x400CFA: w2chs3(char const*) (wchartest3.cpp:12)
==3086== by 0x400D47: main (wchartest3.cpp:16)
==3086== Address 0x5a12040 is 0 bytes inside a block of size 48 alloc'd
==3086== at 0x4C2845D: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==3086== by 0x400C90: w2chs3(char const*) (wchartest3.cpp:9)
==3086== by 0x400D47: main (wchartest3.cpp:16)
==3086==
hello,world!
==3086==
==3086== HEAP SUMMARY:
==3086== in use at exit: 0 bytes in 0 blocks
==3086== total heap usage: 2 allocs, 2 frees, 124 bytes allocated
==3086==
==3086== All heap blocks were freed -- no leaks are possible
==3086==
==3086== For counts of detected and suppressed errors, rerun with: -v
==3086== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 3 from 3)

  • 写回答

9条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥60 dpabi进行Alff计算时脑池有干扰信号
      • ¥15 利用kmeans或kshape聚类分析对归一化的无量纲时间-降雨序列进行聚类
      • ¥20 misc尽快完成,急! ! !
      • ¥15 protel99.SE提示一下弹窗
      • ¥15 银河麒麟v10 执行.run失败如何解决
      • ¥15 如何用Python打开LA文件
      • ¥15 用mysql做一个高校工资管理系统
      • ¥21 Multisim设计用三端集成稳压器设计稳压电路
      • ¥15 tp6,在iframe里的网址通过获取url参数失败
      • ¥15 打开opendaylight查看拓扑,为什么直接就显示了两个Switch,在mininet配置了一个topo,这两个Switch还在