Dear Gary, The Help of Xcode says the returned C string is automatically freed just as a returned object would be released. That means I don't need to release or autorelease xmlStr?
Thanks, Bing On Sat, May 21, 2011 at 3:28 AM, Gary L. Wade <garyw...@desisoftsystems.com>wrote: > In your Xcode text window, hold down the option key and double click on > NSString. You will be given the option of viewing an overview of the > class. Click on the icon in the upper right side of the window that looks > like a book. This will show you a view of the documentation for NSString. > When looking at the documentation, click Command-F to do a search and > enter "const char *" or a substring of that until you find the answer you > want. > > Read the exceptions being thrown. Most likely it says something about > autorelease pools. Do the same thing for the class it mentions that you > did for NSString, albeit leaving out the particular point of searching for > NSString. > > On 05/20/2011 12:04 PM, "Bing Li" <lbl...@gmail.com> wrote: > > >Dear Evadne and all, > > > >I appreciate so much for your replies! > > > >You change the return value from (const char *) to (NSString *). In my > >system, since I need to send the XML to a server via BSD socket. I have to > >convert it to (const char *). > > > >But I got another problem. If autoreleasing xmlStr or release xmlStr > >before > >returning the (const char *) value, the system got exceptions. Why? > > > >For this problem, I have to remove the lines. But how to manage the memory > >of xmlStr? > > > >Best regards, > >Bing > > > >On Fri, May 20, 2011 at 11:19 PM, Evadne Wu > ><monocerosfu...@gmail.com>wrote: > > > >> Hi Bing, > >> > >> Re-factored this method a bit to try out some idea ‹ have a look? It¹s > >> almost certainly asking for trouble to pass C strings around if you¹re > >> working with Cocoa, but I am not sure of your scenario, so maybe I am > >> (what¹s assumed to almost always be true) wrong with returning an > >>NSString. > >> > >> > >> + (NSString *) signInMessageWithPeerKey:(NSString *)aKey name:(NSString > >> *)aName password:(NSString *)aPassword { > >> > >> NSParameterAssert(aKey && aName && aPassword); > >> > >> NSXMLNode *rootNode = [NSXMLNode > >> elementWithName:MessageConstants.XML.MESSAGE_ROOT children:[NSArray > >> arrayWithObjects: > >> > >> [NSXMLNode elementWithName:MessageConstants.XML.PEERKEY > >> stringValue:aKey], > >> [NSXMLNode elementWithName:MessageConstants.XML.PEERNAME > >> stringValue:aName], > >> [NSXMLNode elementWithName:MessageConstants.XML.PASSWORD > >> stringValue:aPassword], > >> > >> nil] attributes:nil]; > >> > >> NSXMLDocument *document = [[[NSXMLDocument alloc] > >> initWithRootElement:rootNode] autorelease]; > >> [document setVersion:MessageConstants.XML.XML_VERSION]; > >> [document setCharacterEncoding:MessageConstants.XML.XML_ENCODING]; > >> > >> return [[[NSString alloc] initWithData:[document > >> XMLDataWithOptions:NSXMLNodePrettyPrint] encoding:[document > >> characterEncoding]] autorelease]; > >> > >> } > >> > >> > >> -ev > >> > >> On May 20, 2011, at 22:43, Bing Li wrote: > >> > >> > Dear Jeff and all, > >> > > >> > I appreciate so much for your help! After reading the document from > >> > apple.com, I changed my code as follows. It must be fine. > >> > > >> > Best regards, > >> > Bing > >> > > >> > + (const char *) CreateSignInMessage: (NSString *)peerKey PN: > >>(NSString > >> > *)peerName PW:(NSString *)password > >> > { > >> > NSXMLElement *root = [NSXMLNode > >> > elementWithName:MessageConstants.XML.MESSAGE_ROOT]; > >> > NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] > >> > initWithRootElement:root]; > >> > [xmlDoc setVersion:MessageConstants.XML.XML_VERSION]; > >> > [xmlDoc > >>setCharacterEncoding:MessageConstants.XML.XML_ENCODING]; > >> > > >> > NSXMLElement *peerKeyElement = [NSXMLNode > >> > elementWithName:MessageConstants.XML.PEERKEY]; > >> > [root addChild:peerKeyElement]; > >> > [peerKeyElement addChild:[NSXMLNode > >>textWithStringValue:peerKey]]; > >> > > >> > NSXMLElement *peerNameElement = [NSXMLNode > >> > elementWithName:MessageConstants.XML.PEERNAME]; > >> > [root addChild:peerNameElement]; > >> > [peerNameElement addChild:[NSXMLNode > >> textWithStringValue:peerName]]; > >> > > >> > NSXMLElement *passwordElement = [NSXMLNode > >> > elementWithName:MessageConstants.XML.PASSWORD]; > >> > [root addChild:passwordElement]; > >> > [passwordElement addChild:[NSXMLNode > >> textWithStringValue:password]]; > >> > > >> > NSData *data = [xmlDoc > >>XMLDataWithOptions:NSXMLNodePrettyPrint]; > >> > NSString *xmlStr = [[NSString alloc] initWithData:data > >> > encoding:NSUTF8StringEncoding]; > >> > const char *xmlChar = [xmlStr UTF8String]; > >> > > >> > /* > >> > [passwordElement release]; > >> > [root release]; > >> > [xmlStr release]; > >> > [data release]; > >> > */ > >> > [xmlDoc release]; > >> > [xmlStr autorelease]; > >> > return xmlChar; > >> > } > >> > > >> > > >> > On Thu, May 19, 2011 at 4:23 PM, Jeffrey Walton <noloa...@gmail.com> > >> wrote: > >> > > >> >> On Wed, May 18, 2011 at 2:16 PM, Ken Thomases <k...@codeweavers.com> > >> wrote: > >> >>> On May 18, 2011, at 12:55 PM, Bing Li wrote: > >> >>> > >> >>>> NSXMLElement *root = [NSXMLNode > >>elementWithName:"MessageRoot"]; > >> >>> > >> >>> As per Cocoa's memory management conventions, you don't not own the > >> >> object returned by -[NSXMLNode elementWithName:]. You have not > >>invoked > >> a > >> >> method whose name contains "alloc", "new", or "copy" or which is > >> explicitly > >> >> documented as giving its caller ownership rights and > >>responsibilities. > >> >>> > >> >>>> NSXMLElement *peerKeyElement = [NSXMLNode > >> >> elementWithName:"PeerKey"]; > >> >>> > >> >>> Same here. > >> >>> > >> >>>> NSXMLElement *peerNameElement = [NSXMLNode > >> >> elementWithName:"PeerName"]; > >> >>> > >> >>> Same here. > >> >>> > >> >>>> NSXMLElement *passwordElement = [NSXMLNode > >> >> elementWithName:"Password"]; > >> >>> > >> >>> Same here. > >> >>> > >> >>>> NSData *data = [xmlDoc > >>XMLDataWithOptions:NSXMLNodePrettyPrint]; > >> >>> > >> >>> Same here. > >> >>> > >> >>>> [passwordElement release]; > >> >>>> // [peerNameElement release]; > >> >>>> // [peerKeyElement release]; > >> >>>> [root release]; > >> >>>> [xmlDoc release]; > >> >>>> [xmlStr release]; > >> >>>> [data release]; > >> >>> > >> >>> Many of these releases are wrong, not just the ones you have > >>commented > >> >> out. > >> >>> Of these, you only own xmlDoc and xmlStr, so those are the only ones > >> you > >> >> are > >> >>> entitled to release. If you didn't happen to get exceptions from > >> >> releasing the > >> >>> others, it was an unhappy accident. (Unhappy because it hid your > >>bug. > >> >> It is > >> >>> always better for bugs to be found early.) > >> >> If it helps Bing (and coming from someone who frequently needs the > >> >> same sort of help): (1) Run you code using Instruments, or (2) select > >> >> Executable -> Arguments and add NSZombieEnabled = YES. Both should > >> >> help you flush out the problems early (as Ken suggests). > >> >> > >> >>> Please review the Memory Management Programming Guide < > >> >> > >> > >> > http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Mem > >>oryMgmt/ > >> >. > >> >> It explains all of this. > >> >>> > >> >> Also grab a book or two. It will help you digest it. Nuremberg's book > >> >> would be a good choice since the doctor stays active on this list. > >> >> > >> >> Jeff > >> >> > >> > _______________________________________________ > >> > > >> > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > >> > > >> > 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: > >> > http://lists.apple.com/mailman/options/cocoa-dev/ev%40monoceroi.com > >> > > >> > This email sent to e...@monoceroi.com > >> > >> > >_______________________________________________ > > > >Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > > >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: > > > http://lists.apple.com/mailman/options/cocoa-dev/garywade%40desisoftsystem > >s.com > > > >This email sent to garyw...@desisoftsystems.com > > > _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com