[try Beta version]
Not logged in

 
how to use dlopen with C++ class object

Sep 13, 2016 at 10:27am
I have shared library that has class and class methods

I want to load this shared library at run time using dlopen ,

Can some one can tell me basic example of the same
Last edited on Sep 15, 2016 at 3:51am
Sep 13, 2016 at 10:34am
In the above code is there any problem and is there any

It is your code. You know whether or not your code has problems.

Please do not expect us to find the problems for you. Even if we are problem solvers, we have no clue.
Sep 13, 2016 at 11:03am
Yes there are problems - you forgot to delete b and have a memory leak.
Also if(b) is useless since new will throw bad_alloc if there is not enough memory.
Class A doesn't have a method setX.
Sep 13, 2016 at 12:18pm
Sorry my question was

In the above code is there any case where getA() can return a NULL pointer or invalid pointer . If yes what is valid check for it ?
Sep 13, 2016 at 1:38pm
There is no way that getA() could possibly return a null pointer. The object from B must of course be valid but checking the result of getA() doesn't make sense nonetheless.
Sep 13, 2016 at 1:46pm
In the above code is there any case where getA() can return a NULL pointer or invalid pointer.

The simple answer is no, as long as b is valid.

The longer answer is if b is not valid, then any reference returned by getA() is also invalid and will result in undefined behavior.
Consider:
1
2
3
4
5
  B * b = new B; 
  //  Do something with b 
  delete b;
  b = nullptr; 
  //  b is no longer valid 


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Last edited on Sep 13, 2016 at 1:48pm
Topic archived. No new replies allowed.