I finally found it! after chasing so many red herrings.. the best advice i got was on a forum specialized for sqlite: the point was to keep the guts of sqlite out of my debugging reach, it's extremely unlikely that it's sqlite's fault.
i basically decided to break my sql statement into smaller pieces and run each on it's own:
original sql statement that caused the memory problem:
NSString* updateStmt = @"INSERT INTO search_email(docid, meta, subject, body, sender, tos, ccs, folder, threadid)"
" SELECT ?, ?, ?, ?, ?, ?, ?, ?,"
" coalesce((SELECT search_email.threadID "
" FROM search_email "
" WHERE search_email.subject MATCH ? UNION SELECT * FROM "
" (SELECT threadID FROM (SELECT threadID FROM search_email WHERE search_email.sender MATCH ? "
" INTERSECT SELECT threadID FROM search_email WHERE search_email.tos MATCH ? ) "
" UNION "
" SELECT threadID FROM (SELECT threadID FROM search_email WHERE search_email.sender MATCH ? "
" INTERSECT SELECT threadID FROM search_email WHERE search_email.tos MATCH ?)) "
" LIMIT 1"
" ),"
" ?"
" )";
the problem happened whenever i supplied strings with special characters to match..
the following are examples of problematic parameters sent to MATCH:
sabaho :)
New core-audio questions for Feb 1 - Stack Exchange
Ref; Data Centric testing/ETL Tester oppurtunity at Chicago, IL.
so to go around that i simply replaced MATCH with a normal =
comparison.. but cleaned up the parameter using regex first:
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"re:(\s)*"
options:NSRegularExpressionCaseInsensitive
error:&error];
error:&error];
NSString *filteredSubjectFromRe = [regex stringByReplacingMatchesInString:subject
options:0
range:NSMakeRange(0, [subject length])
withTemplate:@""];
if(searchEmailInsertStmt == nil) {
NSString* updateStmt = @"INSERT INTO search_email(docid, meta, subject, body, sender, tos, ccs, folder, threadid)"
" SELECT ?, ?, ?, ?, ?, ?, ?, ?,"
" coalesce((SELECT search_email.threadID "
" FROM search_email "
" WHERE search_email.subject = ? UNION SELECT * FROM "
" (SELECT threadID FROM (SELECT threadID FROM search_email WHERE search_email.sender = ? "
" INTERSECT SELECT threadID FROM search_email WHERE search_email.tos = ? ) "
" UNION "
" SELECT threadID FROM (SELECT threadID FROM search_email WHERE search_email.sender = ? "
" INTERSECT SELECT threadID FROM search_email WHERE search_email.tos = ?)) "
" LIMIT 1"
" ),"
" ?"
" )";