c++ ) 함수에서 오브젝트를 리턴할 때 자동으로 삭제되어 리턴이 되지 않는 문제

poerty의 이미지

linked list를 작성하고 있는데

+=, = operator를 작성하였고 정상적으로 동작하는 것도 확인하였습니다.

그 후 + operator를 작성하는데

unvlist unvlist::operator+(const unvlist &ul){
	NodeBase *tmp, *ttmp, *tttmp;
	unvlist *list = new unvlist();
	*list = *this;
	*list += ul;
	return *list;
}

와 같이 작성하니 이유를 모를 메무리 누수(12byte)
Detected memory leaks!
Dumping objects ->
{216} normal block at 0x0122B298, 12 bytes long.
Data: < 8 " " > 05 00 00 00 38 B3 22 01 D0 B4 22 01
Object dump complete.

가 일어나는데 아마 *list를 함수안에서 생성 한 후 delete하는 과정이 없어서 그런 것 같아

unvlist unvlist::operator+(const unvlist &ul){
	NodeBase *tmp, *ttmp, *tttmp;
	unvlist list = unvlist();
	list = *this;
	list += ul;
	return list;
}

와같이 작성하였는데

unvlist l = unvlist(), ll=unvlist();
	l = *l1;
	l += *l2;
 
	ll = *l1 + *l2;

를 실행하면 l같은 경우 정상적으로 작동하나 ll = *l1 + *l2를 실행하는 순간 operator=에서 오류가 납니다

원인은 *l1 + *l2 가 Size는 정상적으로 들어가지만 나머지 노드들이 하나도 들어가지 않습니다.(NULL)

operator+ 함수 내에서 확인해 보니 리턴 전까지는 정상적으로 동작합니다.

아래는 제 destructor함수입니다

unvlist::~unvlist(){
	clear();
	//	free(this);
}
void unvlist::clear(){
	NodeBase* T = Tail;
	NodeBase* tmp = Head;
	NodeBase* ttmp;
	while (tmp != NULL){
		ttmp = tmp->nextNode;
		delete tmp;
		tmp = ttmp;
	}
	Tail = NULL;
	Size = 0;
}

unvlist class는 다음과 같습니다

class unvlist {
private:
	int Size;
	NodeBase* Head;
	NodeBase* Tail;
public:
	unvlist operator+(const unvlist &ul);
	unvlist& operator=(const unvlist &ul);
	unvlist& operator+=(const unvlist &ul);
	bool operator==(const unvlist &ul);
	bool operator!=(const unvlist &ul);
	....
}

즉 정리하자면

*l1+*l2의 값이 (operator+에서) return 되기 전에 자동으로 delete되어서 정상적으로 리턴이 되지 않습니다.

원인이 무엇일까요?

https://kldp.org/node/132271

저와 비슷한 경우인 것 같아 살펴보았지만 저는 return type을 그냥 type으로 맞게 한 것임에도 오류가 나는 이유를 모르겠어 질문 드립니다.

익명 사용자의 이미지

operator= 코드 혹시 좀 보여 주실 수 있으십니까. 일단 거기부터 의심되는데요.

poerty의 이미지

unvlist& unvlist::operator=(const unvlist &ul){
	NodeBase *tmp = ul.Head, *ttmp = NULL, *tttmp;
	if (tmp != NULL){
		Head = tmp->retThis();
		tmp = tmp->nextNode;
	}
	ttmp = Head;
	while (tmp != NULL){
		tttmp = tmp->retThis();
		ttmp->nextNode = tttmp;
		tttmp->preNode = ttmp;
		ttmp = tttmp;
		tmp = tmp->nextNode;
	}
	Tail = ttmp;
	Size = ul.Size;
	return *this;
}

입니다

다만 operator+함수를 클래스 내부펑션이 아닌 외부로 접근하니 해결되었습니다

unvlist operator+(const unvlist &ul1, const unvlist &ul2){
	unvlist list = unvlist();
	list = ul1;
	list += ul2;
	return list;
}

답변 감사합니다

poerty의 이미지

(clone이라고 하나요?)

poerty의 이미지

operator+를 바꾼 것보다

복사 생성자를 만들어서 해결되었습니다...

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.