×
Namespaces

Variants
Actions

Open C++: new操作符的问题

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

兼容于
文章
翻译:
hoolee
最后由 hamishwillee 在 13 Aug 2012 编辑
  • 详细描述

在ARMV5平台抛出bad_alloc或任何new生成的对象都会引发崩溃。甚至当用户使用重载的new操作符抛出生成对象都会引发这个问题。


具有下列特征的操作将会引发此类问题:

 void *operator new(unsigned int aSize);
void *operator new[](unsigned int aSize);
  • 如何重演

下列代码片段描述了此类问题:

class Dummy
{
};
 
void *operator new(unsigned int aSize)
{
void* __y = malloc(aSize);
// try to simulate a bad alloc
if (__y == 0)
{
throw Dummy(); // this will result in a crash
}
return __y;
}
  • 解决方案

完成一个用户重载的new操作符。可以按照如下方式完成:

void* operator new(size_t s, newarg) throw (std::bad_alloc)

并执行如下:

Myclass* my = new (S60) Myclass()
This page was last modified on 13 August 2012, at 08:15.
252 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×