Deallocating cocoa distant objects.
Deallocating cocoa distant objects.
- Subject: Deallocating cocoa distant objects.
- From: Santosh Sinha <email@hidden>
- Date: Fri, 22 May 2009 02:31:53 +0530
Hi List.
I need your help with how to deallocate an object which is created in
a server process A from a client process B.
In the project as we can see in the NSLog once the MyClass object is
created and returned to the client, its retain count becomes 3. This
causes the MyClass object to be refrained from getting deallocated
even after releaseMyself is called.
Also, in the function call setString:(MutableString**). I think I am
doing straightforward in client by allocating it in Client, setting
its content in Server and then deallocating it in Client after I have
used it but the release of the allocated string in the client results
in a crash when the local release pool is popped out.
Thanks
Santosh
//
***************************************************************************************************************************************************************************************************************
//Protocols.h
@protocol ICServer <NSObject>
-(HRESULT) createmMyClass:(id *)outInterface;
@end
@protocol IMyClass <NSObject>
-(HRESULT) releaseMyself;
-(HRESULT) getRetainCount:(long*)outRetainCount;
-(HRESULT) setString:(NSMutableString**)outString;
@end
//
***************************************************************************************************************************************************************************************************************
Client Code:
// Client.h
#import <Cocoa/Cocoa.h>
#import <CoreFoundation/CFPluginCOM.h>
#import "Protocols.h"
@interface Client : NSObject {
id <IMyClass> m_MyClassObj;
id m_RemoteObjectProxy;}
-(void) testRemoteObject;
-(void) deleteMyClass;
@end
//
***************************************************************************************************************************************************************************************************************
// Client.m
#import "Client.h"
@implementation Client
- (id) init{
if (self = [super init]){
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
m_RemoteObjectProxy = nil;
m_MyClassObj = nil;
if(!m_RemoteObjectProxy){
m_RemoteObjectProxy = [[NSConnection
rootProxyForConnectionWithRegisteredName: IDS_SERVER_CXINPROC host:
nil] retain];
[m_RemoteObjectProxy setProtocolForProxy:@protocol(ICServer)];}
if(nil == m_RemoteObjectProxy){
NSLog(@"Error registering with server");}
else{
NSLog(@"Connection with server is successfully established."); }
[pool release];}
return self; }
-(void) dealloc{
if (m_RemoteObjectProxy)
[m_RemoteObjectProxy release];
[super dealloc];}
-(void) testRemoteObject{
HRESULT hRes = E_FAIL;
long retainCt = -1;
NSMutableString* str = [[NSMutableString alloc] init];
NSMutableString* myStr = [[NSMutableString alloc] init];
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
if (m_RemoteObjectProxy)
hRes = [m_RemoteObjectProxy createmMyClass:&m_MyClassObj];
if ( !SUCCEEDED(hRes) )
[str setString:@"\n0 Myclass objects created."];
else{
hRes = [m_MyClassObj getRetainCount:&retainCt];
if (SUCCEEDED(hRes))
[str appendString:[NSString stringWithFormat:@"\nRetainCount of
Myclass is %d",retainCt]];
else
[str appendString:@"\nRetain count of Myclass is not obtained"];
hRes = [m_MyClassObj setString:&myStr];
if (SUCCEEDED(hRes))
[str appendString:[NSString stringWithFormat:@"\nString is set in
server with %@",myStr]]; }
[str release];
[pool release];}
-(void) deleteMyClass{
HRESULT hRes = E_FAIL;
long retainCt = -1;
NSMutableString* str = [[NSMutableString alloc] init];
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
if (m_RemoteObjectProxy){
if (m_MyClassObj){
hRes = [m_MyClassObj releaseMyself];
if (SUCCEEDED(hRes)){
[str appendString:@"\n Myclass object deleted successfully"];}
else
[str appendString:@"Error in deleting Myclass object"];}}
[str release];
[pool release];}
@end
//
***************************************************************************************************************************************************************************************************************
// main.m
#import <Cocoa/Cocoa.h>
#import "Client.h"
int main(int argc, char *argv[]){
Client* client = [[Client alloc] init];
[client testRemoteObject];
[client deleteMyClass];
[client testRemoteObject];
return NSApplicationMain(argc, (const char **) argv);}
Server Code:
//
***************************************************************************************************************************************************************************************************************
// ServerProxy.h
#import <Cocoa/Cocoa.h>
#import <CoreFoundation/CFPluginCOM.h>
#import "Protocols.h"
@interface ServerProxy : NSObject <ICServer>{ }
@end
//
***************************************************************************************************************************************************************************************************************
// ServerProxy.m
#import "ServerProxy.h"
#import "MyClass.h"
@implementation ServerProxy
-(void)startAsServer{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSConnection *defaultConnection = [NSConnection defaultConnection];
[defaultConnection setRootObject:self];
if ([defaultConnection registerName:IDS_SERVER_CXINPROC] == NO){
NSLog(@"Error registering server");}
[pool release];}
-(id)init{
if ( self = [super init] ){
[self startAsServer];}
return self;}
-(HRESULT) createmMyClass:(id *)outInterface{
MyClass* obj = [[MyClass alloc] initWithModule:self];
*outInterface = obj;
return S_OK;}
@end
//
***************************************************************************************************************************************************************************************************************
// main.cp
#include <Carbon/Carbon.h>
#include <syslog.h>
#import "ServerProxy.h"
#define IDS_MSG_PORT_SERVER CFSTR("ServerMsgPort")
CFDataRef messagePortListenerProc (CFMessagePortRef local, SInt32
msgid, CFDataRef data, void *info){
return data;}
//--------------------------------------------------------------------------------------------
int main(int argc, char* argv[]){
CFMessagePortContext context = {0};
CFMessagePortRef remotePort =
CFMessagePortCreateRemote(kCFAllocatorDefault, IDS_MSG_PORT_SERVER);
if (remotePort != NULL){
CFMessagePortInvalidate(remotePort);
CFRelease(remotePort);
syslog(0, "One instance of Server is already runnibg ");
return -1;}
bool done = false;
bool runLoopExit = false;
ServerProxy* proxyObject = [[ServerProxy alloc] init];
CFMessagePortRef localPort =
CFMessagePortCreateLocal(kCFAllocatorDefault, IDS_MSG_PORT_SERVER,
messagePortListenerProc, &context, false);
CFRunLoopSourceRef runLoopSource =
CFMessagePortCreateRunLoopSource(kCFAllocatorDefault, localPort, 0);
CFRunLoopRef runLoopRef = CFRunLoopGetCurrent();
CFRunLoopAddSource(runLoopRef, runLoopSource, kCFRunLoopDefaultMode);
do{
// Start the run loop but return after each source is handled.
SInt32 result = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1,
runLoopExit);
// If a source explicitly stopped the run loop, or if there are no
// sources or timers, go ahead and exit.
if ((result == kCFRunLoopRunStopped) || (result ==
kCFRunLoopRunFinished))
done = true;
// Check for any other exit conditions here and set the
// done variable as needed.}
while (!done);
if (localPort)
CFMessagePortInvalidate(localPort);
CFRunLoopRemoveSource(runLoopRef, runLoopSource,
kCFRunLoopDefaultMode);
if (localPort)
CFRelease(localPort);
if (runLoopSource)
CFRelease(runLoopSource);
if (runLoopRef)
CFRelease(runLoopRef);
[proxyObject release];
return 0;}
//
***************************************************************************************************************************************************************************************************************
// MyClass.h
#import <Cocoa/Cocoa.h>
#import "ServerProxy.h"
@interface MyClass : NSObject <IMyClass>{
ServerProxy* m_Module; }
-(id) initWithModule:(ServerProxy*)module;
@end
//
***************************************************************************************************************************************************************************************************************
// MyClass.m
#import "MyClass.h"
@implementation MyClass
-(id) initWithModule:(ServerProxy*)module{
if (self = [super init]){
m_Module = [module retain];}
return self;}
-(void) dealloc{
[m_Module release];
[super dealloc];}
-(HRESULT) releaseMyself{
[self release];
return S_OK;}
-(HRESULT) getRetainCount:(long*)outRetainCount{
*outRetainCount = [self retainCount];
return S_OK;}
-(HRESULT) setString:(NSMutableString**)outString{
NSString* string = [[NSString alloc] initWithString:@"String
Initialized in MyClass"];
[*outString setString:string];
[string release];
return S_OK;}
@end
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden