crash in overloaded delete operator

Login to reply to this topic.
Tue, 2007-10-09 10:52
Joined: 2007-10-09
Forum posts: 7

hi
For code pasted below can someone explain why the following things are happening:

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
{
public:
        int i,j,k,l;
};

void* operator new [](unsigned int aSize,int aLine)
{
        void *ret = User::Alloc(aSize);
        return ret;
}

void operator delete [](void *p)
{
        User::Free(p);
}


void operator delete [](void *p, int)
{
        User::Free(p);
}


void somefunc()
{
        int i = 10;
        MyClass *pClass = new(i) MyClass[3];
//        delete [] pClass;
        operator delete [](pClass,i);
}


Tue, 2007-10-09 11:41
Joined: 2007-09-12
Forum posts: 8
Re: crash in overloaded delete operator

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.

Wed, 2007-10-10 03:52
Joined: 2007-10-09
Forum posts: 7
Re: crash in overloaded delete operator

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

Wed, 2007-10-10 09:46
Joined: 2007-09-12
Forum posts: 8
Re: crash in overloaded delete operator

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:

This panic is raised by the Free() and FreeZ() member functions of an RHeap. It is caused when the cell being freed overlaps the next cell on the free list (i.e. the first cell on the free list with an address higher than the one being freed).

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.

Wed, 2007-10-10 16:13
Joined: 2007-10-09
Forum posts: 7
Re: crash in overloaded delete operator


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

Wed, 2007-10-10 16:32
Joined: 2007-09-12
Forum posts: 8
Re: crash in overloaded delete operator

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.

Wed, 2007-10-10 16:53
Joined: 2007-10-09
Forum posts: 7
Re: crash in overloaded delete operator

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

Wed, 2007-10-10 17:19
Joined: 2007-09-12
Forum posts: 8
Re: crash in overloaded delete operator

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)

Thu, 2007-10-11 12:29
Joined: 2007-10-09
Forum posts: 7
Re: crash in overloaded delete operator

Yes it works.. you can try it.

  • Login to reply to this topic.