Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1099 from MartinNowak/fixup864
Browse files Browse the repository at this point in the history
don't reset memory of finalized classes/structs/arrays
  • Loading branch information
andralex committed Jan 15, 2015
2 parents dbbea7c + a097620 commit 8fffa42
Showing 1 changed file with 8 additions and 26 deletions.
34 changes: 8 additions & 26 deletions src/rt/lifetime.d
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ extern (C) void _d_delarray_t(void[]* p, const TypeInfo_Struct ti)
if ((*p).ptr)
{
if (ti) // ti non-null only if ti is a struct with dtor
finalize_array(p.ptr, p.length * ti.tsize, ti, false);
finalize_array(p.ptr, p.length * ti.tsize, ti);

// if p is in the cache, clear it as well
if(auto bic = __getBlkInfo((*p).ptr))
Expand Down Expand Up @@ -1370,7 +1370,7 @@ int hasArrayFinalizerInSegment(void* p, size_t size, in void[] segment) nothrow
}

// called by the GC
void finalize_array2(void* p, size_t size, bool resetMemory = true) nothrow
void finalize_array2(void* p, size_t size) nothrow
{
debug(PRINTF) printf("rt_finalize_array2(p = %p)\n", p);

Expand All @@ -1394,15 +1394,15 @@ void finalize_array2(void* p, size_t size, bool resetMemory = true) nothrow

try
{
finalize_array(p, size, si, resetMemory);
finalize_array(p, size, si);
}
catch (Throwable e)
{
onFinalizeError(si, e);
}
}

void finalize_array(void* p, size_t size, const TypeInfo_Struct si, bool resetMemory = true)
void finalize_array(void* p, size_t size, const TypeInfo_Struct si)
{
// Due to the fact that the delete operator calls destructors
// for arrays from the last element to the first, we maintain
Expand All @@ -1411,20 +1411,11 @@ void finalize_array(void* p, size_t size, const TypeInfo_Struct si, bool resetMe
for (auto curP = p + size - tsize; curP >= p; curP -= tsize)
{
si.xdtor(curP); // call destructor

if(resetMemory)
{
ubyte[] w = cast(ubyte[])si.m_init;
if (w.ptr is null)
(cast(ubyte*) curP)[0 .. w.length] = 0;
else
(cast(ubyte*) curP)[0 .. w.length] = w[];
}
}
}

// called by the GC
void finalize_struct(void* p, size_t size, bool resetMemory = true) nothrow
void finalize_struct(void* p, size_t size) nothrow
{
debug(PRINTF) printf("finalize_struct(p = %p)\n", p);

Expand All @@ -1433,15 +1424,6 @@ void finalize_struct(void* p, size_t size, bool resetMemory = true) nothrow
{
if (ti.xdtor)
ti.xdtor(p); // call destructor

if(resetMemory)
{
ubyte[] w = cast(ubyte[])ti.m_init;
if (w.ptr is null)
(cast(ubyte*) p)[0 .. w.length] = 0;
else
(cast(ubyte*) p)[0 .. w.length] = w[];
}
}
catch (Throwable e)
{
Expand Down Expand Up @@ -1502,11 +1484,11 @@ extern (C) void rt_finalizeFromGC(void* p, size_t size, uint attr)
{
// to verify: reset memory necessary?
if (!(attr & BlkAttr.STRUCTFINAL))
rt_finalize2(p, false, true); // class
rt_finalize2(p, false, false); // class
else if (attr & BlkAttr.APPENDABLE)
finalize_array2(p, size, true); // array of structs
finalize_array2(p, size); // array of structs
else
finalize_struct(p, size, true); // struct
finalize_struct(p, size); // struct
}


Expand Down

0 comments on commit 8fffa42

Please sign in to comment.