The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,604 other followers

Archive for April 27th, 2009

Answered @ stackoverflow – on virtual constructors called through class references in Delphi

Posted by jpluimers on 2009/04/27

I just tried to elaborate on an answer to a question from Michael Justin on stackoverflow.
(yes, that’s the author of some message broker clients like the Habari ActiveMQ client that allows Delphi applications to talk to the Apache ActiveMQ message queueing broker).

His question is on virtual constructors and class reference: How can I detect if a Delphi class has a virtual constructor?
The combination of class references and virtual constructors is one of the fundaments of how Delphi became Delphi; in simple terms:

  • class references allow you to register components on the component palette in Delphi
  • virtual constructors allows the designer to construct  the instances of those components at design time

That’s why I think it is important to describe the distinction between virtual constructors and regular non-virtual constructors in the answer below (which is a copy of the answer I posted to stackoverflow)

Read the rest of this entry »

Posted in Delphi, Development, Pingback, Software Development, Stackoverflow | Leave a Comment »

Delphi – bizarre use of class helpers to circumvent ‘with’

Posted by jpluimers on 2009/04/27

Quite a while ago, a co-worker asked how to get tot the Instance variable when using a with statement like this:

function TMyForm.ExecuteForm(FormClass: TFormClass): Integer;
begin
  with FormClass.Create(self) do
    try
      Result := ShowModal;
    finally
      Free;
    end;
end;

So I wrote the blog entry below when I started my blog last week, and set the published date to somewhere early May, a week that will be really busy.
Then I found out about the Stackoverflow question Reference object instance created using “with” in Delphi followed by the answer by user ‘Alexander‘ (a really smart Russian guy).
Enough coincidence to publish the blog article earlier than scheduled :-) 

Before I explain why I really hate ‘with’, lets show what my co-worker wanted, and my solution: Read the rest of this entry »

Posted in Delphi, Development, Pingback, Software Development, Stackoverflow | 2 Comments »

.NET/C# – Issue with FileStream on network with transfers larger than 64 megabyte

Posted by jpluimers on 2009/04/27

Quite a while ago (2006!), I bumped into an issue when copying large chuncks of data to a network.

I posted it to Google, mentioned that breaking up the data in smaller blocks worked, but never had the time to post the solution.

So here it is :-)

First the problem:

The old code consistently fails when:

  • the FileStream is on a network
  • and the MemoryStream is 64 megabytes or larger

The old code succeeds when:

  • the MemoryStream is smaller than 64 megabytes
  • or the FileStream is not on a network

An example of the exception message you get upon failure:

System.IO.IOException: Insufficient system resources exist to complete the requested service.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.WriteCore(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.FileStream.Write(Byte[] array, Int32 offset, Int32 count)
   at System.IO.BufferedStream.Write(Byte[] array, Int32 offset, Int32 count)
...

Then the old code:

        public static void WriteMemoryStreamToFile(string filename, MemoryStream memory)
        {
            using (Stream
                file = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite),
                fileBuffer = new BufferedStream(file)
            )
            {
                byte[] memoryBuffer = memory.GetBuffer();
                int memoryLength = (int)memory.Length;
                fileBuffer.Write(memoryBuffer, 0, memoryLength); //##jpl: drawback: works only up to 2 gigabyte!
                fileBuffer.Close();
                file.Close();
            }
        }

Note that the old code already has a limitation of 2 gigabyte, back then this was not an issue (in 2006 there were not that many people having more than 2 gigabytes of memory, now that is a different story).

Read the rest of this entry »

Posted in .NET, C#, Development | 6 Comments »

 
%d bloggers like this: