realbasic-nug
[Top] [All Lists]

Re: Delegate.Invoke and memoryBlock/nil/ptr conversions?

To: REALbasic NUG <realbasic-nug@lists.realsoftware.com>
Subject: Re: Delegate.Invoke and memoryBlock/nil/ptr conversions?
From: Charles Yeomans <charles@declareSub.com>
Date: Fri, 29 May 2009 14:01:10 -0400
Authentication-results: mx.google.com; spf=neutral (google.com: 74.124.194.228 is neither permitted nor denied by best guess record for domain of realbasic-nug-bounces@lists.realsoftware.com) smtp.mail=realbasic-nug-bounces@lists.realsoftware.com
Delivered-to: listarchive@realsoftware.com
In-reply-to: <B4DDDB9B-1EEB-4072-9A13-1594C88C81F5@xochi.com>
References: <DA25CAB7-E9AC-4C64-B758-46948A20273D@xochi.com> <FFA512D9-E586-498A-A799-7BA8E875D0F5@declareSub.com> <776FCB16-0B35-4A67-967B-9F1481B9B2DE@xochi.com> <9A86B21A-D9C1-4737-AD6F-9D45C3363F1F@declareSub.com> <B4DDDB9B-1EEB-4072-9A13-1594C88C81F5@xochi.com>
Reply-to: REALbasic NUG <realbasic-nug@lists.realsoftware.com>
Sender: realbasic-nug-bounces@lists.realsoftware.com

On May 29, 2009, at 1:41 PM, Michael Diehr wrote:

Ok -- so the logic here is that when a method is expecting a Ptr, and you pass it a nil memoryblock, it is the operator_convert that is raising the exception? It almost seems like operator_convert should be smarter when dealing with nil objects?

Or, perhaps a IllegalCastException would make more sense?

It would not, because you're not casting in this situation; you are converting. The conversion is accomplished by invoking a method; when the object reference is nil, a NilObjectException is the correct exception type.

But I cannot reproduce your situation...

You can see this technique useed in the rgbl code:

Private Delegate Function wglChoosePixelFormatARBDelegate(hdc as UInt32, piAttribIList as Ptr, piAttribFList as Ptr, nMaxFormats as UInt32, byref piFormats as UInt32, byref nNumFormats as UInt32) As Integer

[...]

   //get pointer to pbuffer create function
declare function wglGetProcAddress lib GL_LIB (lpszProc as CString) as Ptr
   dim fnPtr as Ptr = wglGetProcAddress("wglChoosePixelFormatARB")

   //choose pixel format
   dim pixelFormat as uint32
   if fnPtr <> nil then
     dim null as ptr
     dim count as uint32 = 0
     dim fn as new wglChoosePixelFormatARBDelegate(fnPtr)
     call fn.Invoke(current_hdc, attribs, null, 1, pixelFormat, count)
     if count = 0 then
raise new RBGLException("could not find a suitable pixel format for the pbuffer")
     end
   else
raise new RBGLException("wglChoosePixelFormatARB is not supported on this system")
   end



As you see, they are intentionally passing "null" (a nil Ptr) instead of nil to the delegate function that is expecting a Ptr. If you try to pass nil, then it fails.

Still, I cannot reproduce the exception. I don't have a Windows environment in which to test (sadly, I will soon...). Perhaps you would do so. Here is the code I used.


Delegate TestDelegate(p as Ptr)

Sub Foo(p as Ptr)
  beep
End Sub

And the test code itself --

dim f as TestDelegate = AddressOf Foo
f.Invoke(nil)

Let me know whether you can produce a NilObjectException with this code.


Charles Yeomans









_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>


<Prev in Thread] Current Thread [Next in Thread>