Recent

Author Topic: Set a variable in cocoa  (Read 1269 times)

maxnd

  • Jr. Member
  • **
  • Posts: 65
Set a variable in cocoa
« 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?

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Set a variable in cocoa
« Reply #1 on: May 01, 2020, 07:41:45 pm »
Is there a chance you're using other than dwarf 2 debugging symbols?

maxnd

  • Jr. Member
  • **
  • Posts: 65
Re: Set a variable in cocoa
« Reply #2 on: May 01, 2020, 08:10:00 pm »
Great, this fixed the problem. I was using dwarf 3.
Thanks!

maxnd

  • Jr. Member
  • **
  • Posts: 65
Re: Set a variable in cocoa
« Reply #3 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?

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Set a variable in cocoa
« Reply #4 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.
« Last Edit: May 01, 2020, 08:55:30 pm by skalogryz »

maxnd

  • Jr. Member
  • **
  • Posts: 65
Re: Set a variable in cocoa
« Reply #5 on: May 01, 2020, 08:55:09 pm »
OK, now it works fine.
Thanks!

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Set a variable in cocoa
« Reply #6 on: May 01, 2020, 08:55:58 pm »
I've edited my previous post and added information about deallocation.

maxnd

  • Jr. Member
  • **
  • Posts: 65
Re: Set a variable in cocoa
« Reply #7 on: May 01, 2020, 09:01:39 pm »
Got it, thanks!

 

TinyPortal © 2005-2018