[Bug c++/15375] New: destuctor not call from 3rd temporary object in expression

shebeko at mail dot ru gcc-bugzilla@gcc.gnu.org
Wed May 12 08:09:00 GMT 2004


Destructor for temporary object with "str3" never called.

  return iig("str1")&&iig("str2")&&iig("str3");

Originally bug was found on std::string.
I include simple implementation of class string only for bug
illustration.

Source code:

unsigned i;
bool print_time(){return (i%1000000)==0;}

#include <string.h>
#include <stdio.h>


class str_t
{
	char* p;

	void clear(){if(p)delete[] p;}

	void set(const char* _p)
	{
		clear();
		p=new char[strlen(_p)+1];
		strcpy(p,_p);
	}
public:
	str_t() : p(0) {}
  
	str_t(const char* rhs)
	{
		p=0;
		set(rhs);
		if(print_time())printf("str_t(const char*) p=%s\n",p);
	}

	str_t(const str_t& rhs)
	{
		p=0;
		set(rhs.p);
		if(print_time())printf("str_t(const str_t&) p=%s\n",p);
	}

	str_t& operator=(const str_t& rhs)
	{
		if(&rhs!=this)set(rhs.p);
		if(print_time())printf("operator=(const str_t&) p=%s\n",p);
		return *this;
	}
  
	~str_t()
	{
		if(print_time())printf("~str_t() p=%s\n",p);
		clear();
	}

	const char* c_str() const{if(p) return p;else return "";}
};

bool iig(const str_t& val)
{
	if(print_time())printf("iig(%s)\n",val.c_str());
	return true;
}

bool ig()
{
  return iig("str1")&&iig("str2")&&iig("str3");
}

void g()
{
	for(i=0;i<50000000;i++)ig();
}

//---------------------------------------------------------------------

int main(int argc,char *argv[])
{
	g();
	return 0;
}


The result of iteration will be:

str_t(const char*) p=str1
iig(str1)
str_t(const char*) p=str2
iig(str2)
str_t(const char*) p=str3
iig(str3)
~str_t() p=str2
~str_t() p=str1

-- 
           Summary: destuctor not call from 3rd temporary object in
                    expression
           Product: gcc
           Version: 2.95.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: shebeko at mail dot ru
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15375



More information about the Gcc-bugs mailing list