views:

205

answers:

3

I have a System.Drawing.Pen _pen.

When in some iterations is setting _pen.Width = 3 it gives me:

System.ArgumentException 
  Message="Parameter is not valid."
  Source="System.Drawing"  - System.Drawing.dll
  StackTrace:
       at System.Drawing.Pen.set_Width(Single value)
       at MyProject.ctlPanneauGraphique.CustomLine.set_BorderWidth(Int32 value) in 
       ....
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at MySolution.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

What and why? Thanks.

A: 

How are you initializing the _pen variable? According to MSDN documentation, if you are using the Pens class to initialize your variable, you will get an immutable Pen, whose Width parameter cannot be set. Attempting to do so will result in an ArgumentException being thrown, which is exactly what you are seeing.

kbrinley
as mentioned, the pen.Width set fails in some iterations. I mean that not specially the first time. I created the pen like this _pen = new Pen(Color.Black), and also Pens.Black.Clone() - the same result.
serhio
A: 

I started supposing that comes from a memory leak problem. Apparently the OS cant create more that 10.000 GDI+ objects...

After some analysis was performed I detected a huge memory leack in the application, so the error came from there.

serhio
Give us more code please. How exactly was the pen created?
Vulcan Eager
@Agnel, see my kbrinley comment.
serhio
+2  A: 

Are you disposing the _pen variable?

If not, this may indicate a resource leak problem as you have suspected.

If yes, this may indicate that you are accessing a disposed instance. GDI objects often throw ArgumentException when used after they have been disposed.

Marek
Yes, I dispose the Pen variable. Perhaps you have reason.
serhio