A question about Delegates [RESOLVED]

Mac OSX specific forum
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

A question about Delegates [RESOLVED]

Post by coder14 »

From this post: http://www.purebasic.fr/english/viewtop ... 31#p464631
Shardik wrote:

Code: Select all

ProcedureC didRemoveDeviceInBrowser(object.i, selector.i, deviceBrowser.i, removedDevice.i, moreGoing.i)
  Debug "device removed in browser..."
  CocoaMessage(0, removedDevice, "requestCloseSession")
  CocoaMessage(0, removedDevice, "setDelegate:", 0)
  RemoveGadgetItem(#scannerList, selectedScanner - 1)
  ReDim scanners(ArraySize(scanners()) - 1)
EndProcedure   ;IMA!
I managed to get the code working again but everytime the scanner is powered off this method fires and crashes with IMA with the error mark on the EndProcedure line. Sometimes I get "Program aborted (by external library)". :shock:

Maybe the device browser terminates and ends the delegate (PB app) when all scanners are removed so we should make another delegate for it and not use the main PB app. I tried to follow this example but it doesn't work:

From this post: http://www.forums.purebasic.com/english ... a0#p497287

Code: Select all

; ----- Subclass ListIconGadget
SubclassedListIconGadget = objc_allocateClassPair_(CocoaMessage(0,
  GadgetID(0), "class"), "SubclassedListIconGadget", 0)
objc_registerClassPair_(SubclassedListIconGadget)
object_setClass_(GadgetID(0), SubclassedListIconGadget)
A confusing problem but I just need some education about using delegates from PB. Thank you.
Last edited by coder14 on Thu Dec 29, 2016 7:17 am, edited 1 time in total.
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: A question about Delegates

Post by coder14 »

Update 1: I connected 2 scanners and removed just 1 and it still crashed. :cry:

Update 2: Putting a MessageRequester in the procedure stops the crash when the scanner is powered off, but it will still crash when the message box is dismissed. But if the scanner is powered back on and added back to the device browser BEFORE dismissing the message box it does not crash! :D

Code: Select all

ProcedureC didRemoveDeviceInBrowser(object.i, selector.i, deviceBrowser.i, removedDevice.i, moreGoing.i)
  Debug "device removed in browser..."
  MessageRequester("","")
  ;CocoaMessage(0, removedDevice, "requestCloseSession")
  ;CocoaMessage(0, removedDevice, "setDelegate:", 0)
  ;RemoveGadgetItem(#scannerList, selectedScanner - 1)
  ;ReDim scanners(ArraySize(scanners()) - 1)
EndProcedure   ;IMA!
Removing even 1 scanner causes the crash. I still don't know why. :?
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: A question about Delegates

Post by Shardik »

When reporting problems with example codes it is often important that you post your MacOS version, PB version and whether you use the x86 or x64 PB compiler.

On MacOS 10.6.8 (Snow Leopard) with PB 5.44 x86 in ASCII mode I was able to reproduce your IMA when powering off my scanner while running the linked example code. I was able to solve this problem by adding

Code: Select all

  If activeScanner = removedDevice
    activeScanner = 0
  EndIf
at the end of procedure didRemoveDeviceInBrowser() and changing

Code: Select all

CocoaMessage(0, activeScanner, "release")
to

Code: Select all

If activeScanner
  CocoaMessage(0, activeScanner, "release")
EndIf
at the end of the example code.
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: A question about Delegates

Post by coder14 »

Shardik wrote:When reporting problems with example codes it is often important that you post your MacOS version, PB version and whether you use the x86 or x64 PB compiler.

On MacOS 10.6.8 (Snow Leopard) with PB 5.44 x86 in ASCII mode I was able to reproduce your IMA when powering off my scanner while running the linked example code. I was able to solve this problem by adding

Code: Select all

  If activeScanner = removedDevice
    activeScanner = 0
  EndIf
at the end of procedure didRemoveDeviceInBrowser() and changing

Code: Select all

CocoaMessage(0, activeScanner, "release")
to

Code: Select all

If activeScanner
  CocoaMessage(0, activeScanner, "release")
EndIf
at the end of the example code.
Sorry about that. My Mac is running Lion and I have used all x64 versions of PB 5.2, 5.4, 5.44 and 5.51. Now I tried it with 5.4 x86 ASCII but it still crashes.

Thank you so much for your code but that fails too. When the program starts there is no active scanner selected and it still crashes when a scanner is powered off. Sorry. :oops:

I borrowed a 2nd scanner from a neighbor and even if one scanner is still connected it crashes when any one is removed. :shock:

The only clue I have been able to get is that it does not crash with the messagebox AND the scanner is powered on again BEFORE dismissing the messagebox. I think it has something to do with losing that scanner that causes the IMA. :?:

The only thing connecting the scanners with the PB program is the delegate and it is released in the procedure. So I cannot figure out what is causing the crash. My original though about needing a separate delegate for the device browser is shot now as it still crashes when a scanner still remains connected.

:idea: An idea: is it correct that the device browser and the scanners all use the same delegate? Maybe the device browser needs one delegate and the scanners need different individual delegates? (I know, I know. Stupid idea! :lol: )
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: A question about Delegates

Post by coder14 »

Is this:

Code: Select all

dClass = objc_allocateClassPair(objc_lookUpClass("NSObject"), "PB_Delegate", 0)
the same as this:

Code: Select all

d = CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
dClass = CocoaMessage(0, d, "class")
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: A question about Delegates

Post by TI-994A »

coder14 wrote:Is this:

Code: Select all

dClass = objc_allocateClassPair(objc_lookUpClass("NSObject"), "PB_Delegate", 0)
the same as this:

Code: Select all

d = CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
dClass = CocoaMessage(0, d, "class")
Not quite. The first creates a new class named PB_Delegate, while the second instantiates the sharedApplication object and class, which are the same ones used by the PureBasic application. Nonetheless, in the contextual implementation, they both perform the same function.

However, the reported IMA-issue during device-removal has nothing to do with these delegations. As Shardik had suggested, de-initialising the activeScanner variable, should arrest the issue:

Code: Select all

ProcedureC didRemoveDeviceInBrowser(object.i, selector.i, deviceBrowser.i, removedDevice.i, moreGoing.i)
  Debug "device removed in browser..."
  CocoaMessage(0, removedDevice, "requestCloseSession")
  CocoaMessage(0, removedDevice, "setDelegate:", 0)
  RemoveGadgetItem(#scannerList, selectedScanner - 1)
  ReDim scanners(ArraySize(scanners()) - 1)
  If removedDevice = activeScanner
    activeScanner = 0
  EndIf
EndProcedure
Hope it works for you. :wink:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: A question about Delegates

Post by Shardik »

coder14,

for your conveniance I have added my proposed changes to the most recent code example.

Sorry, but I can't reproduce your error. I have tested the modified code example on MacOS 10.6.8 (Snow Leopard) with PB 5.44 x86 in ASCII and Unicode mode and I even took yesterday evening the effort to install a new MacOS 10.7.5 (Lion) with Xcode 4.6.3. And even on Lion the code example ran successfully without any problems with PB 5.44 x86 and x64 in both ASCII and Unicode mode. So unfortunately I can't reproduce your problem anymore...
coder14 wrote:When the program starts there is no active scanner selected and it still crashes when a scanner is powered off.
You have to select the scanner by double clicking the scanner entry on the left side.

As soon as your scanner is detected, the program displays
Scanner wrote:device found and added...
Then you have to perform a double click onto the scanner and for my Epson Stylus CX6600 the program displays
Scanner wrote:session opened... errors: 0
selected device is ready...
Functional unit types:
- Flatbed
functional unit selected: 6677264
MeasurementUnit set to Inches
scanArea set to (0,0)-(10,10)
functional unit selected... errors: 6686016
Then you may click onto the "SCAN" button to start the scan operation. The program should display
Scanner wrote: initiating scan...
scanned to file://localhost/Users/Shardik/Scans/MyScan.png
scan completed... errors: 0
If the scan operation is terminated without starting the scan operation, chances are high that you have forgotten to change the path to your scan folder in procedure deviceDidBecomeReady() which is the path to my private scan folder... :wink:
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: A question about Delegates

Post by coder14 »

Shardik wrote:coder14,

for your conveniance I have added my proposed changes to the most recent code example.

Sorry, but I can't reproduce your error. I have tested the modified code example on MacOS 10.6.8 (Snow Leopard) with PB 5.44 x86 in ASCII and Unicode mode and I even took yesterday evening the effort to install a new MacOS 10.7.5 (Lion) with Xcode 4.6.3. And even on Lion the code example ran successfully without any problems with PB 5.44 x86 and x64 in both ASCII and Unicode mode. So unfortunately I can't reproduce your problem anymore...
coder14 wrote:When the program starts there is no active scanner selected and it still crashes when a scanner is powered off.
You have to select the scanner by double clicking the scanner entry on the left side.

As soon as your scanner is detected, the program displays
Scanner wrote:device found and added...
Then you have to perform a double click onto the scanner and for my Epson Stylus CX6600 the program displays
Scanner wrote:session opened... errors: 0
selected device is ready...
Functional unit types:
- Flatbed
functional unit selected: 6677264
MeasurementUnit set to Inches
scanArea set to (0,0)-(10,10)
functional unit selected... errors: 6686016
Then you may click onto the "SCAN" button to start the scan operation. The program should display
Scanner wrote: initiating scan...
scanned to file://localhost/Users/Shardik/Scans/MyScan.png
scan completed... errors: 0
If the scan operation is terminated without starting the scan operation, chances are high that you have forgotten to change the path to your scan folder in procedure deviceDidBecomeReady() which is the path to my private scan folder... :wink:
Thanks Shardik. No more crashing when the devices are removed. My error was to comment out ALL OTHER lines in the didRemoveDevice procedure when adding your "if activeScanner" condition. (btw - why two "didRemoveDevice" procs? one is never called. :?: )

http://www.purebasic.fr/english/viewtop ... 42#p499142

Thank you for pointing that out TI-994A. And thanks for explaining the delegate thing to me. :D

One more thing is that the folder was yours. :oops: But also that my scanner is a flatbed and the ADF was selected. I changed those and all works now.

You guys are great! :D :D :D
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: A question about Delegates [RESOLVED]

Post by Shardik »

coder14 wrote:(btw - why two "didRemoveDevice" procs? one is never called. :?: )
That's not true. In the code example you will find two different procedures: didRemoveDevice() and didRemoveDeviceInBrowser(). And they are indeed both called: just start your scanner and the program and then close the program. You should observe the following debug output (tested with MacOS 10.12.2 "Sierra" with PB 5.44):
Debug output wrote:device found and added...
device removed
device removed in browser...
I am glad that TI-994A's code example is now working for you! :wink:
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: A question about Delegates [RESOLVED]

Post by coder14 »

Shardik wrote:That's not true. In the code example you will find two different procedures: didRemoveDevice() and didRemoveDeviceInBrowser(). And they are indeed both called: just start your scanner and the program and then close the program. You should observe the following debug output (tested with MacOS 10.12.2 "Sierra" with PB 5.44):
Debug output wrote:device found and added...
device removed
device removed in browser...
I am glad that TI-994A's code example is now working for you! :wink:
I'm on Lion and I tried all combinations but the "device removed" proc is never fired. But I don't think it will be a problem? :shock:

You guys helped a lot but TI-994A pointed out your solution to me and my mistake. :oops:

Thanks both of you! :D
Post Reply