A crash I cannot explain

I am trying to polish the NSURLSession framework I once created but never finished. Everything fine so far, except for I’m unable to build a delegate class. The NSURLSessionDelegate (like the datatask delegates) is retained by the session, so I wanted to build a separate class to avoid memory leaks that could occur if the Session would be its own delegate.
Like usual, I set up a NSObject subclass and added ObjC Runtime method replacements. Here’s an example:

methods.Append new TargetClassMethodHelper("URLSession:dataTask:didBecomeDownloadTask:", AddressOf impl_URLSessiondataTaskdidBecomeDownloadTask, "v@:@@@") mClassPtr = BuildTargetClass ("NSObject", "iOSLibURLSessionDel",methods)

(Code that works throughout the project)

That works, and the shared implementation looks like

Private Shared Sub impl_URLSessiondataTaskdidBecomeDownloadTask(pid as ptr, sel as ptr, session as Ptr, task as ptr, downloadtask as ptr) // dim ego as AppleURLSession= AppleURLSession.MakeFromPtr (session) // ego.informonURLSessiondataTaskdidBecomeDownloadTask (task, downloadtask) break #Pragma Unused pid #Pragma Unused Sel End Sub

The first two lines are commented out. They would usually call this method on Ego:

Attributes( hidden ) Sub informonURLSessiondataTaskdidBecomeDownloadTask(task as ptr, downloadtask as ptr) RaiseEvent DataTaskdidBecomeDownloadTask (new AppleURLSessionTask(task),new AppleURLSessionDownloadTask(downloadtask)) End Sub

Once I uncomment the first lines, the whole project crashes before it shows its first view, even if I define a new SessionDelegate in a button action event. No Xojo debugger or error. In Console I find an

error which is quite funny because the content of my global text variable FoundationLibname ist Foundation.framework, not FoundationLibname.Framework. There is no rename in the upper form and the error only appears if I try to activate the code lines in the implementation, though it is not executed. Similar code is used without problems throughout the whole project, but once I decomment the lines above, the project will not start even if it does nothing with NSURLSession framework.

Any ideas what might be causing this issue? The code is not even called and yet it crashes without notice?

EDIT: Needless to say: I can create a AppleURLSession with its constructors as a separate object, and its declares use the foundationlibname constant …

You might have single or double quotes in one of your constants or declares, similar to this:

Const FoundationLibname As String = "'Foundation.framework'" Declare Function Blablabla Lib "'FoundationLibName'" ()

That would explain it, but no: There’s only one constant definition in the whole project and it’s without single quotes. And even if I should have: Then the error would occur on every run attempt, not only if I unquote the two lines where nothing about Foundation is mentioned.

It is obvious that there is a problem with the substitution of FoundationLibname in one of the declares. Maybe you have a declare in the form of:

Declare Function Blablabla Lib "FoundationLibName.framework" ()

I can never exclude own stupidity, but I have searched the whole project and no, I haven’t. And still (sorry for being so obnoxious): if I would have, why does the program run without crash when I exclude the lines in the event implementation? I can have them somewhere else and no problem.
I have the feeling that somehow the compiler confuses a few things when I activate the code lines. Again, I cannot say it’s impossible I made a really stupid mistake, but I have checked a few hours now and don’t find anything suspicious.

Maybe because “soft declares” are evaluated only at runtime.

Oh boy, I found it. Thanks for supporting me, Eli! I was about to throw the towel on this thing.
I started to use external ObjC declares since they were introduced with 2015r3, and in this one I made a typo and entered a single quote instead the hashtag. I couldn’t find any reference to “’FoundationLibname.framework” because that was constructed by the framework. Should have searched for the name only, without framework.

Again, thx a lot, Eli!