|
It passes references, not copies.
The general rule of thumb is:
If you create a disposable object with new , its up to you to dispose it, or you can just wait for the GC to do it.
|
|
|
|
|
okay, so since i didnt explicitly create this graphics object like <coed>Grapics blah = this.CreateGraphics(); i dont have to worry about disposing it?
Harvey Saayman - South Africa
Junior Developer
.Net, C#, SQL
you.suck = (you.passion != Programming)
|
|
|
|
|
HarveySaayman wrote: okay, so since i didnt explicitly create this graphics object like Grapics blah = this.CreateGraphics(); i dont have to worry about disposing it?
That is correct There might be few exceptions, but I wouldnt lose sleep over it, just let the GC handle them
|
|
|
|
|
cool, thanx bud
Harvey Saayman - South Africa
Junior Developer
.Net, C#, SQL
you.suck = (you.passion != Programming)
|
|
|
|
|
If you were to dispose of this graphics object, I am sure the app would blow up. It's being passed to you, what passes it, needs to deal with it.
NEVER rely on GC for GDI objects you create, that's a good way to kill your app
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Christian Graus wrote: the app would blow up
oooh that sounds like it could be fun. maybe even useful as an "undocumented feature" to "take care" of difficult end users
Christian Graus wrote: NEVER rely on GC for GDI objects you create
yeah, the reason for the question is i remembered reading some ware here on CP that its important to dispose of graphics objects... but i cant remember whether the author was using the OnPaint's grapics object or a new 'ed one.
thanx for your insight
Harvey Saayman - South Africa
Junior Developer
.Net, C#, SQL
you.suck = (you.passion != Programming)
|
|
|
|
|
Glad to help. The idea that GC means you don't have to worry about resources is easy to get from the literature, and it's very wrong.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Christian Graus wrote: and it's very wrong
agreed, which is exactly why i asked the question. the one control(the one with a pie sweeping in between two lines) redraws as fast as it possibly can(im assuming this because i cant see a visible change of the speed when the timer is on 100 or on 1ms)
i was thinking that if there is a resource problem i might not even realize it with my 3Ghz & 2gig ram dev pc where as a users 1.5ghz & 512mb ram might crash or be very slow and i wouldnt know why...
Harvey Saayman - South Africa
Junior Developer
.Net, C#, SQL
you.suck = (you.passion != Programming)
|
|
|
|
|
Hi Harvey,
HarveySaayman wrote: i cant see a visible change of the speed when the timer is on 100 or on 1ms
You may want to read my timers article then.
|
|
|
|
|
ill do just that
thanx Luc
Harvey Saayman - South Africa
Junior Developer
.Net, C#, SQL
you.suck = (you.passion != Programming)
|
|
|
|
|
It's passed as reference, and you shouldn't dispose it at all.
Only dispose Graphics objects that you create yourself (e.g. with a call to CreateGraphics()); the Graphics object in the Paint event is created by .NET and will be disposed by .NET after all event handlers are finished.
|
|
|
|
|
Hi! I want to know if is possible to select just the date from the DateTimePicker, I don't want the time, just the date, any help? Thx
|
|
|
|
|
dateTimePicker1.Value.Date returns a DateTime with just the date value - the time is set to 00:00:00. That's the closest you can get unless you create your own Date class.
Dave
|
|
|
|
|
thx...but I solved other way, I changed the format of the DateTimePicker to custom and then I declare in the customFormat field 'dd-MM-yyyy' and it works fine. But thx for the help anyway, have a nice day
|
|
|
|
|
That controls the display, the underlying DateTime may still have other values for the time-of-day.
And please use an ISO 8601 format: yyyy-MM-dd
|
|
|
|
|
I wrote code to access structre from C# .net where structure is in C dll .here are the two files .When i build i get the error message saying that "Cannot take the address of, get the size of, or declare a pointer to a managed type ('StructureExample.Program.Detail')"
------------------------- StrcutreDisplay.dll---------------------------------
#include<iostream.h>
#include<conio.h>
#include<malloc.h>
#include<string.h>
extern "C"
{
typedef struct
{
char *fname;
char *lname;
char abc[5];
}Detail;
__declspec(dllexport) Detail * Disp()
{
Detail *ptr=(Detail *)malloc(sizeof(Detail));
ptr->fname = (char*)malloc(50);
memset(ptr->fname, 0x00, 50);
ptr->lname = (char*)malloc(50);
memset(ptr->lname, 0x00, 50);
memset(ptr->abc, 0x00, 5);
strcpy(ptr->fname, "larson");
strcpy(ptr->lname, "scolari");
strcpy(ptr->abc, "ABC");
return ptr;
}
}
----------------------------StructureExample---------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace StructureExample
{
class Program
{
[StructLayout(LayoutKind.Explicit)]
public struct Detail
{ // [MarshalAs(UnmanagedType.LPStr)]
public unsafe sbyte* fname;
public unsafe sbyte* lname;
public char[] arr;
}
// strucure DLL
[DllImport("StructureDisplay.dll")]
public static unsafe extern Detail * Disp();//here i get error
static void Main(string[] args)
{
unsafe
{
Detail * var = Disp();
string a = new string(var->fname);
string b = new string(var->lname);
string e = new string(var->abc);
}
Console.WriteLine("hello world");
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(e);
Console.Read();
}
}
}
------------------------------------------------------------------------------------------------
How to solve this problem?can anyone help me out?
Any help appreciated.
Thanks in advance.
|
|
|
|
|
cristi_alonso wrote: [StructLayout(LayoutKind.Explicit)]
Thats wrong. It is not needed.
Also public char[] arr; will need some fixed size attribute. Look at System.Runtime.InteropServices.MarshalAsAttribute .
|
|
|
|
|
I changed my declaration of array to
[MarshalAs(UnmanagedType.LPArray, SizeConst=5)] char[] abc;
but still its giving me an error.
Any help appreciated
Thanks in advance.
|
|
|
|
|
What does sizeof(struct Detail) give you in C? Use that to figure out the layout.
|
|
|
|
|
I removed the line [StructLayout(LayoutKind.Explicit)] from my code .
Any help appreciated.
Thanks in advance.
|
|
|
|
|
cristi, i see your new here...
please get in the habit of using pre tags if your posting code blocks...
thanx
Harvey Saayman - South Africa
Junior Developer
.Net, C#, SQL
you.suck = (you.passion != Programming)
|
|
|
|
|
Hello everyone,
I am using the following code to read a string. The content of message is ends with a lot of \0 characters. Why and how to remove them?
(context is an instance of HttpListenerContext, and my code is read Http request from begin to end)
using (StreamReader requestStream = new StreamReader(context.Request.InputStream))
{
message = requestStream.ReadToEnd();
}
thanks in advance,
George
|
|
|
|
|
George_George wrote: message = requestStream.ReadToEnd();
Try:
message = new string(requestStream.ReadToEnd());
That should stop at the first \0.
|
|
|
|
|
Thanks leppie,
Your code is magic. What is the root cause of my original issue? And why your code can fix?
regards,
George
|
|
|
|
|
George_George wrote: Your code is magic.
George_George wrote: What is the root cause of my original issue? And why your code can fix?
It was probably in the File/Resource you were reading. But because you need to read the whole file, ReadToEnd actually does not seem to stop at \0. This is probably by design.
My code fixes it as creating a string, will make sure it is only up to the first \0. After that, the rest is never considered.
To add: It is interesting that you found this. I have never come across it, but at least I am aware of it now (thanks).
|
|
|
|