<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://www.newlc.com" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>NewLC - TPtrC and destructor - Comments</title>
 <link>http://www.newlc.com/topic-16924</link>
 <description>Comments for &quot;TPtrC and destructor&quot;</description>
 <language>en</language>
<item>
 <title>Re: TPtrC and destructor</title>
 <link>http://www.newlc.com/topic-16924#comment-36119</link>
 <description>&lt;div class=&quot;smf-content&quot;&gt;Yes, just return the HBufC* so you transfer ownership, &lt;br /&gt;and then call -&amp;gt;Des() on it whenever you &amp;quot;need&amp;quot; it as a TPtr, or do the TPtrC(buf-&amp;gt;Ptr(),buf-&amp;gt;Length()) thing whenever you need it as a TPtrC, but DO NOT do that in the function allocation the HBufC like you do now.&lt;br /&gt;&lt;br /&gt;As other says, it might work, but it is not the right way to do it, and will create the problems you outline in your first post.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;</description>
 <pubDate>Thu, 30 Nov 2006 13:00:58 +0100</pubDate>
 <dc:creator>alh</dc:creator>
 <guid isPermaLink="false">comment 36119 at http://www.newlc.com</guid>
</item>
<item>
 <title>Re: TPtrC and destructor</title>
 <link>http://www.newlc.com/topic-16924#comment-36118</link>
 <description>&lt;div class=&quot;smf-content&quot;&gt;Yes but when &lt;i&gt;ownership is transferred&lt;/i&gt; i.e. a CBase derived class&amp;#039;s NewLC() or in this example if the HBufC* is pushed to the cleanup stack and its returend and ownership is transferred.&lt;br /&gt;&lt;br /&gt;You listed transferring ownership of the HBufC as your 2nd option, implying it wasn&amp;#039;t being transferred in the first option. Maybe that&amp;#039;s not what you meant so I wanted to clarify in case any readers thought you were saying ownership wasn&amp;#039;t transferred in the fist option, only in the 2nd.&lt;/div&gt;</description>
 <pubDate>Wed, 29 Nov 2006 20:47:17 +0100</pubDate>
 <dc:creator>NumptyAlert</dc:creator>
 <guid isPermaLink="false">comment 36118 at http://www.newlc.com</guid>
</item>
<item>
 <title>Re: TPtrC and destructor</title>
 <link>http://www.newlc.com/topic-16924#comment-36117</link>
 <description>&lt;div class=&quot;smf-content&quot;&gt;&lt;div class=&quot;quoteheader&quot;&gt;Quote from: NumptyAlert&lt;/div&gt;&lt;div class=&quot;quote&quot;&gt;Unless I&amp;#039;ve misunderstoof your posting you can&amp;#039;t have one function push something to the cleanupstack and then expect the caller to call PopAndDestroyjust because the function it calls has a C in its name. What if FunctionLC() pushed 2 things, or 3 things or 100 things? How does the calling function know what to do?&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;But isn&amp;#039;t the XxxLC naming convention just for this? Function &lt;b&gt;L&lt;/b&gt;eaves and places something on the &lt;b&gt;C&lt;/b&gt;leanup stack for the caller to take away&amp;nbsp; -- &lt;b&gt;LC&lt;/b&gt;.&lt;br /&gt;&lt;br /&gt;And implicitly (at least I haven&amp;#039;t seen any explicit discussion on this) this (LC) always means that there is &lt;b&gt;one and only one thing&lt;/b&gt; on the cleanupstack you are supposed to pop (and usually destroy).&lt;/div&gt;</description>
 <pubDate>Wed, 29 Nov 2006 20:02:14 +0100</pubDate>
 <dc:creator>Andreas</dc:creator>
 <guid isPermaLink="false">comment 36117 at http://www.newlc.com</guid>
</item>
<item>
 <title>Re: TPtrC and destructor</title>
 <link>http://www.newlc.com/topic-16924#comment-36116</link>
 <description>&lt;div class=&quot;smf-content&quot;&gt;You can&amp;#039;t return a TPtrC to data that has to be destroyed.&lt;br /&gt;&lt;br /&gt;Your code will work (although just have &lt;i&gt;return *copy&lt;/i&gt;) but of course copy has to be deleted at some point. &lt;br /&gt;&lt;br /&gt;So either you&lt;br /&gt;- don&amp;#039;t destroy the CRichText until the calling code has used the TPtrC&lt;br /&gt;- make the function return a HBufC* instead of a TPtrC and thus transfer ownership of the HBufC to the calling code so it is its responsiblity to delete it.&lt;br /&gt;&lt;br /&gt;You say the data has to be returned as a TPtrC, almost certainly it doesn&amp;#039;t, you may just think that it does.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&amp;gt;&amp;gt;One option is to push the created HBufC into the cleanupstack and rename your method to functionLC, telling the caller that there is something to be cleaned up after calling this method.&lt;br /&gt;&lt;br /&gt;Unless I&amp;#039;ve misunderstoof your posting you can&amp;#039;t have one function push something to the cleanupstack and then expect the caller to call PopAndDestroyjust because the function it calls has a C in its name. What if FunctionLC() pushed 2 things, or 3 things or 100 things? How does the calling function know what to do?&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;</description>
 <pubDate>Wed, 29 Nov 2006 19:32:10 +0100</pubDate>
 <dc:creator>NumptyAlert</dc:creator>
 <guid isPermaLink="false">comment 36116 at http://www.newlc.com</guid>
</item>
<item>
 <title>Re: TPtrC and destructor</title>
 <link>http://www.newlc.com/topic-16924#comment-36115</link>
 <description>&lt;div class=&quot;smf-content&quot;&gt;One option is to push the created HBufC into the cleanupstack and rename your method to functionLC, telling the caller that there is something to be cleaned up after calling this method.&lt;br /&gt;&lt;br /&gt;Another is to return the HBufC, renaming the method to functionL, and documenting that the caller is responsible for deleting the returned pointer.&lt;/div&gt;</description>
 <pubDate>Wed, 29 Nov 2006 19:23:37 +0100</pubDate>
 <dc:creator>Andreas</dc:creator>
 <guid isPermaLink="false">comment 36115 at http://www.newlc.com</guid>
</item>
<item>
 <title>Re: TPtrC and destructor</title>
 <link>http://www.newlc.com/topic-16924#comment-36114</link>
 <description>&lt;div class=&quot;smf-content&quot;&gt;Thanks...&lt;br /&gt;the situation is that I have a CRichText,in a function, that has to destroyed and the textual data in it have to be returned as TPtrC.&lt;br /&gt;This is the code :&lt;br /&gt;&lt;br /&gt;TPtrC function()&lt;br /&gt;{&lt;br /&gt;... // richtext is a CRichText, just pushed in the cleanupstack&lt;br /&gt;...&lt;br /&gt;...&lt;br /&gt;HBufC* copy=richText-&amp;gt;Read(0).Alloc();&lt;br /&gt;CleanupStack::PopAndDestroy( richText );&lt;br /&gt;&lt;br /&gt;return TPtrC(copy-&amp;gt;Ptr(),copy-&amp;gt;Length());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;It works but IS THERE A BETTER WAY to do it ??&lt;/div&gt;</description>
 <pubDate>Wed, 29 Nov 2006 19:18:41 +0100</pubDate>
 <dc:creator>keanu</dc:creator>
 <guid isPermaLink="false">comment 36114 at http://www.newlc.com</guid>
</item>
<item>
 <title>Re: TPtrC and destructor</title>
 <link>http://www.newlc.com/topic-16924#comment-36113</link>
 <description>&lt;div class=&quot;smf-content&quot;&gt;A TPtrC points to data &lt;i&gt;that is already existing&lt;/i&gt;, and thus is not responsible for either its creation nor its deletion (if either of those are necessary).&lt;/div&gt;</description>
 <pubDate>Wed, 29 Nov 2006 19:04:26 +0100</pubDate>
 <dc:creator>NumptyAlert</dc:creator>
 <guid isPermaLink="false">comment 36113 at http://www.newlc.com</guid>
</item>
<item>
 <title>TPtrC and destructor</title>
 <link>http://www.newlc.com/topic-16924</link>
 <description>&lt;div class=&quot;smf-content&quot;&gt;&lt;/div&gt;&lt;p&gt;&lt;a href=&quot;http://www.newlc.com/topic-16924&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://www.newlc.com/topic-16924#comments</comments>
 <category domain="http://www.newlc.com/forums/symbian-c">Symbian C++</category>
 <pubDate>Wed, 29 Nov 2006 18:07:49 +0100</pubDate>
 <dc:creator>keanu</dc:creator>
 <guid isPermaLink="false">16501 at http://www.newlc.com</guid>
</item>
</channel>
</rss>
