저는 C++을 처음 접했고 약간의 어려움을 겪고 있습니다. C- 문자열을 감싸는 String 클래스를 만들려고합니다.C++에서 메모리 누수가 발견되지 않음
얼마 후 나는 힙에 메모리 누수가 있다는 것을 알았지 만 실수를 발견하지 못했습니다.
#include <iostream>
#include <memory>
using namespace std;
class String final {
size_t m_len;
size_t m_start;
shared_ptr<char> m_string;
public:
String(); // Standard Constr
String(const String& s); // Copy Constr
String(const char* s); // Typconvert Constr
// Deconstr
~String();
}
이는 통화 당에서 생성자은 다음과 같습니다 :
이
내 헤더입니다#include "stdafx.h"
#include "MyString.h"
#include <iostream>
using namespace std;
String::String() : m_len(0), m_start(0), m_string(nullptr)
{
}
String::String(const String &obj) : m_len(obj.m_len), m_start(obj.m_start), m_string(obj.m_string)
{
}
String::String(const char* obj) : m_len(strlen(obj))
{
shared_ptr<char> ptr(unique_ptr<char[]>(new char[m_len]));
m_string = ptr;
for (size_t i = 0; i <= m_len; i++) {
*(m_string.get() + i*sizeof(char)) = obj[i];
if (i == m_len)
{
*(m_string.get() + i*sizeof(char)) = '\0';
}
}
}
String::~String()
{
m_string.reset();
}
가 그리고 이것은 내 주 :
#include "stdafx.h"
#include "MyString.h"
#include <iostream>
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
using namespace std;
int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
String s0;
String s1("");
String s2("abc");
String s3("ab");
String s00(s0);
String s11(s1);
String s22(s2);
_CrtDumpMemoryLeaks();
return 0;
}
지금 나는이 프로그램을 실행할 때 오류가 발생합니다.
Detected memory leaks!
Dumping objects ->
{66} normal block at 0x00668458, 16 bytes long.
Data: <X f > 58 AB 0E 01 01 00 00 00 01 00 00 00 D8 8D 66 00
{65} normal block at 0x00668DD8, 2 bytes long.
Data: <ab> 61 62
{64} normal block at 0x00668D98, 16 bytes long.
Data: <X f > 58 AB 0E 01 02 00 00 00 01 00 00 00 F8 91 66 00
{63} normal block at 0x006691F8, 3 bytes long.
Data: <abc> 61 62 63
{62} normal block at 0x00668C60, 16 bytes long.
Data: <X f > 58 AB 0E 01 02 00 00 00 01 00 00 00 D8 8F 66 00
{61} normal block at 0x00668FD8, 0 bytes long.
Data: <> öß³‘@þÿÿÿ÷O
Object dump complete.
Debug Error!
HEAP CORRUPTION DETECTED: after Normal block (#65) at 0x00668DD8.
CRT detected that the application wrote to memory after end of heap buffer.
어디에서 오는 것인지 알 수 없습니다. 나는 deconstructor에 대해 생각하고 꺼내려고 노력했기 때문에 나를 위해 하나를 쓸 것이지만 그것은 작동하지 않았다. 또한 프로그램을 중단시키는 코드 블록은 다음과 같습니다.
void* __CRTDECL operator new(size_t const size)
{
for (;;)
{
if (void* const block = malloc(size))
{
return block;
}
if (_callnewh(size) == 0)
{
if (size == SIZE_MAX)
{
__scrt_throw_std_bad_array_new_length();
}
else
{
__scrt_throw_std_bad_alloc();
}
}
// The new handler was successful; try to allocate again...
}
}
from new_scalar.cpp.
도움을 주셔서 감사합니다.
는
이 모든 것을 관리하는'std :: string'이 있다는 것을 알고 계셨습니까? – JVApen
전체 규칙 3 개 (일부 규칙 및 5 규칙, 규칙 0 등)에 대해 일부 회원이 누락되었습니다. 당신은 또한 올바른 범위에 버려지지 않습니다. 메모리는 디버그 덤프가 발생한 후에 만 회수됩니다. http://stackoverflow.com/a/38437543/3747990. s0 이전과 s22 후에 중괄호를 추가하십시오. – Niall
너무 단순한 작업을 위해 너무 복잡한 코드를 본 적이 없습니다! 도대체 클래스에서 포인터를 정의하고 메소드에서 포인터를 다시 정의하고 다른 포인터에 하나를 지정합니다 ... strlen을 사용하지만 strcpy는 사용하지 않습니까? 왜? 바퀴를 재발 명하는 것은 좋은 직업 인 것처럼 보인다. 고유 포인터 주위에 공유 포인터 ... 문자열 메모리의 소유자는 누구입니까? 그 물체? – Klaus