[pcre-dev] [Bug 1209] New: Calling pcre_free from C# causes …

Page principale
Supprimer ce message
Auteur: Rhodri Jenkins
Date:  
À: pcre-dev
Nouveaux-sujets: [pcre-dev] [Bug 1209] Calling pcre_free from C# causes a memory access violation, [pcre-dev] [Bug 1209] Calling pcre_free from C# causes a memory access violation
Sujet: [pcre-dev] [Bug 1209] New: Calling pcre_free from C# causes a memory access violation
------- You are receiving this mail because: -------
You are on the CC list for the bug.

http://bugs.exim.org/show_bug.cgi?id=1209
           Summary: Calling pcre_free from C# causes a memory access
                    violation
           Product: PCRE
           Version: 8.20
          Platform: x86-64
        OS/Version: Windows
            Status: NEW
          Severity: bug
          Priority: high
         Component: Code
        AssignedTo: ph10@???
        ReportedBy: rjenkins22@???
                CC: pcre-dev@???



I have been using a 64 bit version of the pree3.dll from a C# application
because the Microsoft implementation of Regex was too slow for our needs.

I am import the pcre3.dll function pcre_compile, pcre_exec and pcre_free.

I can get a compiled pcre object and sucessfully passit to pcre_exec to find a
match.

However when I have finished with the object and try to release the pointer
passed back to me from pcre_Compile by calling pcre_free and passing the
pointer to that function I get an AccessViolationException thrown.

If I do not delete the pointer to the compiled regex I am leaking 2 bytes every
time I call the write function. Is this the correct mechanism to delete the
compiled regex object?

Here is a sample of the code:

 [DllImport("pcre3.dll")]
        private static extern IntPtr pcre_compile(string sPattern, int Options,
ref String ErrorMsg, ref int ErrorOffset, string Unknown);


  [DllImport("pcre3.dll")]
        private static extern int pcre_exec(IntPtr re, string ExtraData, string
SourceData, int SourceDataLen, int Offset, int Options, int[] SubStrings, int
SubStringsCount);


  [DllImport("pcre3.dll")]
        private static extern void pcre_free(IntPtr re);


public override void Write(byte[] buffer, int offset, int count)
{            
     StringBuilder sb = new StringBuilder( this.Encoding.GetString(buffer));
     IntPtr staticContentRegex = IntPtr.Zero;


     try
     {


          int Options = 0x00000001 | 0x00000002; //caseless option and
multiline valid for pcre_compile only


         staticContentRegex = pcre_compile(REGEX_GET_STATIC_CONTENT, Options,
ref ErrorMsg, ref iErrorOffset, null);


         if (sb.Length > 0)
        {
             int StartOffset = offset;
             int iNumMatches = 0;


             do
             {                        
                 iNumMatches = pcre_exec(staticContentRegex, null,
sb.ToString(), sb.Length, StartOffset, 0, ovector, OVECCOUNT);                  


                  if (iNumMatches > 0)
                  {
                            //ovector contains the start and end byte of each
match as a pair
                       //so we increment by 2
                       for (int i = 0; i < iNumMatches * 2; i += 2)
                       {
                           int MatchLength = ovector[i + 1] - ovector[i];
                                char[] Matched = new char[MatchLength];
                           sb.CopyTo(ovector[i], Matched, 0, MatchLength);


                           string Hashed =
Evaluate(this.Encoding.GetString(this.Encoding.GetBytes(Matched)));


                           sb.Remove(ovector[i], MatchLength);
                           sb.Insert(ovector[i], Hashed);


                          StartOffset = ovector[i + 1] + Hashed.Length + 1;
//Set the new Start position for the next pass
                      }


                  }




             } while (iNumMatches > 0);



             byte[] bufferOut = this.Encoding.GetBytes(sb.ToString());


             this.BaseFilter.Write(bufferOut, 0, bufferOut.Length);
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                sb = null;


                if (IntPtr.Zero != staticContentRegex)
                {                    
                    pcre_free(staticContentRegex); //exception thrown here
                    staticContentRegex = IntPtr.Zero;
                }
            }          
        }



--
Configure bugmail: http://bugs.exim.org/userprefs.cgi?tab=email