Jump to content
OpenSplice DDS Forum
Sign in to follow this  
bonjefir

Dynamic allocation

Recommended Posts

Hi

 

I have written a very simple program containing one class which have only one pointer to domain participant. Given below is the mentioned program:

#include <QCoreApplication>
#include "publisher.h"
#include <QThread>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    return a.exec();
}
#ifndef PUBLISHER_H
#define PUBLISHER_H

#include <dds/dds.hpp>
#include "../IDL/myIDL/myIDL_DCPS.hpp"

class Publisher
{
public:
    Publisher();
    dds::domain::DomainParticipant *dp;
};

#endif // PUBLISHER_H

#include "publisher.h"

Publisher::Publisher()
{
   dp = new dds::domain::DomainParticipant(org::opensplice::domain::default_id());
}

But I can not compile this program. The error is: 

"error: 'static void* dds::core::Reference<DELEGATE>::operator new(size_t) [with DELEGATE = org::opensplice::domain::DomainParticipantDelegate; size_t = long unsigned int]' is private

Where does this error come from and what should be done for solving it?

 

Thanks in advance for your help.

Bonjefir

 

Share this post


Link to post
Share on other sites

I don't exactly know where the code fails, but avoid raw pointers at all costs.

Either do:

 

In the Header:

 

dds::domain::DomainParticipant dp;

 

In the Source:

 

Publisher::Publisher() :  dp(org::opensplice::domain::default_id()) {

 

}

 

Or use smart pointers.

Share this post


Link to post
Share on other sites

Dear robuser,

 

Thanks for your answer. But I want to know whether it is possible to define a pointer like above or not. As far as I know, if I can not define a pointer to any DDS entities like above, I can not define a small pointer to them either. Do you have any idea to do that?

 

Best Regards,

Bonjefir

Share this post


Link to post
Share on other sites

They deliberately made the new operator private on the DomainParticipant class, so you cannot create a domain participant on the heap. There are most likely complex internal technical reasons why they did this. You can create a pointer to a data writer for instance without any problems. 

Share this post


Link to post
Share on other sites

Hi robuser,

 

Thanks for your answer. I tried the same code for defining topic, publisher and datawriter but got the same error. I think your are right about the private nature of new operator.

 

Best Regards,

Bonjefir

Share this post


Link to post
Share on other sites

This works fine:

 

std::shared_ptr<dds::pub::DataWriter<Topic> > dw;

dw = std::make_shared<
dds::pub::DataWriter<topic> >(
dds::pub::DataWriter<topic>(pub, topic,
dwqos));
 
or
 
dw = std::make_shared<
dds::pub::DataWriter<topic> >(pub, topic, dwqos);
 
Never use new directly. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×