Lazarus

Programming => Widgetset => Cocoa => Topic started by: maxnd on May 01, 2020, 07:19:23 pm

Title: Set a variable in cocoa
Post by: maxnd on May 01, 2020, 07:19:23 pm
Hi, I cannot compile this code
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. {$modeswitch objectivec1}
  3. interface
  4. uses
  5.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, CocoaAll, CocoaUtils;
  6.  
  7. [...]
  8.  
  9. procedure Sound;
  10.   var mySound: NSSound;
  11. begin
  12.   mySound.initWithContentsOfFile_byReference(
  13.     NSStringUtf8('test.waw'), True);
  14. end;
  15.  

It raises the error
Code: Pascal  [Select][+][-]
  1. unit1.pas(9,1) Error: Internal error 200609171

on the procedure line. Any idea to fix this?
Title: Re: Set a variable in cocoa
Post by: skalogryz on May 01, 2020, 07:41:45 pm
Is there a chance you're using other than dwarf 2 debugging symbols?
Title: Re: Set a variable in cocoa
Post by: maxnd on May 01, 2020, 08:10:00 pm
Great, this fixed the problem. I was using dwarf 3.
Thanks!
Title: Re: Set a variable in cocoa
Post by: maxnd on May 01, 2020, 08:46:23 pm
Actually now the code is compiled, but I cannot assign a variable to NSSound. The code

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.   var mySound: NSSound;
  3. begin
  4.     mySound.initWithContentsOfFile_byReference(
  5.       NSStringUtf8('test.waw'), True);
  6. end;
  7.  

raises the error

Code: Pascal  [Select][+][-]
  1.  Debugger stopped with reason: EXC_BAD_ACCESS

Any idea?
Title: Re: Set a variable in cocoa
Post by: skalogryz on May 01, 2020, 08:47:19 pm
"init" should be preceded with alloc.
Code: Pascal  [Select][+][-]
  1.  mySound := NSSound.alloc.initWithContentsOfFile_byReference(
  2.       NSStringUtf8('test.waw'), True); // should it be test.wav instead of .waw?
or a longer version
Code: Pascal  [Select][+][-]
  1.   mySound := NSSound.alloc;  
  2.   mySound := mySound.initWithContentsOfFile_byReference(
  3.       NSStringUtf8('test.waw'), True); // should it be test.wav instead of .waw?
  4.   mySound.play;
keep in mind that you HAVE to use the result of initXXX method as the actual object.
This is ObjC allocation/initialization rule.

Keep in mind that calling "alloc" requires you to use either "autorelease" or an explicit "release" at some point.
"autorelease" will deallocate the object as soon as it not need by Cocoa. (generally that means at the end of the current system event handling)
"release" will "deallocate" the object when you call it.

Yet, since all Cocoa object are Cocoa-reference counted, the term "deallocation" should not be taken literally. It's "deallocated" only from your own code perspective.
Title: Re: Set a variable in cocoa
Post by: maxnd on May 01, 2020, 08:55:09 pm
OK, now it works fine.
Thanks!
Title: Re: Set a variable in cocoa
Post by: skalogryz on May 01, 2020, 08:55:58 pm
I've edited my previous post and added information about deallocation.
Title: Re: Set a variable in cocoa
Post by: maxnd on May 01, 2020, 09:01:39 pm
Got it, thanks!
TinyPortal © 2005-2018