<?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 - HBufC and threads - Comments</title>
 <link>http://www.newlc.com/forum/hbufc-and-threads</link>
 <description>Comments for &quot;HBufC and threads&quot;</description>
 <language>en</language>
<item>
 <title>Re: HBufC and threads</title>
 <link>http://www.newlc.com/forum/hbufc-and-threads#comment-44323</link>
 <description>&lt;p&gt;I&#039;d really recommend you to try them fig7!&lt;br /&gt;
I havn&#039;t ever felt the need to use one of those horrible HBufC:s since I found out about them.&lt;br /&gt;
RBufs are generally very simple and efficient to use, and produce _alot_ nicer code imo.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;</description>
 <pubDate>Mon, 04 Feb 2008 09:55:23 +0100</pubDate>
 <dc:creator>alh</dc:creator>
 <guid isPermaLink="false">comment 44323 at http://www.newlc.com</guid>
</item>
<item>
 <title>Re: HBufC and threads</title>
 <link>http://www.newlc.com/forum/hbufc-and-threads#comment-44317</link>
 <description>&lt;p&gt;Yeah, the RBuf class was introduced because the HBufC class has a few issues (such as the one we&#039;ve discovered here, for example). However, I&#039;ve never really felt the need to start using RBuf instead. I think the phrase is &quot;better the devil you know...&quot;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;</description>
 <pubDate>Mon, 04 Feb 2008 00:34:35 +0100</pubDate>
 <dc:creator>fig7</dc:creator>
 <guid isPermaLink="false">comment 44317 at http://www.newlc.com</guid>
</item>
<item>
 <title>Re: HBufC and threads</title>
 <link>http://www.newlc.com/forum/hbufc-and-threads#comment-44315</link>
 <description>&lt;p&gt;Hmm, one idea just came to my mind  &lt;img src=&quot;/sites/all/modules/smileys/packs/example/cool.png&quot; title=&quot;Cool&quot; alt=&quot;Cool&quot; /&gt; , maybe I should use RBuf instead of HBufC  &lt;img src=&quot;/sites/all/modules/smileys/packs/example/evil.png&quot; title=&quot;Evil&quot; alt=&quot;Evil&quot; /&gt; &lt;/p&gt;</description>
 <pubDate>Mon, 04 Feb 2008 00:00:17 +0100</pubDate>
 <dc:creator>lman</dc:creator>
 <guid isPermaLink="false">comment 44315 at http://www.newlc.com</guid>
</item>
<item>
 <title>Re: HBufC and threads</title>
 <link>http://www.newlc.com/forum/hbufc-and-threads#comment-44097</link>
 <description>&lt;p&gt;Yep.. Its just a limitation of the Des function.&lt;/p&gt;

&lt;p&gt;The memory of the HBufC is accessible from the other thread, even if they don&#039;t share heaps.&lt;br /&gt;
If you&#039;d create the pointer descriptor in the same thread as where the HBufC is created, and then pass the pointer descriptor over the thread boundary, it will also work.&lt;br /&gt;
&lt;/p&gt;</description>
 <pubDate>Thu, 24 Jan 2008 09:17:27 +0100</pubDate>
 <dc:creator>alh</dc:creator>
 <guid isPermaLink="false">comment 44097 at http://www.newlc.com</guid>
</item>
<item>
 <title>Re: HBufC and threads</title>
 <link>http://www.newlc.com/forum/hbufc-and-threads#comment-44081</link>
 <description>&lt;p&gt;I think you can just use&lt;br /&gt;
&lt;code&gt;iSharedBuf = new(ELeave) TBuf&amp;lt;sizeYouWant&amp;gt;;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;instead of using HBufC, which as you&#039;ve discovered seems to be completely unusable if you&#039;ve got threads with their own heaps. You might also want to think about using RBuf, although I haven&#039;t really used it very much so I don&#039;t know for sure if that can be used with different threads (and heaps!)&lt;/p&gt;

&lt;p&gt;EDIT: Actually I&#039;ve added another bright idea. Can&#039;t you just create the TPtr when you create the HBufC?&lt;/p&gt;

&lt;p&gt;i.e. do the following:&lt;/p&gt;

&lt;p&gt;HBufC* iSharedBuf;&lt;br /&gt;
TPtr iSharedPtr;&lt;/p&gt;

&lt;p&gt;Thread 1:&lt;br /&gt;
iSharedBuf = HBufC::NewL(100);&lt;br /&gt;
iSharedPtr.Set(iSharedBuf-&gt;Des());&lt;/p&gt;

&lt;p&gt;Thread2:&lt;br /&gt;
iSharedPtr = someOtherBuf&lt;/p&gt;

&lt;p&gt;EDIT2: You&#039;d need to construct the TPtr with (NULL, 0) and then set it. I&#039;m fairly sure this would work. Actually there&#039;s no need to share the HBufC object with the other thread at all, just let the other thread do everything through the TPtr.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;</description>
 <pubDate>Wed, 23 Jan 2008 22:47:28 +0100</pubDate>
 <dc:creator>fig7</dc:creator>
 <guid isPermaLink="false">comment 44081 at http://www.newlc.com</guid>
</item>
<item>
 <title>Re: HBufC and threads</title>
 <link>http://www.newlc.com/forum/hbufc-and-threads#comment-44080</link>
 <description>&lt;p&gt;Des() method calls AllocLen() of RHeap class, but I will try to do this that way which you proposed in @up post&lt;/p&gt;

&lt;p&gt;Edit: Currently I&#039;m using TBuf&lt;600&gt; but I wanted to switch to HBufC which would optimize my program a little &lt;img src=&quot;/sites/all/modules/smileys/packs/example/smile.png&quot; title=&quot;Smiling&quot; alt=&quot;Smiling&quot; /&gt;&lt;/p&gt;</description>
 <pubDate>Wed, 23 Jan 2008 21:15:34 +0100</pubDate>
 <dc:creator>lman</dc:creator>
 <guid isPermaLink="false">comment 44080 at http://www.newlc.com</guid>
</item>
<item>
 <title>Re: HBufC and threads</title>
 <link>http://www.newlc.com/forum/hbufc-and-threads#comment-44079</link>
 <description>&lt;p&gt;So getting back on topic:&lt;/p&gt;

&lt;p&gt;Is there any way to modify ( e.g. assign some new value ) HBufC descriptor which was created in different thread ?&lt;/p&gt;

&lt;p&gt;You&#039;ve got two choices:&lt;/p&gt;

&lt;p&gt;1. Use a shared heap&lt;br /&gt;
2. Do it manually:&lt;/p&gt;

&lt;p&gt;// Get a TPtr to the data&lt;br /&gt;
TPtr ptrToData((TUint16*) iSharedBuf-&gt;Ptr(), iSharedBuf-&gt;Length(), iSharedBuf-&gt;MaxLength());&lt;/p&gt;

&lt;p&gt;// Modify the data&lt;br /&gt;
ptrToData.Append(someotherBuf);&lt;/p&gt;

&lt;p&gt;// Update the length in the HBufC&lt;br /&gt;
iSharedBuf-&gt;SetLength(updatedLength);&lt;/p&gt;

&lt;p&gt;And as alh has pointed out you&#039;ll need a mutex if there&#039;s any chance that the other thread will also want to access the data at the same time. Note that I&#039;m assuming here that calling HBufC::Length(), HBufC::MaxLength() and HBufC::SetLength(), doesn&#039;t call any of those RHeap functions!&lt;/p&gt;

&lt;p&gt;EDIT: Actually that doesn&#039;t work either because there isn&#039;t a HBufC::SetLength() function! It looks to me like you&#039;ve found a really tricky problem. How about not using HBufC and simply creating a TBuf on the heap instead, would that work?&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;</description>
 <pubDate>Wed, 23 Jan 2008 21:07:03 +0100</pubDate>
 <dc:creator>fig7</dc:creator>
 <guid isPermaLink="false">comment 44079 at http://www.newlc.com</guid>
</item>
<item>
 <title>Re: HBufC and threads</title>
 <link>http://www.newlc.com/forum/hbufc-and-threads#comment-44077</link>
 <description>&lt;p&gt;From the 3rd edition SDK:&lt;/p&gt;

&lt;p&gt;This panic is raised by a number of RHeap member functions, AllocLen(), Free(), FreeZ(), ReAlloc(), ReAllocL(), Adjust() and AdjustL() when a pointer passed to these functions does not point to a valid cell. &lt;/p&gt;

&lt;p&gt;so where did you get the &quot;It checks if memory cell on which we want to work on belongs to current thread heap or not&quot; bit from?&lt;/p&gt;

&lt;p&gt;Maybe internally Des() calls one of those functions, so the panic would make sense if the threads have seperate heaps. In any case what you need to is change the call to RThread::Create() so that you use a shared heap. I do find it quite odd though that something like Des() would actually call one of the RHeap functions. Clearly that&#039;s really annoying if all you want to do is write to a HBufC (which may or may not belong to your threads heap).&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;</description>
 <pubDate>Wed, 23 Jan 2008 20:41:38 +0100</pubDate>
 <dc:creator>fig7</dc:creator>
 <guid isPermaLink="false">comment 44077 at http://www.newlc.com</guid>
</item>
<item>
 <title>Re: HBufC and threads</title>
 <link>http://www.newlc.com/forum/hbufc-and-threads#comment-44076</link>
 <description>&lt;p&gt;&lt;div class=&quot;bb-quote&quot;&gt;&lt;blockquote class=&quot;bb-quote-body&quot;&gt;And according to my SDK User 42 has nothing to do with threads!&lt;/blockquote&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;I didn&#039;t write that that it has something with threads, I wrote&lt;/p&gt;

&lt;p&gt;&lt;div class=&quot;bb-quote&quot;&gt;&lt;blockquote class=&quot;bb-quote-body&quot;&gt;... when I call Des() method on it I always get panic User 42. &lt;span style=&quot;font-weight:bold&quot;&gt;I&#039;ve take a look on this panic description and it says that this panic is raised by RHeap class&lt;/span&gt;. It checks if memory cell on which we want to work on belongs to current thread heap or not.&lt;/blockquote&gt;&lt;/div&gt;&lt;/p&gt;</description>
 <pubDate>Wed, 23 Jan 2008 20:23:26 +0100</pubDate>
 <dc:creator>lman</dc:creator>
 <guid isPermaLink="false">comment 44076 at http://www.newlc.com</guid>
</item>
<item>
 <title>Re: HBufC and threads</title>
 <link>http://www.newlc.com/forum/hbufc-and-threads#comment-44075</link>
 <description>&lt;p&gt;Actually what am i talking about!?  Your code should be fine regardless as you&#039;ve only got one object on one heap!&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;</description>
 <pubDate>Wed, 23 Jan 2008 20:16:05 +0100</pubDate>
 <dc:creator>fig7</dc:creator>
 <guid isPermaLink="false">comment 44075 at http://www.newlc.com</guid>
</item>
<item>
 <title>Re: HBufC and threads</title>
 <link>http://www.newlc.com/forum/hbufc-and-threads#comment-44055</link>
 <description>&lt;p&gt;Not good, but anyway thx guys&lt;/p&gt;</description>
 <pubDate>Wed, 23 Jan 2008 20:16:00 +0100</pubDate>
 <dc:creator>lman</dc:creator>
 <guid isPermaLink="false">comment 44055 at http://www.newlc.com</guid>
</item>
<item>
 <title>Re: HBufC and threads</title>
 <link>http://www.newlc.com/forum/hbufc-and-threads#comment-44074</link>
 <description>&lt;p&gt;class CWhatever : public CBase&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
HBufC*  iSharedBuf;&lt;br /&gt;
};&lt;/p&gt;

&lt;p&gt;Thread 1:&lt;br /&gt;
iSharedBuf = HBufC::NewL(100);&lt;/p&gt;

&lt;p&gt;Thread2:&lt;br /&gt;
iSharedBuf-&gt;Des() = someOtherBuf;&lt;/p&gt;

&lt;p&gt;This should be fine if you have used the version of RThread::Create() that creates a thread using the same heap as the parent (you just pass NULL for the heap ptr IIRC)&lt;/p&gt;

&lt;p&gt;&quot;especially raghav_an&quot;&lt;/p&gt;

&lt;p&gt;Except that raghav doesn&#039;t know what he&#039;s talking about whereas alh does. The only major issue with having seperate heaps is that it is sometimes easy to &quot;forget&quot; which thread created an object so you end up calling delete on an object that is on a different thread&#039;s heap, which not surprisingly usually ends up generating some kind of panic.&lt;/p&gt;

&lt;p&gt;And according to my SDK User 42 has nothing to do with threads!&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;</description>
 <pubDate>Wed, 23 Jan 2008 20:12:34 +0100</pubDate>
 <dc:creator>fig7</dc:creator>
 <guid isPermaLink="false">comment 44074 at http://www.newlc.com</guid>
</item>
<item>
 <title>Re: HBufC and threads</title>
 <link>http://www.newlc.com/forum/hbufc-and-threads#comment-44072</link>
 <description>&lt;p&gt;Sorry guys but I think that you started loosing point of my question. So I will ask again, giving you more info:&lt;/p&gt;

&lt;p&gt;In first thread I create HBufC object. In second thread I want to write some data into it but when I call Des() method on it I always get panic User 42. I&#039;ve take a look on this panic description and it says that this panic is raised by RHeap class. It checks if memory cell on which we want to work on belongs to current thread heap or not. So is there any way to around this limitation ?&lt;br /&gt;
&lt;/p&gt;</description>
 <pubDate>Wed, 23 Jan 2008 19:00:55 +0100</pubDate>
 <dc:creator>lman</dc:creator>
 <guid isPermaLink="false">comment 44072 at http://www.newlc.com</guid>
</item>
<item>
 <title>Re: HBufC and threads</title>
 <link>http://www.newlc.com/forum/hbufc-and-threads#comment-44060</link>
 <description>&lt;p&gt;&lt;div class=&quot;bb-quote&quot;&gt;&lt;blockquote class=&quot;bb-quote-body&quot;&gt;&lt;br /&gt;
On the other hand, I agree that if your child thread has no communication with the parent thread it may be better to use a seperate heap (so that the memory is freed if the thread panics). Personally though I never use child threads that don&#039;t have some form of communication with the parent. Parent threads shouldn&#039;t really disown their children, at the very least they should &quot;logon&quot;...&lt;br /&gt;
&lt;/blockquote&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;The problem is that &quot;logon&quot; in itself doesn&#039;t solve all problems.&lt;br /&gt;
You also need some way to get the pointers to all allocated cells allocated by that thread, and manually go through them and call &quot;delete&quot; on all of them.&lt;br /&gt;
Could possibly be done with smart usage of the cleanupstack, or some other way to pass a base pointer to the tree of all allocated cells by that thread to the main thread so it can clean it up, but most people seem to forget this.&lt;br /&gt;
The normal cleanup wont happen &quot;automagically&quot; when the thread panics.&lt;/p&gt;

&lt;p&gt;If you let the thread has its own heap, that whole chunk containing the heap will be freed when the thread dies, so no need to clean it up manually, so the problem solves itself...&lt;br /&gt;
&lt;/p&gt;</description>
 <pubDate>Wed, 23 Jan 2008 13:40:51 +0100</pubDate>
 <dc:creator>alh</dc:creator>
 <guid isPermaLink="false">comment 44060 at http://www.newlc.com</guid>
</item>
<item>
 <title>Re: HBufC and threads</title>
 <link>http://www.newlc.com/forum/hbufc-and-threads#comment-44057</link>
 <description>&lt;p&gt;&lt;div class=&quot;bb-quote&quot;&gt;&lt;blockquote class=&quot;bb-quote-body&quot;&gt;&lt;br /&gt;
If the thread has it own heap then it is not possible to access the objects present in another threads heap, even if both threads are in same address space.&lt;br /&gt;
If the thread tries to access any memory from another heap there will be KERN-EXEC 3 panic for access violation.&lt;br /&gt;
&lt;/blockquote&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;No. You wont. &lt;br /&gt;
If you get Kern-Exec:s when trying to share objects between threads, it is not because of memory violation, but other reasons.&lt;br /&gt;
(One classic is Kern-Exec 0 because handles can&#039;t be shared between threads unless specifically created to be shareable)&lt;br /&gt;
The process is the unit of memory mapping, the OS will not remap your virtual memory when switching between threads.&lt;br /&gt;
(Thats part of the definition of the difference between a thread and a process)&lt;/p&gt;

&lt;p&gt;I&#039;m not just saying here, I have production code running that uses that exact fact.&lt;/p&gt;</description>
 <pubDate>Wed, 23 Jan 2008 13:32:39 +0100</pubDate>
 <dc:creator>alh</dc:creator>
 <guid isPermaLink="false">comment 44057 at http://www.newlc.com</guid>
</item>
<item>
 <title>HBufC and threads</title>
 <link>http://www.newlc.com/forum/hbufc-and-threads</link>
 <description>&lt;p&gt;Is there any way to modify ( e.g. assign some new value ) HBufC descriptor which was created in different thread ?&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://www.newlc.com/forum/hbufc-and-threads&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://www.newlc.com/forum/hbufc-and-threads#comments</comments>
 <category domain="http://www.newlc.com/forums/symbian-c">Symbian C++</category>
 <pubDate>Tue, 22 Jan 2008 17:58:00 +0100</pubDate>
 <dc:creator>lman</dc:creator>
 <guid isPermaLink="false">20051 at http://www.newlc.com</guid>
</item>
</channel>
</rss>
