[fpc-devel] Creating a Cocoa Application

Felipe Monteiro de Carvalho felipemonteiro.carvalho at gmail.com
Sun Jan 6 19:31:47 CET 2008


> Without this supportyou must create the NSString at run time from a
> regular constant string. Regarding when you need an autorelease pool
> and when not (and how to create it), see e.g. http://www.otierney.net/objective-c.html#autorelease

Ok, pretty excelent so far =) I can already show the status panel.

About the autorelease pool, I'm pretty sure I need one, and I think
there is something wrong with my code which should create it.

//  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  NSAutoreleasePoolId := objc_getClass(PChar(Str_NSAutoreleasePool));
  pool := objc_msgSend(NSAutoreleasePoolId,
sel_registerName(PChar(Str_alloc)), []);
  objc_msgSend(NSAutoreleasePoolId, sel_registerName(PChar(Str_init)), []);

On the second call to msgSend where should I declare that I am calling
a method from a already created object? I mean, the typical "self"
pointer is missing.

The first argument should be the "self" pointer according to the docs,
but if I set it to pool instead of NSAutoreleasePoolID I get a crash.
Also I can't consiliate it expecting a generic ID from the class with
it being the "self" pointer.

My new code:

program cocoamsgbox;

{$mode objfpc}{$H+}

uses
  objc, FPCMacOSAll, appkit;

const
  Str_NSAutoreleasePool = 'NSAutoreleasePool';
  Str_alloc = 'alloc';
  Str_init = 'init';
  Str_release = 'release';
  Str_Panel_Title = 'This is the title';
  Str_Panel_Message = 'This is the message';
var
  { classes }
  NSAutoreleasePoolId: objc.id;
  { objects }
  pool: objc.id;
  { strings }
  CFTitle, CFMessage: CFStringRef;
begin
//  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  NSAutoreleasePoolId := objc_getClass(PChar(Str_NSAutoreleasePool));
  pool := objc_msgSend(NSAutoreleasePoolId,
sel_registerName(PChar(Str_alloc)), []);
  objc_msgSend(NSAutoreleasePoolId, sel_registerName(PChar(Str_init)), []);

  NSApplicationLoad();
  { uses a default "OK" button and no alternate buttons }
//    NSRunAlertPanel(@"This is the title", @"This is the message",
nil, nil, nil);
  CFTitle := CFStringCreateWithCString(nil, PChar(Str_Panel_Title),
kCFStringEncodingUTF8);
  CFMessage := CFStringCreateWithCString(nil,
PChar(Str_Panel_Message), kCFStringEncodingUTF8);

  NSRunAlertPanel(CFTitle, CFMessage, nil, nil, nil, []);

  CFRelease(CFTitle);
  CFRelease(CFMessage);

//  [pool release];
  pool := objc_msgSend(NSAutoreleasePoolId,
sel_registerName(PChar(Str_release)), []);
end.

thanks,
-- 
Felipe Monteiro de Carvalho



More information about the fpc-devel mailing list