[Vala] Problem with Posix.isspace() : Memory leak ?
- From: Serge Hulne <serge hulne gmail com>
- To: vala-list gnome org
- Subject: [Vala] Problem with Posix.isspace() : Memory leak ?
- Date: Thu, 9 Jun 2011 09:37:37 +0200
In the code hereunder, in the while loop (simple scanner), when I
substitute !isalnum() by Posix.isspace(), the application crashes when
given a very large text file (170 MB) as argument.
Yet it works with !isalnum() instead of isspace().
Also it the application does not crash in either cases when used with
a 15 MB text file as input.
The error message is : "Segmentation fault"
I suspect a memory issue with Posix.isspace()
NB : It also crashes if I substitute !isalnum() for (!isalnum(c) && isspace(c))
Serge.
//----------------
using Gee;
using Posix;
// Struct to hold key, values pairs
struct pair {
public uint freq;
public string word;
}
// Comparison function for stucts
int comp(pair a, pair b) {
if( a.freq > b.freq) return -1;
else if( a.freq < b.freq) return +1;
else return 0;
}
// Equality function for structs
bool equals (pair a, pair b) {
if (Posix.strcmp(a.word, b.word) ==0 ) return true;
else return false;
}
int main(string[] args) {
Posix.assert(args.length == 2);
var f = FileStream.open(args[1], "r");
var map = new HashMap<string, uint> ();
StringBuilder word = new StringBuilder("");
char c = ' ';
// Parse file and insert tokens in Map
while (!f.eof()) {
c = (char) f.getc ();
if (isspace(c))
//if (!isalnum(c))
{
if (word.len>0) {
if (map.has_key(word.str) == false) {
map[word.str] = 1;
}
else {
map[word.str] = map[word.str]+1;
}
}
word.erase(0,-1);
}
else {
word.append_c(c);
}
}
//List
var L = new LinkedList<pair?>( (EqualFunc) equals);
// Insert in List (to sort)
pair p = pair();
foreach (var item in map.entries) {
p.word = item.key;
p.freq = item.value;
L.add(p);
}
// Sort list
L.sort( (CompareFunc)comp );
// Freq distribution
var i = 0;
foreach (var item in L) {
Posix.printf("(%u,\t '%s')\n", item.freq, item.word);
i++;
if (i > 10) break;
}
return 0;
}
//----------------
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]