我试图在D中使用Derelict3构建一个简单的3D游戏引擎。事情一直在顺利进行,直到我开始使用关联数组绘制opengl glGenTextures/glGenBuffers。为此,我构建了一个简单的结构,其中包含对要绑定的纹理/ vbo以及从opengl返回的id的引用。然后这将被映射为纹理/ vbo的散列以供稍后检索。关联数组不存储映射
但是,只要它完成设置实际映射,映射就会被神奇地移除。我还不明白为什么。下面是我想要实现的一个简单示例,并且可以观察到相同的行为。
module main;
import std.datetime;
import std.stdio;
class Placeholder {
string value;
this(string value) {
this.value = value;
}
}
private class ResourceInfo {
uint id;
uint time;
Object resource;
static ResourceInfo getOrCreate(Object resource, ResourceInfo[uint] map) {
uint hash = resource.toHash();
ResourceInfo* temp = (hash in map);
ResourceInfo info;
if (!temp){
info = new ResourceInfo();
info.resource = resource;
map[hash] = info;
} else{
info = *temp;
}
// placeholders.lenght is now 1 (!)
info.time = stdTimeToUnixTime(Clock.currStdTime);
return info;
}
}
protected ResourceInfo[uint] placeholders;
void main() {
Placeholder value = new Placeholder("test");
while(true) {
ResourceInfo info = ResourceInfo.getOrCreate(value, placeholders);
// placeholders.lenght is now 0 (!)
if (!info.id) {
info.id = 1; // Here we call glGenBuffers(1, &info.id); in the engine
} else {
// This never runs
writeln("FOUND: ", info.id);
}
}
}
调用placeholders[value.toHash()] = info
手动时,没有ID存在暂时修复,但后来我开始越来越在_aaApply2
object.Error: Access Violation
和_d_delclass
每当我尝试了几秒钟后访问的info
一个实例。
任何人都看到任何明显的我失踪?
难道说那些被分配的malloc()或通过从d使用一些C/C++库相似?通常GC对它们一无所知,只要你知道,它们就可以是免费的()d ... – DejanLekic
我不喜欢你如何删除一个对象的类型信息,当然,你可以投射它并检查它的零,但我这样做的方式是隐藏投射和检查模板的方法,应该返回类型,如果演员失败我会断言假/抛出,取决于你的风格。这样你就不会有凌乱的铸造代码直接在你的使用代码 – Quonux