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

Some error of pointer when use 'return_loan'

Recommended Posts

I am a beginer of DDS.
 
This is a very simple program,just like the example 'Tutorial'. Publisher sends some messages and subscriber gets them.
 
But subscriber can only receive one message ,after that the program could be crashed.
 
The code is as follows:
	for (DDS::ULong i = 0;i<msg1Seq->length();i++)      
	{
		top1 *msg1 = &(msg1Seq[i]);				
		cout<<"receive data:"<<msg1->content<<endl;
		fflush(stdout);
	}

	status = top1dr->return_loan(msg1Seq,info1Seq);       
	checkStatus(status,"pubsub::top1DataReader::return_loan");
 
When run to 'status = top1dr->return_loan(msg1Seq,info1Seq);', the program be crashed.
 
It shows:
 

 

Debug Assertion Failed!

 

Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

 

Someone says it means a wild pointer,but i don't know why.

 

Thank you.

 

 

Share this post


Link to post
Share on other sites

The full code is as follows:

#include<iostream>
#include<stdlib.h>
#include<string.h>

#include"ccpp_dds_dcps.h"
#include"CheckStatus.h"
#include"ccpp_top.h"

#ifndef _WIN32
#include <unistd.h>
#else
#include <Windows.h>
#endif


using namespace DDS;
using namespace pubsub;

#define TERMINATION_MESSAGE -1

int main()
{

	DomainParticipantFactory_var	dpf;
	DomainParticipant_var			dp;
	Topic_var						top1Topic;
	Subscriber_var					suber;
	DataReader_var					drer1;

	TopicQos						top1qos;
	SubscriberQos					subqos;

	top1TypeSupport_var				top1TS;
	top1DataReader_var				top1dr;
	top1Seq_var						msg1Seq = new top1Seq();
	SampleInfoSeq_var               info1Seq = new SampleInfoSeq();     
	ReturnCode_t					status;
	DomainId_t						domain = DOMAIN_ID_DEFAULT;

	bool                            terminated = false;
    const char *                    partitionName = "Communication";
    char  *                         top1TypeName = NULL;             


	dpf = DomainParticipantFactory::get_instance();
	checkHandle(dpf.in(),"DDS::DomainParticipantFactory::get_instance");
	dp = dpf->create_participant(domain,PARTICIPANT_QOS_DEFAULT,NULL,STATUS_MASK_NONE);      
	checkHandle(dp.in(),"DDS::DomainParticipantFactory::create_participant");

	top1TS = new top1TypeSupport();                     
	checkHandle(top1TS.in(),"new top1TypeSupport");
	top1TypeName = top1TS->get_type_name();
	status = top1TS->register_type(
		dp.in(),
		top1TypeName);
	checkStatus(status,"pubsub::top1TypeSupport::register_type");

	status = dp->get_default_topic_qos(top1qos);
	checkStatus(status,"DDS::DomainParticipant::get_default_topic_qos");

	top1Topic = dp->create_topic(           
		"pubsub_top1",                
		top1TypeName,                   
		top1qos,						
		NULL,							
		STATUS_MASK_NONE);			
	checkHandle(top1Topic.in(),"DDS::DomainParticipant::create_topic");       

	status = dp->get_default_subscriber_qos(subqos);
	checkStatus(status,"DDS::DomainParticipant::get_default_subscriber_qos");

    subqos.partition.name.length(1);       
    subqos.partition.name[0] = partitionName;

	suber = dp->create_subscriber(
		subqos,
		NULL,
		STATUS_MASK_NONE);
	checkHandle(suber.in(),"DDS::DomainParticipant::create_subscriber");

	drer1 = suber->create_datareader(
		top1Topic.in(),
		DATAREADER_QOS_USE_TOPIC_QOS,
		NULL,
		STATUS_MASK_NONE);
	checkHandle(drer1.in(),"DDS::Subscriber::create_datareader");

	top1dr = pubsub::top1DataReader::_narrow(drer1.in());
	checkHandle(top1dr.in(),"pubsub::top1DataReader::_narrow");

	cout<<"prepare to read data..."<<endl;

	while (!terminated) {

	status = top1dr->take(              
		msg1Seq,							
		info1Seq,						
		LENGTH_UNLIMITED,					
		ANY_SAMPLE_STATE,				
		ANY_VIEW_STATE,						
		ALIVE_INSTANCE_STATE);				
	checkStatus(status,"pubsub::top1DataReader::take");
	

	for (DDS::ULong i = 0;i<msg1Seq->length();i++)     
	{
		top1 *msg1 = &(msg1Seq[i]);				
		cout<<"receive data:"<<msg1->content<<endl;
		fflush(stdout);
	}

	status = top1dr->return_loan(msg1Seq,info1Seq);       
	checkStatus(status,"pubsub::top1DataReader::return_loan");


#ifdef USE_NANOSLEEP
        sleeptime.tv_sec = 0;
        sleeptime.tv_nsec = 100000000;
        nanosleep(&sleeptime, &remtime);
#elif defined _WIN32
        Sleep(100);
#else
        usleep(100000);
#endif
	}


	status = suber->delete_datareader(top1dr.in());
	checkStatus(status,"DDS::Subscriber::delete_datareader");

	status = dp->delete_subscriber(suber.in());
	checkStatus(status,"DDS::Participant::delete_subscriber");

	status = dp->delete_topic(top1Topic.in());
	checkStatus(status,"DDS::Participant::delete_topic");

	DDS::string_free(top1TypeName);

	status = dpf->delete_participant(dp.in());
	checkStatus(status,"DDS::DomainParticipantFactory::delete_participant");

	system("pause");

	return 0;

}

Share this post


Link to post
Share on other sites

Hi ,

 

Try to check msg1Seq is not null nor 0 length  before calling return loan

    status = top1dr->take(              
		msg1Seq,							
		info1Seq,						
		LENGTH_UNLIMITED,					
		ANY_SAMPLE_STATE,				
		ANY_VIEW_STATE,						
		ALIVE_INSTANCE_STATE);				
	checkStatus(status,"pubsub::top1DataReader::take");
	

	for (DDS::ULong i = 0;i<msg1Seq->length();i++)     
	{
		top1 *msg1 = &(msg1Seq[i]);				
		cout<<"receive data:"<<msg1->content<<endl;
		fflush(stdout);
	}

    if (msg1Seq->length() > 0)
    {
		status = top1dr->return_loan(msg1Seq,info1Seq);       
		checkStatus(status,"pubsub::top1DataReader::return_loan");
    }

 

take method is non-blocking .

Therefore , even no data coming , take will return and msg1Seq should be nothing .

 

 

Share this post


Link to post
Share on other sites

Unfortunately in my case the error occurs even if msg1Seg and info1Seq size is 1. I investigated further and it seems _BLOCK_TYPE_IS_VALID assert fails when trying to delete[] SampleInfoSeq, indicating heap is corrupt somehow. 

Share this post


Link to post
Share on other sites

Hi ,

 

So , will console printf data ? 

Why top1Seq_var and SampleInfoSeq_var ? 

They should be top1Seq and SampleInfoSeq , according to sample code .

 

And what OpenSplice version you use ? 

 

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  

×