Jump to page: 1 2
Thread overview
struct destructor
May 15, 2021
Alain De Vos
May 15, 2021
Adam D. Ruppe
May 15, 2021
Alain De Vos
May 15, 2021
Alain De Vos
May 15, 2021
Dennis
May 15, 2021
Alain De Vos
May 15, 2021
Alain De Vos
May 16, 2021
cc
May 16, 2021
Adam D. Ruppe
May 16, 2021
mw
May 20, 2021
frame
May 21, 2021
Imperatorn
May 15, 2021
Adam D. Ruppe
May 15, 2021
Adam D. Ruppe
May 15, 2021
Alain De Vos
May 15, 2021
mw
May 15, 2021

When I do a "new" in a struct constructor to assign to a member variable of this struct, what do i write in the same struct destructor to free the memory ?

May 15, 2021

On Saturday, 15 May 2021 at 16:52:10 UTC, Alain De Vos wrote:

>

When I do a "new" in a struct constructor to assign to a member variable of this struct, what do i write in the same struct destructor to free the memory ?

If you used new the garbage collector is responsible for it.

May 15, 2021

On Saturday, 15 May 2021 at 16:53:04 UTC, Adam D. Ruppe wrote:

>

On Saturday, 15 May 2021 at 16:52:10 UTC, Alain De Vos wrote:

>

When I do a "new" in a struct constructor to assign to a member variable of this struct, what do i write in the same struct destructor to free the memory ?

If you used new the garbage collector is responsible for it.

Can I send a kind message to the garbage collector to please free that speficic memory at that time specified ?

May 15, 2021

Feature request, a function old which does the opposite of new, allowing deterministic,real-time behavior and memory conservation.

May 15, 2021

On Saturday, 15 May 2021 at 17:55:17 UTC, Alain De Vos wrote:

>

Feature request, a function old which does the opposite of new, allowing deterministic,real-time behavior and memory conservation.

You can use object.destroy to destruct, and GC.free to free memory allocated with new.

May 15, 2021

Thanks, good idea but,
It does not initiate a GC cycle or free any GC memory.

May 15, 2021

On Saturday, 15 May 2021 at 18:15:24 UTC, Dennis wrote:

>

On Saturday, 15 May 2021 at 17:55:17 UTC, Alain De Vos wrote:

>

Feature request, a function old which does the opposite of new, allowing deterministic,real-time behavior and memory conservation.

You can use object.destroy to destruct, and GC.free to free memory allocated with new.

Specifically you wanna do:

.destroy(*ptr);
GC.free(GC.addrOf(ptr));
May 15, 2021

Sorry free does , indeed.

May 15, 2021

On Saturday, 15 May 2021 at 17:55:17 UTC, Alain De Vos wrote:

>

Feature request, a function old which does the opposite of new, allowing deterministic,real-time behavior and memory conservation.

You're best off doing malloc+free if you want complete control though.

May 15, 2021

I'll try first the first tip of Adam, here the code,

import std.stdio:writeln;
import core.memory: GC;
void myfun(){
class C{
	int[10000] x;
	}//class C
	
struct S {
	C c=null;
	@disable this();

	this(int dummy) {
		c=new C();
		writeln("Constructor");
		};//Constructor

	~this(){
		writeln("Destructor");
		.destroy(c);
		void * address=GC.addrOf(cast(void *)c);
		GC.free(address);
		};//destructor
}//struct S
S mys=S(0);
};//myfun()

void main(){
	myfun();
};//main
« First   ‹ Prev
1 2