GNU LD: dynamic library loading a vector of strings crashes - but array of strings do not.

Rakesh Kumar <[email protected]>
Newsgroups gmane.comp.gnu.utils
Organization http://groups.google.com
Message-ID <97d7b6f4-5e7c-40dd-99c3-b4cb23221232@d27g2000prf.googlegroups.com>
Hi -
  I have an issue with a vector of strings in a dynamic library.
I am posting a scaled down version of the code. Sorry - if this one is
too long .

dynstr.c :
-----------
#include "dynstr.h"
#include "dynapi.h"

#include <string>
#include <vector>
#include <iostream>

using namespace std;

dynstr::dynstr() {
}

dynstr::~dynstr() {
}

void dynstr::allocVectorOfStrings() {
    std::vector<std::string> * vt = new std::vector<std::string>();
    vt->reserve(50);
    for (size_t i = 0; i < vt->capacity(); ++i) {
        std::cout << "We are probably ok" << i << "\n";
        vt->operator[](i).reserve(40);
    }
    delete vt;
}

void dynstr::allocArrayOfStrings() {
    std::string vt[50];
    for (size_t i = 0; i < 50; ++i) {
        std::cout << "We are probably ok" << i << "\n";
        vt[i].reserve(40);
    }
}

void run() {
    dynstr * p = new dynstr;
    p->allocArrayOfStrings();
    delete p;
}

void run1() {
    dynstr * p = new dynstr;
    p->allocVectorOfStrings();
    delete p;
}



dynstr.h:
-----------
#ifndef DYNSTR_H_
#define DYNSTR_H_

class dynstr {
public:
    dynstr();
    virtual ~dynstr();

    void allocVectorOfStrings();
    void allocArrayOfStrings();
};

#endif /*DYNSTR_H_*/


dynapi.h
------------
#ifndef DYNAPI_H_
#define DYNAPI_H_

extern "C" {

 	void run();


	void run1();

}
#endif /*DYNAPI_H_*/


strcrash.cpp:
----------------
#include <dlfcn.h>
#include <iostream>
#include <cstdlib>

int main(int argc, char * argv[]) {
        if (argc != 2)
        {
                std::cout << "Enter either run / run1 as the argument
\n";
                exit(EXIT_FAILURE);
        }

        void * handle = dlopen("libdynstr.so", RTLD_NOW);

        if (!handle) {
                dlerror();
                exit(EXIT_FAILURE);
        }

        typedef void (* ptrtype)();

        ptrtype pf = (ptrtype) dlsym(handle, argv[1]);

        pf();

        dlclose(handle);

        return EXIT_SUCCESS;
}

Makefile:

libdynstr.so:    dynstr.cpp    dynstr.h    dynapi.h
    g++ -Wall -g -Werror -o libdynstr.so -fpic -shared dynstr.cpp

strcrash:    libdynstr.so    strcrash.cpp
    g++ -Wall -g -Werror -o strcrash    strcrash.cpp libdynstr.so -ldl


Execution:

./strcrash run
   This creates an array of strings with no issues whatsoever.

./strcrash run1
   This creates a vector of strings and crashes at the first call when
we try to reserve memory
for the first string in the vector with a SIGSEGV .

   I am unable to understand the behavior here.  This is a scaled down
version of the problem in actual code. Can someone explain me what
could be going wrong here.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.