segmentation fault in C++ example

asked 2018-07-17 20:25:02 +0100

olk gravatar image

Hi, I get an segmentation fault from XComponentLoader::loadComponentFromURL() - could you tell me how to fix it, please? I'm using libreoffice SDK 6.0.5 on Arch Linux.

#include <stdio.h>

#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/lang/XMultiComponentFactory.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>

#include <cppuhelper/bootstrap.hxx>

#include <osl/file.hxx>
#include <osl/process.h>
#include <sal/config.h>
#include <sal/main.h>

#include <bootstrap.hxx>

using namespace com::sun::star::bridge;
using namespace com::sun::star::frame;
using namespace com::sun::star::lang;
using namespace com::sun::star::uno;
using namespace cppu;
using namespace rtl;

// a procedure for what the so called boostrap
Reference< XMultiServiceFactory > ooConnect(){
   // create the initial component context
   Reference< XComponentContext > rComponentContext = defaultBootstrap_InitialComponentContext();

   // retrieve the servicemanager from the context
   Reference< XMultiComponentFactory > rServiceManager = rComponentContext->getServiceManager();

   // instantiate a sample service with the servicemanager.
   Reference< XInterface > rInstance =  rServiceManager->createInstanceWithContext(
         OUString::createFromAscii(""), rComponentContext);

   // Query for the XUnoUrlResolver interface
   Reference< XUnoUrlResolver > rResolver{ rInstance, UNO_QUERY };
   if ( ! ) {
      printf( "Error: Couldn't instantiate service\n" );
      return nullptr;
   try {
      // resolve the uno-url
      rInstance = rResolver->resolve( OUString::createFromAscii(
         "uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager") );

      if ( ! ) {
         printf("StarOffice.ServiceManager is not exported from remote counterpart\n");
         return nullptr;

      // query for the simpler XMultiServiceFactory interface, sufficient for scripting
      Reference< XMultiServiceFactory > rOfficeServiceManager{ rInstance, UNO_QUERY };

      if ( ! ) {
            printf("XMultiServiceFactory interface is not exported for StarOffice.ServiceManager\n");
            return nullptr;
        return rOfficeServiceManager;
   catch ( Exception const& e) {
      OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US);
      printf("Error: %s\n", o.pData->buffer);
      return nullptr;
   return nullptr;

//Listing 0b Again our starting main Code
int main() {
    OUString sAbsoluteDocUrl, sWorkingDir, sDocPathUrl;
    osl::FileBase::getAbsoluteFileURL( sWorkingDir, sDocPathUrl, sAbsoluteDocUrl);

    //retrieve an instance of the remote service manager
    Reference< XMultiServiceFactory > rOfficeServiceManager = ooConnect();
    if ( !  ){
        printf("Not sucessfully connected to the office\n");
        return 1;

//get the desktop service using createInstance returns an XInterface type
    Reference< XInterface  > Desktop = rOfficeServiceManager->createInstance(
        OUString::createFromAscii("") );

//query for the XComponentLoader interface
    Reference< XComponentLoader > rComponentLoader{ Desktop, UNO_QUERY };
    if ( ! ) {
            printf("XComponentloader not successfully instanciated\n");
            return 1;

//get an instance of the spreadsheet
    Reference< XComponent > xcomponent = rComponentLoader->loadComponentFromURL(
            Sequence< ::com::sun::star::beans::PropertyValue >{} );
    // add code here
    return 0;
edit retag flag offensive close merge delete


Perhaps rComponentLoader was not initialized correctly. What documentation or examples did you look at to write this code? The example code from DocumentLoader.cxx in the SDK shows Reference < XDesktop2 > xComponentLoader = Desktop::create(xComponentContext);.

Jim K gravatar imageJim K ( 2018-07-18 16:02:32 +0100 )edit

Also if LibreOffice is 32-bit then use the 32-bit SDK as well, with versions that match.

Jim K gravatar imageJim K ( 2018-07-18 16:05:42 +0100 )edit