问题描述
- 高手指教,为什么这个简单的函数会报错呢??
-
#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)
解决方案
#include cstring
#include iostream
#include memory
using namespace std;
wstring w2chs3(const char s1) {
size_t len = strlen(s1);
wchar_t *ws2 = (wchar_t)malloc(len*sizeof(wchar_t));
mbstowcs(ws2, s1, len);
wstring wstr(ws2);
free(ws2);
return wstr;
}
int main() {
auto p2 = w2chs3("hello,world!");
wcout<<p2<<endl;
return 0;
}
改成这样,剩下一个错误:
==2539== Memcheck, a memory error detector
==2539== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==2539== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==2539== Command: ./wchartest3
==2539==
==2539== Invalid read of size 4
==2539== at 0x4C2E404: wcslen (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==2539== by 0x4EF4074: std::basic_string, std::allocator >::basic_string(wchar_t const*, std::allocator const&) (in /usr/lib64/libstdc++.so.6.0.19)
==2539== by 0x400CCE: w2chs3(char const*) (wchartest3.cpp:10)
==2539== by 0x400D27: main (wchartest3.cpp:16)
==2539== Address 0x5a12070 is 0 bytes after a block of size 48 alloc'd
==2539== at 0x4C2845D: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==2539== by 0x400C90: w2chs3(char const*) (wchartest3.cpp:8)
==2539== by 0x400D27: main (wchartest3.cpp:16)
==2539==
hello,world!
==2539==
==2539== HEAP SUMMARY:
==2539== in use at exit: 0 bytes in 0 blocks
==2539== total heap usage: 2 allocs, 2 frees, 124 bytes allocated
==2539==
==2539== All heap blocks were freed -- no leaks are possible
==2539==
==2539== For counts of detected and suppressed errors, rerun with: -v
==2539== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 3 from 3)
解决方案二:
上面头文件没有了,补充::
#include <cstring>
#include <iostream>
#include <memory>
解决方案三:
wstring w2chs3(const char s1) ?你确定你没写错这里么?
解决方案四:
malloc 得到的内存,没有自己释放吧,所以你用内存检测工具会报错!
解决方案五:
奇怪,为什么wstring w2chs3(const char s1) {这一行中的星号*也没有了,本来是有的,改正一下
wstring w2chs3(const char *s1) {
解决方案六:
malloc的ws2没有对应的free,造成内存泄露。
解决方案七:
我将程序改成这样,用new,智能指针应该会正确调用delete[],但还是报错:
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, len);
return wstring(ws2);
}
int main() {
auto p2 = w2chs3("hello,world!");
wcout<<p2<<endl;
return 0;
}
==2402== Memcheck, a memory error detector
==2402== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==2402== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==2402== Command: ./wchartest3
==2402==
==2402== Invalid read of size 4
==2402== at 0x4C2E404: wcslen (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==2402== by 0x4EF4074: std::basic_string, std::allocator >::basic_string(wchar_t const*, std::allocator const&) (in /usr/lib64/libstdc++.so.6.0.19)
==2402== by 0x400CF9: w2chs3(char const*) (wchartest3.cpp:12)
==2402== by 0x400D60: main (wchartest3.cpp:16)
==2402== Address 0x5a12070 is 0 bytes after a block of size 48 alloc'd
==2402== at 0x4C2900A: operator new (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==2402== by 0x400CA8: w2chs3(char const*) (wchartest3.cpp:8)
==2402== by 0x400D60: main (wchartest3.cpp:16)
==2402==
hello,world!
==2402==
==2402== HEAP SUMMARY:
==2402== in use at exit: 0 bytes in 0 blocks
==2402== total heap usage: 2 allocs, 2 frees, 124 bytes allocated
==2402==
==2402== All heap blocks were freed -- no leaks are possible
==2402==
==2402== For counts of detected and suppressed errors, rerun with: -v
==2402== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 3 from 3)
解决方案八:
我将程序改成这样,用new,智能指针应该会正确调用delete[],但还是报错:
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, len);
return wstring(ws2);
}
int main() {
auto p2 = w2chs3("hello,world!");
wcout<<p2<<endl;
return 0;
}
==2402== Memcheck, a memory error detector
==2402== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==2402== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==2402== Command: ./wchartest3
==2402==
==2402== Invalid read of size 4
==2402== at 0x4C2E404: wcslen (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==2402== by 0x4EF4074: std::basic_string, std::allocator >::basic_string(wchar_t const*, std::allocator const&) (in /usr/lib64/libstdc++.so.6.0.19)
==2402== by 0x400CF9: w2chs3(char const*) (wchartest3.cpp:12)
==2402== by 0x400D60: main (wchartest3.cpp:16)
==2402== Address 0x5a12070 is 0 bytes after a block of size 48 alloc'd
==2402== at 0x4C2900A: operator new (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==2402== by 0x400CA8: w2chs3(char const*) (wchartest3.cpp:8)
==2402== by 0x400D60: main (wchartest3.cpp:16)
==2402==
hello,world!
==2402==
==2402== HEAP SUMMARY:
==2402== in use at exit: 0 bytes in 0 blocks
==2402== total heap usage: 2 allocs, 2 frees, 124 bytes allocated
==2402==
==2402== All heap blocks were freed -- no leaks are possible
==2402==
==2402== For counts of detected and suppressed errors, rerun with: -v
==2402== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 3 from 3)
解决方案九:
我改成这样也不行
using namespace std;
wstring w2chs3(const char s1) {
size_t len = strlen(s1);
wchar_t *ws2 = (wchar_t)malloc(len*sizeof(wchar_t));
mbstowcs(ws2, s1, len);
free(ws2);
return wstring(ws2);
}
int main() {
auto p2 = w2chs3("hello,world!");
wcout<<p2<<endl;
return 0;
}
解决方案十:
#include
#include
#include
#include
using namespace std;
wstring w2chs3(const char* s1) {
size_t len = strlen(s1);
wchar_t ws2 = (wchar_t)malloc(len*sizeof(wchar_t));
auto_ptr wstr(ws2);
mbstowcs(ws2, s1, 100);
return wstring(ws2);
}
int main() {
wstring p2 = w2chs3("hello,world!");
wcout<<p2<<endl;
return 0;
}
你看看吧 贴出来的代码 都编译不过让别人怎么帮你看问题。如果还报错,应该是linux下malloc的内存不能用delete释放,你不要用智能指针了,老老实实的用freee吧。