Why does this code crash Livecode ?

LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.

Moderators: LCMark, LCfraser

Post Reply
Simon Knight
Posts: 845
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Why does this code crash Livecode ?

Post by Simon Knight » Thu Feb 13, 2020 6:25 pm

Hi,

I have just written some LCB code that crashes Livecode. I needed to convert a string into an NSString object but for some reason I initially did not spot the built in conversion.

So I attempted to use these to handlers:

Code: Select all

-- Need to be able to create an NSString object
private foreign handler ObjC_NSStringAlloc() \
			returns objcRetainedID \
			binds to "objc:NSString.+alloc"

private foreign handler ObjC_NSStringInitWithString(in pObj as ObjcID, in pString as String) \
			returns objcID \
			binds to "objc:NSString.-initWithString:"
Crashed every time. Is it because I was attempting to pass a native string to the Objective-C compiler thingy?
best wishes
Skids

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

Re: Why does this code crash Livecode ?

Post by trevordevore » Thu Feb 13, 2020 6:34 pm

That is most likely the cause. A LiveCode String is not an Objective-C String. You need to pass an ObjcId.

So this:

Code: Select all

ObjC_NSStringInitWithString(in pObj as ObjcID, in pString as String)
Should be this:

Code: Select all

ObjC_NSStringInitWithString(in pObj as ObjcID, in pString as ObjcId)
And you would pass the result of StringToNSString(tLiveCodeString) as the parameter.

I looked up some code I have which creates an ObjC URL object from a string and I have this signature:

Code: Select all

private foreign handler ObjC_NSURLAlloc() \
        returns ObjcRetainedId binds to "objc:NSURL.+alloc"
private foreign handler ObjC_NSURLInitWithString(in pObj as ObjcRetainedId, in pUrl as ObjcId) \
        returns optional ObjcRetainedId binds to "objc:NSURL.-initWithString:"
Notice the use of ObjcRetainedId.
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

Simon Knight
Posts: 845
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Why does this code crash Livecode ?

Post by Simon Knight » Thu Feb 13, 2020 6:50 pm

When I think about it LCB almost had to provide a conversion routine to convert string to NSString as without it there would not be any way to pass strings in foreign handlers.
Notice the use of ObjcRetainedId.
Noticed but not understood. I thought I read that the +alloc should return an objcID and the -init should be passed the objcID and return a ObjcRetainedID. Or may be it was the other way round. Something to do with preventing an attempt to deallocate an object that has already been deallocated.

The bottom line is that it would be good if LCMark or Ali could publish some instructions, aimed at three year olds, that I could follow.
best wishes
Skids

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

Re: Why does this code crash Livecode ?

Post by trevordevore » Thu Feb 13, 2020 7:24 pm

Yes, it would be nice to have a lot of examples showing when to use ObjcId vs ObjcRetainedId.

In this case I *think* the reason is that you are taking ownership because you are allocating and initializing the object.
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

Simon Knight
Posts: 845
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Why does this code crash Livecode ?

Post by Simon Knight » Thu Feb 13, 2020 8:00 pm

I just found this on stack overflow:
https://stackoverflow.com/questions/657 ... bjective-c

So reading the comments an object occupies some memory and that area of memory/object has a reference count associated. When the reference count drops to zero the object is destroyed.

What is unclear, to me, is what LCB does . For example an init method increments the reference count. But the LCB documents describe ObjcID as "- an id with no implicit action on its reference count" . So does this mean that the reference count does not get incremented as this is the implied action of the init method?

ObjcRetainedID is described as "an id which is expected to already have been retained. (i.e. the caller or callee expects to receive it with +1 ref count)" . So does this mean that ObjcRetainedID should be used with every init?

Its all clear as mud.
best wishes
Skids

Post Reply

Return to “LiveCode Builder”