global new/delete for C++ | developer.brewmp.com global new/delete for C++ | developer.brewmp.com

Developer

global new/delete for C++

For C++ class we need to override new/delete operators per class basis. I have tried to override new/delete at global level with few simpler class and was able to compile and run in device successfully.

For large code base, overriding new/delete per class basis is rather tedious and error prone job.

Did anybody do that for larger code base and did you face any problem? Qualcomm tech support informed me that they haven't done extensive testing against global new/delete operators.

Any input would be appreciated.
regards
ruben

Hi,
Would you mind posting how you did the global override?
I tried to get this working several months ago and failed. My application has about 40 classes.
Thanks.
Matt.

Hi,
Would you mind posting how you did the global override?
I tried to get this working several months ago and failed. My application has about 40 classes.
Thanks.
Matt.

I'm using global new and delete operators and I expect to have no problems with it (I haven't sent a C++ app to NSTL yet, however). Nonetheless, I'm not worried as I can't see how the ARM compiler would be able to pass internal testing without getting this one right. I got this off the BREW forums...sorry I can't remember the original thread.
#include "aeestdlib.h"
void* operator new(size_t sz)
{
return MALLOC(sz);

void operator delete(void *p)
{
FREE(p);

void* operator new[](size_t sz)
{
return MALLOC(sz);

void operator delete[](void *p)
{
FREE(p);

I'm using global new and delete operators and I expect to have no problems with it (I haven't sent a C++ app to NSTL yet, however). Nonetheless, I'm not worried as I can't see how the ARM compiler would be able to pass internal testing without getting this one right. I got this off the BREW forums...sorry I can't remember the original thread.
#include "aeestdlib.h"
void* operator new(size_t sz)
{
return MALLOC(sz);

void operator delete(void *p)
{
FREE(p);

void* operator new[](size_t sz)
{
return MALLOC(sz);

void operator delete[](void *p)
{
FREE(p);

I also do all the 4 new/delete operators overload. Compiler did not complain and DBGPRINTF showed correct result. Only thing is that Qualcomm tech support did not give definitive answer if there is a possiblity of any problem.
regards
ruben

I also do all the 4 new/delete operators overload. Compiler did not complain and DBGPRINTF showed correct result. Only thing is that Qualcomm tech support did not give definitive answer if there is a possiblity of any problem.
regards
ruben

We have passed several NSTL certifications while using global new/deletes. It seems to work ok.
Your Mileage May Vary.

We have passed several NSTL certifications while using global new/deletes. It seems to work ok.
Your Mileage May Vary.

I've also been using it for some time.
There's actually no reason why it would or should not work. The ARM compiler supports the full C++ standard, and operator overloading does not require any external libararies because it is part of the actual C++ language.

I've also been using it for some time.
There's actually no reason why it would or should not work. The ARM compiler supports the full C++ standard, and operator overloading does not require any external libararies because it is part of the actual C++ language.

When overloading the delete operator, will brew still call the destructor of your class that you are deleting?

When overloading the delete operator, will brew still call the destructor of your class that you are deleting?

absolutely. first your destructor is called, and then operator delete() is called to release the memory. This is mandated by the C++ spec.
-Aaron

absolutely. first your destructor is called, and then operator delete() is called to release the memory. This is mandated by the C++ spec.
-Aaron

hi all...
overriding new/delete globally (post of 'aisaksen' overriding using inline) works fine with my app... and i feel that for large programm base it should not give any problem, since my app is video decoder....
sdg

hi all...
overriding new/delete globally (post of 'aisaksen' overriding using inline) works fine with my app... and i feel that for large programm base it should not give any problem, since my app is video decoder....
sdg

Yes it will. Operator overloading has nothing to do with the calling of constructors and destructors, really.

Yes it will. Operator overloading has nothing to do with the calling of constructors and destructors, really.

Hi All,
Do you guys inline these operator overrides?
I get an error at link time for every class if I do not inline the operator overrides:
In Visual C++:
error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in Bkgd.obj
In ARM:
Error: L6200E: Symbol operator new(unsigned int) multiply defined (by ConfigFile.o and Bkgd.o).
To perform the global operator override, I made an "AppStd.h" file:
---------------------------------------------
#ifndef APPSTD_H_
#define APPSTD_H_
#include "AEEStdLib.h"
void* operator new(size_t size) {
return MALLOC(size);

void operator delete(void* p) {
FREE(p);

#endif
------------------------------------------------
Then #include this in every class header file...
I am assuming that both compilers are defining their own default new and delete operators for each class, and the linker doesn't know which one to use.
Thanks.
Matt.

Hi All,
Do you guys inline these operator overrides?
I get an error at link time for every class if I do not inline the operator overrides:
In Visual C++:
error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in Bkgd.obj
In ARM:
Error: L6200E: Symbol operator new(unsigned int) multiply defined (by ConfigFile.o and Bkgd.o).
To perform the global operator override, I made an "AppStd.h" file:
---------------------------------------------
#ifndef APPSTD_H_
#define APPSTD_H_
#include "AEEStdLib.h"
void* operator new(size_t size) {
return MALLOC(size);

void operator delete(void* p) {
FREE(p);

#endif
------------------------------------------------
Then #include this in every class header file...
I am assuming that both compilers are defining their own default new and delete operators for each class, and the linker doesn't know which one to use.
Thanks.
Matt.

put your function definitions in a cpp file, not in a h file. That will solve the problem.
memory allocation is typically a slow operation in embedded systems, so theoretically you aren't buying much by inlining it.
-Aaron

put your function definitions in a cpp file, not in a h file. That will solve the problem.
memory allocation is typically a slow operation in embedded systems, so theoretically you aren't buying much by inlining it.
-Aaron

Try defining the functions in AppStd.cpp:
#include "AEEStdLib.h"
#include "AppStd.h"
void* operator new(size_t size) {return MALLOC(size);}
void operator delete(void* p) {FREE(p);}
and just declaring them in AppStd.h:
#ifndef APPSTD_H_
#define APPSTD_H_
void* operator new(size_t size);
void operator delete(void* p);
#endif

Try defining the functions in AppStd.cpp:
#include "AEEStdLib.h"
#include "AppStd.h"
void* operator new(size_t size) {return MALLOC(size);}
void operator delete(void* p) {FREE(p);}
and just declaring them in AppStd.h:
#ifndef APPSTD_H_
#define APPSTD_H_
void* operator new(size_t size);
void operator delete(void* p);
#endif

That works. Thank you guys.

That works. Thank you guys.

hello everyone, i define the new/delete as pcoleman says.
but there's an problem.
my class is like this:
class IWindow
{
IWindow();
virtual ~IWindow();
virtual void doIt() = 0;
....

class CWindow : public IWindow
{
CWindow();
virtual ~CWindow();
....

class CMainWin : public CWindow
{
CMainWin();
virtual ~CWindow();
virtual doIt();
....

....
IWindow* pWin = new CMainWin();//// that's ok!
....
delete pWin;//// ERROR ocurred here!!
The emulator prompt a message box says that:
"The instruction at '0x4104488b' referenced memory at '0x4104488b'. the memory could not be 'read'"
could someone help me solve this problem?? thanx in advance
-michael

hello everyone, i define the new/delete as pcoleman says.
but there's an problem.
my class is like this:
class IWindow
{
IWindow();
virtual ~IWindow();
virtual void doIt() = 0;
....

class CWindow : public IWindow
{
CWindow();
virtual ~CWindow();
....

class CMainWin : public CWindow
{
CMainWin();
virtual ~CWindow();
virtual doIt();
....

....
IWindow* pWin = new CMainWin();//// that's ok!
....
delete pWin;//// ERROR ocurred here!!
The emulator prompt a message box says that:
"The instruction at '0x4104488b' referenced memory at '0x4104488b'. the memory could not be 'read'"
could someone help me solve this problem?? thanx in advance
-michael

Is your delete op trying to FREE() an uninitialized pointer member somewhere?

Is your delete op trying to FREE() an uninitialized pointer member somewhere?

hi, Murray, first, thanks for your reply.
then, i review my source code, and i'm sure that pWin = new CMainWin() is done in the initAppData(). and i debug my application step by step, i find the error occured at the line "delete pWin;", but before FREE(), maybe it's a problem related with the virtual destructor.
i really don't know how to do with it, Could you give me some suggestion??
thanx
-michael

hi, Murray, first, thanks for your reply.
then, i review my source code, and i'm sure that pWin = new CMainWin() is done in the initAppData(). and i debug my application step by step, i find the error occured at the line "delete pWin;", but before FREE(), maybe it's a problem related with the virtual destructor.
i really don't know how to do with it, Could you give me some suggestion??
thanx
-michael

thank you, Murray! i have find the problem, it has no business with the new/delete, while there's a wrong operation in my CWinMain's destructor. (i relese a wrong pointer of a brew interface) :)
so happy now!
-michael

thank you, Murray! i have find the problem, it has no business with the new/delete, while there's a wrong operation in my CWinMain's destructor. (i relese a wrong pointer of a brew interface) :)
so happy now!
-michael

I certainly didn't do much! Glad you solved it!

I certainly didn't do much! Glad you solved it!

As stated in this thread, I have overloaded new and delete for my class. They are working fine, but when i overload new[] and delete[], device resets.
Is there somethng extra to be done for it.

As stated in this thread, I have overloaded new and delete for my class. They are working fine, but when i overload new[] and delete[], device resets.
Is there somethng extra to be done for it.

Hi Guys,
I am new to brew, and have come accross this thread with the help of another user.
I was wondering, can I declare a base class say, CObject, and overide the new and delete opertaor in this?
I would then derive all classes that I write from CObject?
Thanks for your help.
Adrian

Hi Guys,
I am new to brew, and have come accross this thread with the help of another user.
I was wondering, can I declare a base class say, CObject, and overide the new and delete opertaor in this?
I would then derive all classes that I write from CObject?
Thanks for your help.
Adrian