Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hello!

I develope in C# , manged code ,IDE vs2010.

I received the follow exception:

"Attempted to read or write protected memory.this is often indication that other memory is corrupt"

I recived this error when I try to change button image from resources.

code :

statusButton.Image=Resources.Status_Ok;

Or

statusButton.Image=Resources.Status_Fail;


The exception is not appear all the time!!.

The command is fetch by timer [System.Timers.Timer].


it's make me crazy ....

Thanks!
M.A.K
Posted
Updated 28-Jul-15 5:36am
v2
Comments
Sergey Alexandrovich Kryukov 28-Jul-15 11:43am    
Not enough information. You must be doing something nasty, but not in the lines shown.
—SA
[no name] 28-Jul-15 14:52pm    
O.K ,you catch me...
In background I use driver that I wrote for pooling data from pci-e card via DMA,
If I run only the driver w/o to update the GUI status I can run for hours/days the only problem start only when I try to update the button image [update other thing like label , text etc' is o.k] .

Start by loading the two Images into class level variables in the Load event, and use those to set your button image in the timer Tick event - at the moment, you are creating new images each time you set it which may be the problem.
 
Share this answer
 
Comments
[no name] 29-Jul-15 1:33am    
Like I say ,
In background I use driver that I wrote for pooling data from pci-e card via DMA,
If I run only the driver w/o to update the GUI status I can run for hours/days the only problem start only when I try to update the button.
Your answer solve my current error , it's move it to another place , I still recived
the same exceptin in other places in my code....
May you have any technique for recognize the source of the memory problem.
Thanks!
Moti
OriginalGriff 29-Jul-15 1:46am    
We can't really help you with that because we can't see - and more importantly run - your code.
So start by looking for places where you do "silly things": creating images a lot, using Graphics contexts and not disposing of them, DB connections or commands without disposing them, anything that uses resources. And check your "driver" - it's quite possible that it's causing the problem but it shows up elsewhere.
Sorry to sound vague - but we have no idea how your software works, so vague is the best we can manage with a "general" problem like that! :laugh:
[no name] 29-Jul-15 3:47am    
The problem solved.
Bad marshal operation.
When I recived data from the pci-e card I also write the data into H.D for each buffer I add header , the header is convert from struct to byte array .
I use the function :

Marshal.StructToPtr(struct,_headerPtr,false);
public static void StructureToPtr(Object structure,IntPtr ptr,bool fDeleteOld)

fDeleteOld

true to call the Marshal.DestroyStructure method on the ptr parameter before this method copies the data. The block must contain valid data. Note that passing false when the memory block already contains data can lead to a memory leak.

So I cahnge it to true and that solve the problem.

Thanks.
M.A.K
OriginalGriff 29-Jul-15 3:52am    
Glad to hear it! :thumbsup:
It simply say that resources are off limit for modifications.
You are not allowed to do this as long as the image is in the resources.
Try to copy it in a variable.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900