ios: using the same sqlite parameter more than once causes premature memory deallocation

后端 未结 1 1828
一个人的身影
一个人的身影 2020-12-04 03:57

Note: the question is misleading.. i thought it was using more than one parameter that causes a memory error.. but that\'s not the reason.. the reason was an i

相关标签:
1条回答
  • 2020-12-04 04:22

    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"
        " ),"
        " ?"
        " )";
    
    0 讨论(0)
提交回复
热议问题