Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Developer
MedNez
Posts: 67
Registered: ‎11-24-2008
My Device: Not Specified
Accepted Solution

C++ ~App Destructor causes build error?

Hi all,

 

I downloaded some sample code:

 

https://github.com/blackberry/Presentations/tree/master/2012-BlackBerryJam-Americas/JAM09/BaDumTss

 

You will notice in the app.cpp and app.h files, the following lines in bold.

 

app.cpp

 

SoundPartyApp::SoundPartyApp()
{

   // create the qml etc.

   mSoundManager = new SoundManager("sounds/");
}

SoundPartyApp::~SoundPartyApp()
{
  // Destroy the sound manager.
  delete mSoundManager;
}

 

app.h

 

class SoundPartyApp: public QObject
{
Q_OBJECT

public:

  // This is our constructor to setup our badumtss2 application
  SoundPartyApp();

  // This is our destructor that frees the memory of the sound manager
  ~SoundPartyApp();
  // more stuff here etc

private:
  // The sound manager
  SoundManager *mSoundManager;
};

 

I understand this is with the old c++ code. The new app.hpp which I am using in beta 3, therefore, has a different "virtual" destructor:

 

class SoundPartyApp : public QObject
{
Q_OBJECT

public:

    SoundPartyApp(bb::cascades::Application *app);
    virtual ~SoundPartyApp() {}

    // etc

}

 

The new virtual ~SoundPartyApp() {} seems to conflict with the function in app.cpp.

 

Not sure why.

 

I have commented out the function and now it works... but I guess now I am not properly unallocating the soundmanager from memory. 

 

I can't seem to find any useful threads about how to get beta2 ~destructor functions working in beta3. 

 

Any Ideas?

Please use plain text.
Developer
Dredvard
Posts: 160
Registered: ‎01-27-2012
My Device: Playbook
My Carrier: Rogers

Re: C++ ~App Destructor causes build error?

I'm not sure what your error code is, but the virtual destructor should be different in beta 3 vs beta 2.  That's a c++ which affect things when they're cast.  Google the appropriate use of virtual destructors in cpp.

---
If you find my post helpful please "like" it and "accept as a solution"
Please use plain text.
New Developer
MedNez
Posts: 67
Registered: ‎11-24-2008
My Device: Not Specified

Re: C++ ~App Destructor causes build error?

Right, I've done some googling, and am I correct in suggesting that in Beta 2, you defined the destructor in the header file:

 

~SoundPartyApp();

 And then the function in the class file:

 

SoundPartyApp::~SoundPartyApp()
{
  // Destroy the sound manager.
  delete mSoundManager;
}

 But in Beta 3, the virtual destuctor already exists:

 

virtual ~SoundPartyApp() {}

 

So I should be able to just expand that, e.g.

 

virtual ~SoundPartyApp() {
// do some important cleanup delete mSoundManager; }

 

I got the idea from here:

 

http://stackoverflow.com/questions/461203/when-to-use-virtual-destructors 

 

(see the comment in the code ''// Do some important cleanup')

 

Looks like this is the place to do it? 

Please use plain text.
Developer
Dredvard
Posts: 160
Registered: ‎01-27-2012
My Device: Playbook
My Carrier: Rogers

Re: C++ ~App Destructor causes build error?

If you want to relocate the code to the main app, just do this:

virtual ~SoundPartyApp();

And then in the main app do whatever you need to do:

 SoundPartyApp::~SoundPartyApp() {
// do some important cleanup delete mSoundManager; }
---
If you find my post helpful please "like" it and "accept as a solution"
Please use plain text.
New Developer
MedNez
Posts: 67
Registered: ‎11-24-2008
My Device: Not Specified

Re: C++ ~App Destructor causes build error?

Awesome. Worked perfectly, thanks!

 

Successfully updated some of the other Beta2 sample code now too - thank you.

Please use plain text.