crash in overloaded delete operator
| Tue, 2007-10-09 10:52 | |
|
hi a. The class MyClass has 4 ints. WHen I allocate an array of 3 objects the size of memory requested in overloaded new [] operator is 56. b. When I return from this overloaded new, the memory address in pClass variable is offset by 8 bytes ( 56-48) automatically! c. When I try to delete using the overloaded delete operator (the one which takes a bool) the code crashes! d. However if delete using the standard overloaded operator (delete [](void *)) then the code does not crash. e. The memory addresses received as input are different, if you guessed it by now, offset by 8! class MyClass |
|






Forum posts: 8
MyClass *pClass = new(i) MyClass[3];This does not invoke your overloaded ctor. Probably you are not getting a pointer to a cell from it.
When you call your overloaded dtor, it panics beacuse the ponter you supplied isn't a cell.
Check usage of your ctor to begin with. If it doesn't help, please post more info about the PANICs you are getting.
Forum posts: 7
I get a USER 44 panic.
The code works if I call
delete [] pClass;what is a Cell?
As mentioned before if you debug both the deletes the address passed to it changes, the compiler is generating code which passes the correct address to the operator.
void operator delete [](void *p){
User::Free(p);
}
however for the overloaded operator the address passed is incorrect.
Regards
Chimanrao
Forum posts: 8
When you are debugging, you are using Window's memory model, which is a lot forgiving(or misleading should I say?) in situations like this. However on the device, you are subject to ARM memory handling, which is a lot stricter.
Back to your problem. I must refer you to http://www.symbian.com/developer/techlib/v9.2docs/doc_source/reference/reference-cpp/E32_EKA2/UserClass.html#%3a%3aUser for what I meant by a cell. Please have look in descriptions of Alloc and Free functions.
Additionally if we have look at panic USER 44:
Bottom of the line; if you want to use your overloaded dtor; you have to use your overloaded ctor in this situation. You are using the Symbian overload of the new operator instead.
Forum posts: 7
Actually I am overloading the new operator
void* operator new [](unsigned int aSize,int aLine){
void *ret = User::Alloc(aSize);
return ret;
}
and invoking it as as follows
MyClass *pClass = new(i) MyClass[3];// delete [] pClass;
operator delete [](pClass,i);
Yes I know I am not calling the classes constructor in the new, but in my sample, immediately after construction I am deleting the array, so i shouldnt matter if I call the constructor or not.
The point is if I dont overload the delete operator, everything works fine and redefined the stanadard delete operator as
void operator delete [](void *p){
User::Free(p);
}
everything works fine.
Regards
Chimanrao
Forum posts: 8
Be careful about the number of parameters you are passing to new opertor.
MyClass *pClass = new(i, __LINE__) MyClass[3];I guess this is what you are trying to do, assuming aLine refers to the line in the code file.
Forum posts: 7
no the way I have written is correct,
you can check the code by compiling it
when you overload the new [] operator the first argument is fixed as per the standard and refers to the number of bytes to be allocated. After that come the arguments with which you want to overload the new operator.
So if you want to differentiate your new [] operator from the system new[] operator you need to write the code they way I wrote it.
Regards
Chimanrao
Forum posts: 8
Are you sure, your new operator is being called? Did you try putting in some logging perhaps?
I have a feeling that you overloaded operator is too similar to standard Symbian overload of new(i.e new(ELeave) CMyClass)
Forum posts: 7
Yes it works.. you can try it.