Bug 5683 – Calling .clear on a fresh associative array causes subsequent segfault
Status
RESOLVED
Resolution
DUPLICATE
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Linux
Creation time
2011-03-02T14:52:00Z
Last change time
2011-08-04T02:19:06Z
Assigned to
nobody
Creator
Marco.Leise
Comments
Comment #0 by Marco.Leise — 2011-03-02T14:52:05Z
In DMD 2.051 and 2.052 the following code doesn't run:
void main() {
int[char] test;
test.clear; // <- this provokes the bug
test['x'] = 42;
}
This happens independently of the key type (int, char, struct). It seems like associative arrays are not in the 'clear' state when they are created.
A typical use case is a field of a class that gets filled with values by a method that needs to clear out old content if any. I considered this a major bug, because it happens with a core feature.
Comment #1 by ibuclaw — 2011-04-04T12:15:29Z
This is a horrible bug with .init, and nothing to do with .clear.
Same code but without the external call:
void main() {
int[char] test;
test = typeof(test).init;
test['x'] = 42;
}
What seems to be happening is that the address of the constructor is being assigned (ie: test = &test_init) when it should *really* be a direct assignment.
Comment #2 by code — 2011-07-19T09:21:33Z
*** Issue 5816 has been marked as a duplicate of this issue. ***
Comment #3 by code — 2011-07-19T12:54:13Z
A quickfix if urgently needed by anybody is to add a static init method to the template AA structure.
struct AssociativeArray(Key, Value)
{
static Value[Key] init() @property
{
void* p;
return *cast(Value[Key]*)(&p);
}
}
Comment #4 by code — 2011-08-04T02:19:06Z
*** This issue has been marked as a duplicate of issue 6433 ***