Hello friends,
i am facing issues while finding the reason for a segmentation fault in a CPP-application. Please let me know how to move forward to look for this error,

i have a doubt on the following code snippet as this CPP code uses malloc function instead of new.
can it be the reason for the segmentiaon fault?

LDAPMessageQueue* LDAPExtRequest::sendRequest(){
    DEBUG(LDAP_DEBUG_TRACE, "LDAPExtRequest::sendRequest()" << endl);
    int msgID=0;
    BerValue* tmpdata=0;
    if(m_data != ""){
    [B]    tmpdata=(BerValue*) malloc(sizeof(BerValue));[/B]
        tmpdata->bv_len = m_data.size();
   [B]     tmpdata->bv_val = (char*) malloc(sizeof(char) * (m_data.size()) );[/B]
        m_data.copy(tmpdata->bv_val, string::npos);
    }
    LDAPControl** tmpSrvCtrls=m_cons->getSrvCtrlsArray();
    LDAPControl** tmpClCtrls=m_cons->getClCtrlsArray();
    int err=ldap_extended_operation(m_connection->getSessionHandle(),
            m_oid.c_str(), tmpdata, tmpSrvCtrls, tmpClCtrls, &msgID);
    LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
    LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
    ber_bvfree(tmpdata);
    if(err != LDAP_SUCCESS){
        delete this;
        throw LDAPException(err);
    }else{
        m_msgID=msgID;
        return new LDAPMessageQueue(this);
    }
}

please help!

Recommended Answers

All 5 Replies

ermithun,

Wrap up source code with BB code tags. Read this article - How to use BB code tags?

It's difficult to guess but as you said :-

i have a doubt on the following code snippet as this CPP code uses malloc function instead of new.
can it be the reason for the segmentiaon fault?

So rewrite this code and see what happen?

LDAPMessageQueue* LDAPExtRequest::sendRequest(){
DEBUG(LDAP_DEBUG_TRACE, "LDAPExtRequest::sendRequest()" << endl);
int msgID=0;
BerValue* tmpdata=0;
if(m_data != ""){
tmpdata=(BerValue*) malloc(sizeof(BerValue));
tmpdata->bv_len = m_data.size();
tmpdata->bv_val = (char*) malloc(sizeof(char) * (m_data.size()) );
m_data.copy(tmpdata->bv_val, string::npos);
}
LDAPControl** tmpSrvCtrls=m_cons->getSrvCtrlsArray();
LDAPControl** tmpClCtrls=m_cons->getClCtrlsArray();
int err=ldap_extended_operation(m_connection->getSessionHandle(),
m_oid.c_str(), tmpdata, tmpSrvCtrls, tmpClCtrls, &msgID);
LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
ber_bvfree(tmpdata);
if(err != LDAP_SUCCESS){
delete this;
throw LDAPException(err);
}else{
m_msgID=msgID;
return new LDAPMessageQueue(this);
}
}

thanks Adatapost.
i have done what u have asked me to use. Please help me understand it.

thanks in advance

This problem actually occurs in CPP not in C. please advice

Thanks.

Print "Debug" to the screen at diffrent spots in the program. if you see the word "Debug" on the screen, you know its lower, if you don't see "Debug" its higher. I do this al the time befor I use a debugger. Good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.