3

Objects in C++ can be created using the methods listed below(that I am aware of):

Person p;

or

Person p("foobar");

or

Person * p = new Person();

Then, why does not the Samsung Bada IDE allow me to do the first two methods? Why do I always have to use pointers? I am ok with using pointers and all, just that I want to know the fundamental reason behind the style.

Sample code from Bada API reference.

// Create a Label
Label *pLabel = new Label();
pLabel->Construct(Rectangle(50, 200, 150, 40), L"Text");
pLabel->SetBackgroundColor(Color::COLOR_BLUE);
AddControl(*pLabel);

I modified and tried using the code below. Although it compiles and the app runs, the label does not show up on the form.

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

Note : Rectangle class which is used creates an object on the fly without pointer. How is it different from Label then? Its confusing :-/

yogiam
  • 168
  • 7
  • I have no idea about the answer to your question, but `Person *p = new Person()` is *really* bad code (in general). In this situation `p` should be some sort of smart pointer that can take ownership of the newly allocated `Person`. – Mankarse Sep 20 '11 at 08:17
  • Can you please elaborate why it is bad? How should it be otherwise(good)? – yogiam Sep 20 '11 at 08:52
  • 1
    @Mankarse, it's just normal c++ pointer code, it's not bad; the bad thing can be the misuse of it or not `delete`ing the allocated memory. – Griwes Sep 20 '11 at 09:10
  • @yogiam, the first "two" methods are the same method, second is different only because it does provide some data to object's constructor and first does not. – Griwes Sep 20 '11 at 09:11
  • @Griwes, @yogiam - The instant that you have resources that are not owned by any object, you must either ban exceptions in the following region of code, or put in horrible duplicate clean-up code (one cleanup in a `catch(...)`, the other in the main code block). It is unfortunate that many C++ texts miss this, and present code that is simply incorrect. Put simply, failure to use RAII causes you to miss out on 50% of the goodness of C++. – Mankarse Sep 20 '11 at 09:19
  • @Mankarse, I just wrote that _the code_ is not bad; only it's usage can be bad. Where do you know from that the code isn't followed by something like `this.vector.append(p);`? – Griwes Sep 20 '11 at 09:20
  • Here it is suggested that's because the implementation does not have exceptions and smart pointers: http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=487. No idea if you are constructing objects correctly or if you have to do something different. – visitor Sep 20 '11 at 09:21
  • 2
    @Mankarse: bada bans exceptions completely. It prefers two phase construction, the direct opposite of RAII. It prefers a non-throwing constructor (e.g. no `new` in constructor) followed by a Construct call. The reason for this is that bada (and Symbian) is designed for resource limited devices and adding exception handling was considered too much of an overhead. The IDE may provide assistance with this but I don't know why it's causing the OPs problems - perhaps looking at the constructor might help? – Skizz Sep 20 '11 at 09:29
  • 1
    @Skizz, Bada does uses the new operator followed by a Contruct call. Please see the code above. Code copied from [Bada API reference](http://static.bada.com/contents/docs/apis/bada-V1.0.0a3/framework/classOsp_1_1Ui_1_1Controls_1_1Label.html) – yogiam Sep 20 '11 at 10:05
  • Thanks all for the replies. Replies from Skizz and david have answered my question precisely. Although I cannot accept both the answers, I agree with both of them :) – yogiam Nov 09 '11 at 08:06

3 Answers3

4

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.

Skizz
  • 69,698
  • 10
  • 71
  • 108
  • What about the Rectangle class used in Construct call? How come a reference of it is maintained by the framework :-/ – yogiam Sep 20 '11 at 10:30
  • @yogiam: the Rectangle data is probably copied into the Label control by value using the Control::SetBounds method. I don't know anything about the inner workings of the API so it may do something else. The important thing is that the data is stored before the Rectangle is destroyed. – Skizz Sep 20 '11 at 10:39
  • 1
    Skizz, you overlooked the most vexing parse! `Label pLabel()` declares a function! This code does something completely differently from what you say. – Konrad Rudolph Sep 28 '11 at 08:36
  • @Konrad: Good spot. I just copy+pasted from the question, but the OP said the code compiled OK and ran so it must be a typing error by the OP. – Skizz Sep 28 '11 at 09:23
2

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.

david
  • 1,311
  • 12
  • 32
  • Thanks for the link david. I was looking for that 1 line document which clears how memory is managed in bada :) – yogiam Nov 09 '11 at 07:59
0

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.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • I'm not so sure Bada is doomed. After Google's acquisition of Motorola HTC is mumbling about buying a mobile operating system and I wouldn't be surprised if Samsung started investing more in Bada. – piokuc Sep 20 '11 at 10:25
  • @piokuc: Forking Android would still make more sense than starting from scratch. Symbian was succesful for a decade by starting at the high end, migrating down to the low end as superior alternatives overtook Nokia. Bada _started_ at the low-end, where Symbian is now, and they both have got the same future. – MSalters Sep 20 '11 at 11:03
  • I'd argue about what you define as low end. Admittedly, I've stumbled upon the low end S575 but thankfully it is rather obscure. In the UK the Wave quietly appeared, then dissappeared and currently you get a whole lot of phone (like iPhone level hardware) for £100 in the Wave II. It's rather like the iPhone's OS, one manufacturer to develop for makes it easier. They just need the Apple marketing team now. – John Oct 09 '11 at 19:27