It is currently Mon Sep 25, 2017 4:16 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: A question about Delegates [RESOLVED]
PostPosted: Sun Dec 25, 2016 4:15 pm 
Offline
Enthusiast
Enthusiast

Joined: Tue Jun 21, 2011 10:39 am
Posts: 326
From this post: viewtopic.php?p=464631#p464631
Shardik wrote:
Code:
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:
; ----- 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.

Top
 Profile  
Reply with quote  
 Post subject: Re: A question about Delegates
PostPosted: Sun Dec 25, 2016 6:56 pm 
Offline
Enthusiast
Enthusiast

Joined: Tue Jun 21, 2011 10:39 am
Posts: 326
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:
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. :?


Top
 Profile  
Reply with quote  
 Post subject: Re: A question about Delegates
PostPosted: Mon Dec 26, 2016 10:36 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 1440
Location: Germany
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:
  If activeScanner = removedDevice
    activeScanner = 0
  EndIf
at the end of procedure didRemoveDeviceInBrowser() and changing
Code:
CocoaMessage(0, activeScanner, "release")
to
Code:
If activeScanner
  CocoaMessage(0, activeScanner, "release")
EndIf
at the end of the example code.


Top
 Profile  
Reply with quote  
 Post subject: Re: A question about Delegates
PostPosted: Tue Dec 27, 2016 4:25 am 
Offline
Enthusiast
Enthusiast

Joined: Tue Jun 21, 2011 10:39 am
Posts: 326
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:
  If activeScanner = removedDevice
    activeScanner = 0
  EndIf
at the end of procedure didRemoveDeviceInBrowser() and changing
Code:
CocoaMessage(0, activeScanner, "release")
to
Code:
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: )


Top
 Profile  
Reply with quote  
 Post subject: Re: A question about Delegates
PostPosted: Tue Dec 27, 2016 7:23 pm 
Offline
Enthusiast
Enthusiast

Joined: Tue Jun 21, 2011 10:39 am
Posts: 326
Is this:
Code:
dClass = objc_allocateClassPair(objc_lookUpClass("NSObject"), "PB_Delegate", 0)
the same as this:
Code:
d = CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
dClass = CocoaMessage(0, d, "class")


Top
 Profile  
Reply with quote  
 Post subject: Re: A question about Delegates
PostPosted: Wed Dec 28, 2016 8:03 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 19, 2011 3:47 am
Posts: 1948
coder14 wrote:
Is this:
Code:
dClass = objc_allocateClassPair(objc_lookUpClass("NSObject"), "PB_Delegate", 0)
the same as this:
Code:
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:
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 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!


Top
 Profile  
Reply with quote  
 Post subject: Re: A question about Delegates
PostPosted: Wed Dec 28, 2016 10:06 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 1440
Location: Germany
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:


Top
 Profile  
Reply with quote  
 Post subject: Re: A question about Delegates
PostPosted: Thu Dec 29, 2016 7:09 am 
Offline
Enthusiast
Enthusiast

Joined: Tue Jun 21, 2011 10:39 am
Posts: 326
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. :?: )

viewtopic.php?p=499142#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


Top
 Profile  
Reply with quote  
 Post subject: Re: A question about Delegates [RESOLVED]
PostPosted: Thu Dec 29, 2016 9:56 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 1440
Location: Germany
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:


Top
 Profile  
Reply with quote  
 Post subject: Re: A question about Delegates [RESOLVED]
PostPosted: Fri Dec 30, 2016 4:25 am 
Offline
Enthusiast
Enthusiast

Joined: Tue Jun 21, 2011 10:39 am
Posts: 326
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye