www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Memory error when using a template in a class destructor

reply "JC" <mx4399 gmail.com> writes:
The following code throws a 
core.exception.InvalidMemoryOperationError (0), anyone have an 
idea of what is causing it?

---

import std.stdio;

class Test
{
	this()
	{
		struct Start
		{
			string filename;
		}
		Message!(Start)(Start("one.txt"));
	}

	~this()
	{
		struct Stop
		{
			string filename;
		}
		Message!(Stop)(Stop("one.txt"));
	}
}

unittest
{
	auto t = new Test();
}

struct Message(T)
{
	this(T message)
	{
		writeln(typeid(T));
		writeln(message);
	}
Jan 20 2015
parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Tue, 20 Jan 2015 17:59:38 +0000
JC via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

 The following code throws a=20
 core.exception.InvalidMemoryOperationError (0), anyone have an=20
 idea of what is causing it?
=20
 ---
=20
 import std.stdio;
=20
 class Test
 {
 	this()
 	{
 		struct Start
 		{
 			string filename;
 		}
 		Message!(Start)(Start("one.txt"));
 	}
=20
 	~this()
 	{
 		struct Stop
 		{
 			string filename;
 		}
 		Message!(Stop)(Stop("one.txt"));
 	}
 }
=20
 unittest
 {
 	auto t =3D new Test();
 }
=20
 struct Message(T)
 {
 	this(T message)
 	{
 		writeln(typeid(T));
 		writeln(message);
 	}
`writeln` can allocate.
Jan 20 2015