Skip to content

Instantly share code, notes, and snippets.

@mamarjan
Created June 11, 2012 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamarjan/2911818 to your computer and use it in GitHub Desktop.
Save mamarjan/2911818 to your computer and use it in GitHub Desktop.
Minimal example for GC seg fault in D
// Run this file with one large text file and a segmentation fault should occur
import std.stdio, std.file, std.conv, std.string;
File input_file;
void main(string[] args) {
input_file = File(args[1], "r");
while(true) {
next_item();
try_and_catch();
}
}
void try_and_catch() {
try {
throw new Exception("A record with invalid number of columns");
} catch (Exception e) {
}
}
char[] current_chunk;
void next_item() {
char[] line;
bool line_complete = false;
while (!line_complete) {
if (current_chunk.length == 0) {
current_chunk = new char[8177];
current_chunk = input_file.rawRead(current_chunk);
} else {
auto newline_index = current_chunk.indexOf('\n');
if (newline_index != -1) {
line ~= current_chunk[0..newline_index];
current_chunk = current_chunk[newline_index+1..$];
line_complete = true;
} else {
line = current_chunk;
current_chunk = null;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment