samsung bada development without using pointers

大城市里の小女人 提交于 2019-12-01 08:46:13

In this code:

// Create a Label
Label pLabel;
pLabel.Construct(Rectangle(50, 200, 150, 40), L"Text");
pLabel.SetBackgroundColor(Color::COLOR_BLUE);
AddControl(pLabel);

the label object is destroyed when it goes out of scope and hence it fails to show up on the form. It is unfortunate that the AddControl method takes a reference as that implies that the above should work. Using:

Label *pLabel = new Label();

works because the destructor is not called by default when the pLabel variable goes out of scope.

Note that Bada documentation says this

All containers and controls must be created on the device heap memory. When an application terminates, the bada platform deletes the frame control and its descendants. Furthermore, the platform frees the heap memory that has been allocated to the application.

Your code may run fine because it ignores the call to AddControl because it detected that the control was not allocated in the heap space. In that case AddControl should have returned an error code.

To get a correct Bada code, one needs to write something like:

result MySample::OnInitializing() {
  result r = E_SUCCESS;

  std::auto_ptr<Label> pLabel(new Label);
  if (pLabel.get() != null && E_SUCESS == pLabel->Construct(Rectangle(50, 200, 150, 40), L"Text"))
  {
     pLabel->SetBackgroundColor(Color::COLOR_BLUE);
     r = AddControl(*pLabel);
     pLabel.release();
  }
  else
  {
    r = E_FAILURE;
  }
  return r;

}

Following such coding guidelines ensures that your application will be able to escalate any issues while initializing UI except if the issue happens whiele executing OnAppInitializing.

Visitor nailed it in his comment; Bada uses C++ techniques pioneered in the mid-nineties by Symbian. They're utterly outdated today. Don't take it too seriously, you can't really expect Samsung to put their top-rate people on such a doomed project. Those will be working on their Android line.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!