Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dtor / destructor not called for (rvalue) struct used in opApply #17594

Open
dlangBugzillaToGithub opened this issue Jun 18, 2013 · 4 comments
Open

Comments

@dlangBugzillaToGithub
Copy link

Marco Leise (@mleise) reported this on 2013-06-18T16:09:26Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=10409

CC List

Description

This code

--- 8< ---------------

import core.stdc.stdio;

struct DestroyMe
{
	~this() { printf("~this() called
"); }

	int opApply(in int delegate(int item) dg)
	{
		throw new Exception("Here we go!");
	}
}

void main()
{
	printf("Version with no dtor call:
");
	try {
		foreach (item; DestroyMe()) {}
	} catch {}
	printf("Version with dtor call:
");
	try {
		auto lvalue = DestroyMe();
		foreach (item; lvalue) {}
	} catch {}
}

--- >8 --------------

prints:
Version with no dtor call:
Version with dtor call:
~this() called

So the dtor call gets missed when the struct's scope is inside the foreach header. There is another open bug about struct destructors not being called on out parameters:
http://d.puremagic.com/issues/show_bug.cgi?id=6186
@dlangBugzillaToGithub
Copy link
Author

k.hara.pg commented on 2013-07-07T23:13:36Z

I couldn't reproduce the issue on Windows7, by using release-dmd versions from 2.058 to 2.063.2. What version do you use?

@dlangBugzillaToGithub
Copy link
Author

Marco.Leise commented on 2013-07-08T02:54:27Z

(In reply to comment #1)
> I couldn't reproduce the issue on Windows7, by using release-dmd versions from
> 2.058 to 2.063.2. What version do you use?

I was using GDC and LDC. It really doesn't happen with DMD. I'll mark this as invalid and open new reports for the other compilers. Thanks for looking into this anyway!

@dlangBugzillaToGithub
Copy link
Author

ibuclaw (@ibuclaw) commented on 2013-07-08T11:34:09Z

From the backend's perspective, the frontend represents the code in this way:

try
{
    DestroyMe __sl5;
    DestroyMe.opApply (&__sl5, {.object=0B, .func=__foreachbody6});
    apply.DestroyMe.~this (&__sl5);
}
catch (struct Throwable &)
{
}


try
{
    struct DestroyMe lvalue;

    try
    {
        DestroyMe.opApply (&lvalue, {.object=0B, .func=__foreachbody8});
    }
    finally
    {
        apply.DestroyMe.~this (&lvalue);
    }
}
catch (struct Throwable &)
{
}


In this instance, you could either say that it is the job of the backend to wrap ExpDtorStatement's in try{} finally{} blocks, or fix the frontend to generate a matching representation.

@dlangBugzillaToGithub
Copy link
Author

ibuclaw (@ibuclaw) commented on 2013-07-08T12:15:06Z

(In reply to comment #3)
> In this instance, you could either say that it is the job of the backend to
> wrap ExpDtorStatement's in try{} finally{} blocks, or fix the frontend to
> generate a matching representation.

Hmmm... I meant Expression::toElemDtor.  :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant