How to be more careful about null pointers?
cy via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Mar 28 23:00:32 PDT 2016
On Monday, 28 March 2016 at 21:24:48 UTC, Adam D. Ruppe wrote:
> If it didn't give the error, either you swallowed it or you
> didn't actually dereference null.
Okay, so it's not actually supposed to happen. Hopefully it's
something I did wrong...
> What is the db library you are using? Did you compile it along
> with your program or use a .lib with it?
d2sqlite3: https://github.com/biozic/d2sqlite3
Compiled it along with my program. And "Database.prepare" is
neither static, nor final. I keep describing it wrong though,
what I ended up doing. It's easier to just write a program to
describe it. What I ended up doing was like this:
struct Database {
string derp;
Statement prepare(string s) {
return Statement(1234);
}
}
struct Statement {
int member;
void bind(int column, int value) {
import std.stdio;
writeln("derp",member);
}
}
class Wrapper {
Database something;
Statement prep;
this() {
something = Database("...");
prep = something.prepare("...");
}
}
Wrapper oops;
void initialize() {
oops = new Wrapper();
}
class Entry {
Wrapper parent;
this(Wrapper parent) {
//this.parent = parent;
//oops
parent = parent;
}
void usefulmethod() {
parent.prep.bind(1,42);
//parent.prep.execute();
//parent.prep.reset();
}
}
void main() {
initialize();
auto entry = new Entry(oops);
entry.usefulmethod();
}
That program causes a segmentation fault on my machine. Somehow
despite never initializing Entry.parent, a class object (whose
default init is a null pointer), I can still call methods on it,
access members on it, and call methods on those members. No
warnings or errors. The segfault doesn't happen until the bind()
method.
More information about the Digitalmars-d-learn
mailing list