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

StackOverflow https://stackoverflow.com/questions/10346720

Question

Like a lot of this topics so far I am having a problem that this exception occures in one specific place (5% of the time) and somewhere else.

The first line of code is this

 ((DefinitionDetailForm.FindNameInContent("DataFieldDefinitionPoints") as DataField).
                        FindName("DefinitionScoringPoints") as RadNumericUpDown).
                        Minimum = 0;

First I thought that I maybe need to keep the reference in a field but this didn't help either.

The other situation is in an undefined place for which I don't even get anything in the call stack so that I can not give any more info :(

Anyone at least having an idea about the first one?

Edit:

Call stack when the thing breaks

[Managed to Native Transition]  
System.Windows.dll!MS.Internal.XcpImports.SetValue(MS.Internal.IManagedPeerBase obj, System.Windows.DependencyProperty property, string s) + 0x6f bytes 
System.Windows.dll!MS.Internal.XcpImports.SetValue(MS.Internal.IManagedPeerBase doh, System.Windows.DependencyProperty property, object obj) + 0x2ce bytes  
System.Windows.dll!System.Windows.DependencyObject.SetObjectValueToCore(System.Windows.DependencyProperty dp, object value) + 0xd6 bytes    
System.Windows.dll!System.Windows.DependencyObject.SetEffectiveValue(System.Windows.DependencyProperty property, ref System.Windows.EffectiveValueEntry newEntry, object newValue) + 0x35 bytes 
System.Windows.dll!System.Windows.DependencyObject.UpdateEffectiveValue(System.Windows.DependencyProperty property, System.Windows.EffectiveValueEntry oldEntry, ref System.Windows.EffectiveValueEntry newEntry, System.Windows.DependencyObject.ValueOperation operation) + 0xe6 bytes    
System.Windows.dll!System.Windows.DependencyObject.SetValueInternal(System.Windows.DependencyProperty dp, object value, bool allowReadOnlySet, bool isBindingInStyleSetter) + 0x248 bytes   
System.Windows.dll!System.Windows.Controls.TextBox.Text.set(string value) + 0x33 bytes  
Telerik.Windows.Controls.Input!Telerik.Windows.Controls.RadNumericUpDown.UpdateText() + 0xa5 bytes  
Telerik.Windows.Controls.Input!Telerik.Windows.Controls.RadNumericUpDown.OnValueChanged(Telerik.Windows.Controls.RadRangeBaseValueChangedEventArgs e) + 0x1f5 bytes 
Telerik.Windows.Controls!Telerik.Windows.Controls.RadRangeBase.OnValueChanged(System.Windows.DependencyObject d, System.Windows.DependencyPropertyChangedEventArgs e) + 0x105 bytes 
Telerik.Windows.Controls!Telerik.Windows.PropertyMetadata.PropertyChangeHook.OnPropertyChanged(System.Windows.DependencyObject d, System.Windows.DependencyPropertyChangedEventArgs e) + 0x58b bytes    
System.Windows.dll!System.Windows.DependencyObject.RaisePropertyChangeNotifications(System.Windows.DependencyProperty dp, object oldValue, object newValue) + 0x53 bytes    
System.Windows.dll!System.Windows.DependencyObject.UpdateEffectiveValue(System.Windows.DependencyProperty property, System.Windows.EffectiveValueEntry oldEntry, ref System.Windows.EffectiveValueEntry newEntry, System.Windows.DependencyObject.ValueOperation operation) + 0xf3 bytes    
System.Windows.dll!System.Windows.DependencyObject.RefreshExpression(System.Windows.DependencyProperty dp) + 0x47 bytes 
System.Windows.dll!System.Windows.Data.BindingExpression.SendDataToTarget() + 0xff bytes    
System.Windows.dll!System.Windows.Data.BindingExpression.SourceAcquired() + 0x5f bytes  
System.Windows.dll!System.Windows.Data.Binding.EnsureBreakPoint(System.Windows.Data.Debugging.BindingDebugState debugState, System.Action callback, bool canDelay) + 0x47 bytes 
System.Windows.dll!System.Windows.Data.BindingExpression.System.Windows.IDataContextChangedListener.OnDataContextChanged(object sender, System.Windows.DataContextChangedEventArgs e) + 0xa4 bytes  
System.Windows.dll!System.Windows.Data.BindingExpression.DataContextChanged(object sender, System.Windows.DataContextChangedEventArgs e) + 0xc bytes    
System.Windows.dll!System.Windows.FrameworkElement.OnDataContextChanged(System.Windows.DataContextChangedEventArgs e) + 0x1e bytes  
System.Windows.dll!System.Windows.FrameworkElement.OnAncestorDataContextChanged(System.Windows.DataContextChangedEventArgs e) + 0x26 bytes  
System.Windows.dll!System.Windows.FrameworkElement.NotifyDataContextChanged(System.Windows.DataContextChangedEventArgs e) + 0xcb bytes  
System.Windows.dll!System.Windows.FrameworkElement.OnTreeParentUpdated(System.Windows.DependencyObject newParent, bool bIsNewParentAlive) + 0x3d bytes  
System.Windows.dll!System.Windows.DependencyObject.UpdateTreeParent(MS.Internal.IManagedPeer oldParent, MS.Internal.IManagedPeer newParent, bool bIsNewParentAlive, bool keepReferenceToParent) + 0x4a bytes    
System.Windows.dll!MS.Internal.FrameworkCallbacks.ManagedPeerTreeUpdate(System.IntPtr oldParentElement, System.IntPtr parentElement, System.IntPtr childElement, byte bIsParentAlive, byte bKeepReferenceToParent, bool canCreateParent) + 0xf8 bytes   
[Managed to Native Transition]  
System.Windows.dll!MS.Internal.XcpImports.FrameworkElement_MeasureOverride(System.Windows.FrameworkElement element, System.Windows.Size availableSize) + 0x62 bytes 
System.Windows.dll!System.Windows.FrameworkElement.MeasureOverride(System.Windows.Size availableSize) + 0x18 bytes  
System.Windows.dll!System.Windows.FrameworkElement.MeasureOverride(System.IntPtr nativeTarget, float inWidth, float inHeight, out float outWidth, out float outHeight) + 0x9e bytes 
[Managed to Native Transition]  
System.Windows.dll!MS.Internal.XcpImports.FrameworkElement_MeasureOverride(System.Windows.FrameworkElement element, System.Windows.Size availableSize) + 0x62 bytes 
System.Windows.dll!System.Windows.FrameworkElement.MeasureOverride(System.Windows.Size availableSize) + 0x18 bytes  
System.Windows.dll!System.Windows.FrameworkElement.MeasureOverride(System.IntPtr nativeTarget, float inWidth, float inHeight, out float outWidth, out float outHeight) + 0x9e bytes 
[Appdomain Transition]  
Was it helpful?

Solution

The exception indicates that something has corrupted unmanaged memory. The corruption often happens at some point before the exception is thrown making it hard to troubleshoot the issue. The spinner might not be involved at all but simply the messenger of a more sinister problem in your application.

Frankly I can only think of one way to corrupt unmanaged memory in Silverlight and that is by using P/Invoke in Silverlight 5. If you are doing that you should take a careful look at that part of your code.

The casts in the question are in no way harmful. At worst a missing or wrong control can trigger a NullReferenceException but that will not corrupt unmanaged memory.


If you want to improve on the casting (which wont solve your problem) you can use an extension method to do some validation and tell you if there is a problem:

static class FrameworkElementExtensions {

  public T FindName<T>(this FrameworkElement parent, String name) {
    var child = parent.FindName(name);
    if (child == null)
      throw new ArgumentException(
        String.Format("No element named '{0}' exists.", name);
    var typedChild = child as T;
    if (typedChild == null)
      throw new ArgumentException(
        String.Format("Named element '{0}' has wrong type.", name);
    return typedChild;
  }

}

Then you can use it like this:

dataField.FindName<RadNumericUpDown>("DefinitionScoringPoints").Minimum = 0;

You will have to implement similar extension methods for the Telerik specific calls.

OTHER TIPS

Well First things first that's a lot of Casts for a single line of code. Every type cast in code means that you know a lot more than the code does, which is bad. And there are also a possiblity that the Find method might return null

If you're trying to set the minimum value for a Numeric Spinner for a form, I'd suggest you do it inside the form - something along the lines of.

DefinitionDetailForm.SetNumericSpinnerMinValue(0);

And the SetNumericSpinnerMinValue could have the logic for setting the Min value for the spinner with proper null checks.

Use a try/catch block around the code you're suspecting the exception to rise.

Again - Get rid of those casts !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top