Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ STL Linux Map CORE
dll.cpp:
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif
 
void show(){
    fprintf(stdout,"%s","dll show call.");
}
 
#ifdef __cplusplus
}
#endif
dll_1.cpp:
#include "dll.h"

map<string,value> g_map;
 
int register_funk(string xx,fn_cmd_handler_t fn, string dex) { // removed obscenity
    fprintf(stdout,"%s","core begin\n");
    fprintf(stdout,"%u",g_map.size());
    fprintf(stdout,"%s","core end\n");
    fprintf(stdout,"-------fn-%p",fn);
    fprintf(stdout,"%s","----\n");
    fprintf(stdout,"%s","----\n");
    g_map[xx].fn = fn; //---------->core at this, why ?? who can tell me????
    g_map[xx].des = dex;
    return 0;
}
dll_2.cpp:
#include "dll.h"

int samxxxx( int machine_id, vector<string> &vecValue );
 
static int __inss = register_funk("xxxx",samxxxx,"funk"); // removed obscenity

int samxxxx( int machine_id, vector<string> &vecValue )
{
    fprintf(stdout,"%s","ssss--------->");
    return 0;
}
dll.h:
#ifndef __xxxx__
#define __xxxx__
 
#include ,map>
#include <vector>
#include <string>
#include <stdio.h>
using namespace std;
typedef int ( *fn_cmd_handler_t )( int machine_id, vector<string> &vecValue );
 
typedef struct {
    fn_cmd_handler_t fn;
    string des;
}VALUE;
 

int register_funk(string xx,fn_cmd_handler_t fn, string dex);  // removed obscenity

#endif
//and i build this so use
//g++ -g -O2 -shared -Wall -o a.so *.cpp
 
//the follow is exe file
//main.cpp
#include <dlfcn.h>
#include <stdio.h>

int main()
{
    void *handle = dlopen("/home/samuel/Temp/funk/a.so", RTLD_LAZY);
    fprintf(stdout, "%s\n", dlerror());
    dlclose(handle);
    return 0;
}
//i build this use
//g++ main.cpp -rdynamic -ldl
when i type this in centos
>./a.out
it core
who can tell me why?
thanks a lot Smile | :)
Posted 29-Oct-12 19:17pm
Edited 30-Oct-12 3:14am
v7
Comments
CPallini at 30-Oct-12 7:13am
   
The code you posted is not the actual one (e.g. 'value' type is not declared).
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Both your static int __inss and map<string,value> g_map; are global variables and the order of their initialization isn't specified because they reside in separate .cpp and .obj files. In your case you were not lucky and __inss was initialized before g_map so g_map is used before its actual initialization because the initialization of __inss uses g_map. g_map.size() doesn't crash because its just substraction of two pointers that doesn't crash even if the pointers are uninitialized or zero but putting an item to the map is more complex to survive. You can solve the problem by putting g_map and __inss to the same .cpp file and making sure that g_map is above __inss becuase inside the same .cpp (compilation unit) the initialization order is top to bottom while the destruction is bottom to top.
 
If you were lucky and g_map would initialize before __inss then you would probably face this error at a later point in your project when your linker decides to swap the initialization order.
 
My general advice: Don't use global variables at all if you don't need them. Declare a main class for your DLL, and instatiate just that as a global variable and then put everything into that class as member variables. Something like this:
#include "OtherClass1.h"
#include "OtherClass2.h"

class OtherClass3;
 
class CDll
{
public:
   CDll();
   ~CDll();
   void func();
   void func2();
 
private:
   std::map<x,y> m_Map;
   int m_BlahBlah;
   int m_XXX;
   OtherClass1 m_OtherClass1;
   OtherClass2 m_OtherClass2;
   OtherClass3* m_OtehrClass3;
};
 
// The only static variable in the DLL
CDll g_Dll;
 

// functions exported by the shared lib:
void shared_dll_func()
{
   g_Dll.func();
}
 
void shared_dll_func2()
{
   g_Dll.func2();
}
 
...
  Permalink  
v5
Comments
CPallini at 30-Oct-12 8:46am
   
Good point, my 5.
pasztorpisti at 30-Oct-12 8:48am
   
Thank you!
denghuancong at 31-Oct-12 5:10am
   
nice , thanks. :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You have not put any values into your map so you cannot use an indexer to refer to an element within it. My bad.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

no , when i use g_map[xx],it will auto create key=xx and a value.
  Permalink  
Comments
Richard MacCutchan at 30-Oct-12 8:00am
   
You need to step into this code with your debugger to see what is happening. As CPallini says, the code above does not have a definition for value used in your map.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 230
1 PIEBALDconsult 150
2 DamithSL 125
3 BillWoodruff 108
4 Garth J Lancaster 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 30 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100