Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No stable cloning of System.Drawing.Font object #30

Open
ghost opened this issue Jul 7, 2022 · 7 comments
Open

No stable cloning of System.Drawing.Font object #30

ghost opened this issue Jul 7, 2022 · 7 comments

Comments

@ghost
Copy link

ghost commented Jul 7, 2022

No description provided.

@force-net
Copy link
Owner

It can be problematic on Windows due use of native handles, which are problematic to clone. When you dispose one object, it closes native handle, so, other object stops to work. And in different classes - different policy for using native resources. I do not found way to make correct work for these types in any situation.
May be I'll try to make something with this type. Can you describe, which effects on Font clone occur?

@ghost
Copy link
Author

ghost commented Jul 20, 2022

The solution is to identify the type and re-new a new object. It would solve away most of the problem, I'm not familiar with whether DeepCloner has a special tool to handle this. So if my class involves related objects, I would consider writing deep copies and using DeepCloner for other objects to solve it.

@ian-g-holm-intel
Copy link

ian-g-holm-intel commented Nov 2, 2022

I'm having a similar problem cloning an Exception that includes an IntPtr in the TargetSite property. Is it possible to have DeepCloner ignore certain properties in Exception such as TargetSite?

@force-net
Copy link
Owner

force-net commented Nov 3, 2022

Every IntPtr is a problem. In dotnet it simply native int (long/int depends on architecture). But it points to some unmanaged resource. And when we clone object - two object begun to own this resource and use it. But they do no not know about each other and can release it or do something other that will cause an error.
If DeepCloner will not copy IntPtr (e.g. set it to 0) - second object can throw NullPointerException but in some native way.
Some handles can be safely cloned, but other is impossible for this operation.
As result, it is very serious problem, and I do not know, what I can do with this globally.

@ian-g-holm-intel
Copy link

But an IntPtr is simply a struct that points to that address. So isn't cloning the IntPtr as simple as creating a new IntPtr that points to the same address? The question of "what" is at that address shouldn't be relevant to the cloning process.

@sgf
Copy link

sgf commented Dec 6, 2023

Intptr is just a pointer, essentially just a nint. You only need to copy its value to the new nint/IntPtr structure like nint.

@lofcz
Copy link

lofcz commented Jan 8, 2025

I've added support for System.Drawing.Font in FastCloner. This is tricky and requires adding a reference to FastCloner.Contrib for it to work because of a reference to System.Drawing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants