PDA

View Full Version : HeaderFooter: System.AccessViolationException


Ebh_Finans
January 22, 2007, 14:01:15
In some unit tests we traverse the headers and footers of a ServerTextControl.
We do this by activating a HeaderFooter, editing, and deactivating.

Once in a while, we get the following exception:

Message: System.AccessViolationException : Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

at TXTextControl.Win32.SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam, Int32 lParam)
at TXTextControl.HeaderFooter.Activate()
at EBH.GuG.Util.Print.GenerateTemplate.InsertTextFiel dsInHeader

..but we cannot always reproduce the error.
Our code looks like this:

int InsertTextFieldsInHeader(Dictionary<string, string> dollarCodes, int idCount)
{
foreach (HeaderFooter hf in TextControl.HeadersAndFooters)
{
hf.Activate();
foreach (Line l in hf.Lines)
{
foreach (string dollarCode in dollarCodes.Keys)
{
int pos = 0;
while ((pos = l.Text.IndexOf(dollarCode, pos)) > -1)
{
hf.Selection.Start = pos;
hf.Selection.Length = dollarCode.Length;
hf.Selection.Text = "";
TextField tf = new TextField(dollarCode);
tf.Name = dollarCode;
tf.ID = idCount++;
tf.ShowActivated = true;
TextControl.TextFields.Add(tf);
pos += 1;
}
}
}
hf.Deactivate();
}
return idCount;
}


Any suggestions why we get this memory error?

Björn Meyer
January 22, 2007, 14:34:48
Hello

In some first tests, I was not able to reproduce this issue. Do you have a sample document that produces this problem more "reliable"?

Gunnar Giffey
January 25, 2007, 15:50:14
Hi Martin,

I am working with Björn on your case and I found a small error in your code.
Could you replace
TextControl.TextFields.Add(tf);
with
hf.TextFields.Add(tf);
and test this?
I assume that you do not want to add a text field to the main body, but to the header, right?
Please get back to me with the results.
Thank you!

Ebh_Finans
January 29, 2007, 14:10:20
Thanks for your suggestion.

With regards to layout I still get the desired result. The exception occurs very seldom, so I can't say for sure that it's gone away, but I'll post back if I encounter it again with the new code..

Ebh_Finans
February 13, 2007, 15:04:27
We have encountered the problem again, this time in a different method. Just for good measure, can you see any problems with this use of headers and footers:

ISet<string> dollarCodes = new HashedSet<string>();

HeaderFooter hf = textControl.HeadersAndFooters.GetItem(HeaderFooter Type.Header);
if (hf == null) hf = textControl.HeadersAndFooters.GetItem(HeaderFooter Type.FirstPageHeader);

foreach (TextField tf in textControl.TextFields)
{
dollarCodes.Add(tf.Name);
}

if (hf != null)
{
hf.Activate();
foreach (TextField tf in textControl.TextFields)
{
dollarCodes.Add(tf.Name);
}
hf.Deactivate();
}

Gunnar Giffey
February 13, 2007, 15:28:36
Martin,

I noticed the same mistake as before.
If you work with text fields in headers or footers, please use HeaderFooter.TextFields:
foreach (TextField tf in hf.TextFields)
Also, please add new text fields using this text field collection for headers/footers.
Apart from this, I can not see any problems with the code.

Ebh_Finans
September 1, 2008, 13:53:45
We still have a problem with "System.AccessViolationException : Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

When we run a set of unittest we get the error in two of our test, but when we run the two tests one by one it does´nt fail. ?
When it fails it is always when we call hf1.Activate(); ??

Code:
foreach (Section s in textControl.Sections)
{
foreach (HeaderFooter headerFooter in s.HeadersAndFooters)
{
ExtractHeadersAndFooters(dollarCodeExpr, dollarCodesInDoc, headerFooter);
}
}

private static void ExtractHeadersAndFooters(Regex dollarCodeExpr, ISet<DollarCodeSearchResult> dollarCodesInDoc, HeaderFooter hf1)
{
if (hf1 != null)
{
hf1.Activate();
MatchCollection matches = dollarCodeExpr.Matches(hf1.Selection.Text);
AddDollarCodeFromMatch(matches, dollarCodesInDoc);
hf1.Deactivate();
}
}

Am i the only one experiencing this ?

Björn Meyer
September 1, 2008, 17:11:57
Does an Activate() make sense using a non-visible ServerTextControl instance?

Ebh_Finans
September 25, 2008, 10:59:46
Hello TxTextControl support team. We're still experiencing random AccessViolation errors from the TxText control lib:

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.



at TXTextControl.Win32.SendMessage?(IntPtr? hWnd, Int32 msg, Int32 wParam, Int32[] iArr) at TXTextControl.TextControlCore?.SendToKernel?(TextP art? iTextPart, Int32 msg, Int32 wParam, Int32[] iArr) at TXTextControl.Selection.ApplyToKernel?()

The code leading up to it is:

foreach (Section section in textControl.Sections)
{
foreach (HeaderFooter hf in section.HeadersAndFooters)
{
foreach (DollarCodeSearchResult dollarCodeInDoc in dollarCodesInDoc)
{
while (hf.Selection.Text.IndexOf(dollarCodeInDoc.FullCod e) > -1)
{
hf.Selection.Start = 0;
hf.Selection.Length = dollarCodeInDoc.FullCode.Length;



I cannot see how we could do anything through your API that would cause an AccessViolationException and as the exceptiontext implies the actual memorycorruption could have been caused by something done much earlier.

As we are experiencing this around every 2 weeks and the releaseday of our product is approaching, we are very eager to get rid of the bug. Is there any way you could provide an instrumented library to us with extended debugging capabilities so we're able to provide you more detailed feedback when it happens again ??

Kind regards
ebh-finansservice

Björn Meyer
September 25, 2008, 11:33:05
Hello

Could you please check, if you always call serverTextControl.Dispose() at the end of your processing?

FBtheC
November 9, 2008, 22:51:44
I am running this code:

Dim hdr As TXTextControl.HeaderFooter = txtEditor.Sections.GetItem().HeadersAndFooters.Get Item(TXTextControl.HeaderFooterType.Header)
If Not (hdr Is Nothing) Then
Dim htccoll As TXTextControl.TextCharCollection = hdr.TextChars
Dim hinbold As Boolean = False
Dim hinunder As Boolean = False
If Not (htccoll Is Nothing) Then
Dim hmax As Integer = htccoll.Count - 1
'htccoll = Nothing
If (hmax > 0) Then
For hindex As Integer = 0 To hmax
'hdr.Selection = New TXTextControl.Selection()
hdr.Selection.Start = hindex
hdr.Selection.Length = 1
txtEditor.Refresh()
Application.DoEvents()
System.Threading.Thread.Sleep(1000)
If Not (hdr.Selection.Underline = TXTextControl.FontUnderlineStyle.None) Then
If Not (hinunder) Then
ucount += 1
hinunder = True
End If
Else
hinunder = False
End If
If (hdr.Selection.Bold) Then
If Not (hinbold) Then
bcount += 1
hinbold = True
End If
Else
hinbold = False
End If

Next
End If
End If
End If

The idea is to count the number of bold and underline characters in the header and footer of a saved document.

If I single-step through the above, then it works. If I run without stopping then it fails. It always seems to be on the line that reads the property, and the error is " Attempted to read or write protected memory. This is often an indication that other memory is corrupt." - this sounds very much like the error you have been getting. I may have done something stupid here, but if so I can't for the life of me see what it is...

Björn Meyer
November 10, 2008, 10:13:40
Quick question:

Do you use the ServerTextControl or TextControl?

FBtheC
November 10, 2008, 16:55:20
TextControl

FBtheC
November 10, 2008, 16:59:14
I should add that the calls to application.doevents and thread.sleep were added in an attempt to track down the cause of this problem, and should not otherwise be necessary from an application point of view. An instance of a TextControl is created and loaded with a document, though not shown; it parses body text very well, we have only hit this problem when dealing with the header and footer

Gunnar Giffey
November 10, 2008, 17:04:15
The TextControl should have been placed on a Windows Form and must be visible when using it.
In your case, I assume the control is invisible, correct?
What result do you get if you set the TextControl to visible?

FBtheC
November 10, 2008, 17:51:27
The text control is created at run time, and its visible property set to true, though it is never actually shown on screen; I had to set the visible property in order to get it correctly loading a document. We cannot use a visible control on screen since the routine ultimately needs to be transferred to a windows service

Gunnar Giffey
November 11, 2008, 16:01:40
We cannot use a visible control on screen since the routine ultimately needs to be transferred to a windows service
In this case, you should use the ServerTextControl, which requires a TX Text Control .NET Server license.
The TextControl is intended to be used visible for user interaction, the ServerTextControl for all invisible word processing processes.