Question about using new[] with R-classes

Login to reply to this topic.
Tue, 2007-12-11 12:35
Joined: 2004-07-12
Forum posts: 123

Hi,

I know that using new[] is not recommended with C-classes becasue this function is not overloaded by CBase and thus does not zero the memory allocated. (http://www.newlc.com/inside-cbase-class-six-essential-questions).

But why it does not work with R-classes? I tried it with RThread where i wanted to create an array of threads but i got USER panic 42. I just want to understand the reason behind it not working, just from educational curiosity. I managed to sort this out using RArray though.

Hope to hear from the experts here Smiling

AF


Tue, 2007-12-11 12:41
Joined: 2004-11-29
Forum posts: 1246
Re: Question about using new[] with R-classes

are you using delete[] to delete it again?

USER 42 means some kind of heap corruption, I don't think it has to do with the class being an R-class, but something else you do wrong.

Tue, 2007-12-11 13:13
Joined: 2004-07-12
Forum posts: 123
Re: Question about using new[] with R-classes

ooppsss!!!! I missed that bit Smiling
u r totally right. I tired ur suggestion and it seems to solve the problem.
I thought there is some sort of Symbian-specific concept under the hood, like new[] for CBase. But it seems i was reading too much between the lines:)

thanx any ways
AF

Tue, 2007-12-11 18:08
Joined: 2007-09-23
Forum posts: 159
Re: Question about using new[] with R-classes

R objects are not intended to be allocated on the heap anyway though, if you need an array of R objects you could use one of the R array types !

Wed, 2007-12-12 17:12
Joined: 2004-07-12
Forum posts: 123
Re: Question about using new[] with R-classes

Yee i know this convention. But if you want to instantiate an array of RThreads u have to allocate them on the heap. And if u think about it, RArray does exactly this under the hood. It allocates Flat/Segmented memory on the heap for each member of the array (in my case of type RThread).
Any ways thanks for the comments

Wed, 2007-12-12 17:14
Joined: 2003-12-05
Forum posts: 683
Re: Question about using new[] with R-classes

Yes but if you allocate R classes from the heap, you easily forget to call Close before delete-ing the object, which will be bad.

If you want to instantiate an array of threads, you still can do that using dynamic arrays and need not use C arrays which can lead you to the usual trouble with them.

Wed, 2007-12-12 18:03
Joined: 2007-09-23
Forum posts: 159
Re: Question about using new[] with R-classes

Why do you need to allocation the RThreads on the heap in that instance? R Object are handles and the object to which they are a handle will be allocated on the heap, but that is done not in your heap in in the system's. You can put your R object on the heap if you really want to, however making it leave safe becomes more troublesome (you will have to push the R boject itself and the pointer to the R object onto the cleanupstack which is inelegant and more error prone).

Fri, 2007-12-14 13:23
Joined: 2004-11-29
Forum posts: 1246
Re: Question about using new[] with R-classes

Its not that much gain putting your R-object in an RArray compared to put it in a heap cell you manage yourself, when it comes to cleanup handling.

the array of the RArray is indeed allocated on _your_thread_heap_ and not on some system heap. (It would be quite inefficient to do that...)
And if you put R-objects on the RArray, you still need a special cleanup-item to make sure you call "Close" on all items of the array before calling "Close" on the array itself, quite similar to what you would need if you put it in a heap cell...

What you do get though is the normal gains of using an RArray over selfmanaged ones, which is mainly cleaner syntax and support for searching and sorting, and possibility to easily resize it.

But I still agree using RArray is usually better then using c arrays Smiling

  • Login to reply to this topic.