Skip to content

Fix Issue 16824 - std.experimental.allocator.dispose leaks memory #4982

Merged
JackStouffer merged 4 commits intodlang:masterfrom
RazvanN7:Issue_16824
Jan 17, 2017
Merged

Fix Issue 16824 - std.experimental.allocator.dispose leaks memory #4982
JackStouffer merged 4 commits intodlang:masterfrom
RazvanN7:Issue_16824

Conversation

@RazvanN7
Copy link
Collaborator

The dispose function now checks recursively if there are multidimensional arrays allocated and deallocates them accordingly

@dlang-bot
Copy link
Contributor

Fix Bugzilla Description
16824 std.experimental.allocator.dispose leaks memory for arrays of more than 1 dimension

Copy link
Contributor

@wilzbach wilzbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Watch the style bot (aka CircleCi) ;-)

struct TestAllocator
{
import std.experimental.allocator.common: platformAlignment;
import std.experimental.allocator.mallocator: Mallocator;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style nit: whitespace between the colon : (see also the failed CircleCi or run make -f posix.mak style)


assert(_allocations.canFind!pred);

_allocations = _allocations.remove!pred;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: there is countUntil which allows to the search only once, but of course it doesn't matter for this simple test.

bool deallocate(void[] bytes)
{
import std.algorithm: remove, canFind;
import std.conv: text;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused import


foreach (ref ints1d; ints2d)
foreach(ref ints0d; ints1d)
ints0d = allocator.makeArray!(int)(4);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parenthesis aren't needed here and how about adding this to foreach loop before?

ints0d = allocator.makeArray!(int)(4);

allocator.dispose(ints2d);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about doing the same test with something that has a destructor and thus verifying that the destructor is called for all elements? e.g.

static instances = 0;
struct Foo
{

~this()
{
   instances = 0;
}

// allocate
assert(instances == 100);
// dispose
assert(instances == 0);

@9il
Copy link
Member

9il commented Dec 21, 2016

Should we add a deallocation for pointers?

@andralex
Copy link
Member

Hmmm... it's debatable. I'll comment in https://issues.dlang.org/show_bug.cgi?id=16824

@JackStouffer
Copy link
Contributor

Ping @RazvanN7

@RazvanN7
Copy link
Collaborator Author

@JackStouffer Maybe this PR and associated bug is invalid? As you can see the discussion at [1] and the fact that there is already a function called makeNdarray which seems to do the exact same thing [2].
I tried to implement something, but you can see the community's reaction [3]

[1] https://issues.dlang.org/show_bug.cgi?id=16824
[2] https://github.com/dlang/phobos/blob/master/std/experimental/ndslice/slice.d#L834
[3] http://forum.dlang.org/post/znjnasmklluqwxbntvhm@forum.dlang.org

@RazvanN7
Copy link
Collaborator Author

@9il do you think this is needed?

@9il
Copy link
Member

9il commented Jan 12, 2017

I'm not sure

Copy link
Member

@andralex andralex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice thx

/**
Allocates a multidimensional array of elements of type T.

Params:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't use capital names for values, just use n

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx for the good examples, though no exception is justified there. We should probably slowly fix those as the code gets touched instead of introducing more instances.

cc @wilzbach: all values and functions are likeThis, all types are LikeThis, aliases that may be either should generally be likeThis, modules and labels should be like_this.


Returns:
An N-dimensional array with individual elements of type T.
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

N should be uint

return makeArray!T(alloc, lengths[0]);
}
else
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces around -, .. cc @wilzbach

import std.experimental.allocator.mallocator : Mallocator;

size_t[3] dimArray = [2, 3, 6];
auto mArray = Mallocator.instance.makeMultidimensionalArray!(dimArray.length, int)(dimArray);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

guess it's nice to also deallocate here with scope(exit)

array = the multidimensional array that is to be deallocated
*/
void disposeMultidimensionalArray(Allocator, T)(auto ref Allocator alloc, T[] array)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after if cc @wilzbach

@RazvanN7
Copy link
Collaborator Author

@andralex Hope we're good now. Thanks for reviewing!

@JackStouffer
Copy link
Contributor

Merging due to Andrei's approval

@JackStouffer
Copy link
Contributor

Auto-merge toggled on

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants