2

I have a global variable defined in a c-library [ex: int globalcount = 0] and in library initialization function [say library_init] globalcount is incremented by 1.

The issue here is : when library open with dlopen and appln call library_init function and closed the libs using dlclose then again dlopen call is reseting the globalcount to 0.

I want to know how to prevent reset of global variable [I want globalcount must be 1 not 0] when next dlopen is called because process is not exit so global variable also must not reset.

If library linked to proceess using -l at link time , I am not facing the abv issue. the issue only when libs open using dlopen/dlclose [multiple times]

OS is Linux Prog Lan : C Compiler : gcc

How to do, memory in the data segment must not freed when dlclose called ?

3
  • This is an OS issue rather than a language issue. It may help (though I doubt it) if you tell us the OS. What about persisting the state of the variable to a file when dlclose() is called. Commented Nov 5, 2011 at 6:50
  • OS is Linux, We don't want to save any think into a file, I think its not a good way . Is any way using static storage class can I solve abv issue Commented Nov 5, 2011 at 7:05
  • I think you should just ask yourself the question why you ever do dlclose on that library. I can't figure out much use for that. Commented Nov 5, 2011 at 8:43

2 Answers 2

1

When you call dlclose your dll is unloaded that means any memory in the data segment of your library is freed. This is the reason why the value is reset to 0. You could use shared memory to have a variable that is persistent even when your library gets unload.

Sign up to request clarification or add additional context in comments.

7 Comments

Is any simplest method , such that I can make a verialbe permenant for entire process life ?
@syedsma as I said: when the .so is unload all memory ocupied by it is freed. There is no way around it. It is how shared objects are designed.
How to do memory in the data segment must not freed when dlclose called ?
@Syedsma One last time: There is no way. It is freed by the operating system. You can use shared memory.
Thanks for u r Response:). But why dynamic memeory allocated using malloc inside the library will not automaticaly freed when dlclose called , valgrind/purify report this memory as leaked
|
0

Use RTLD_NODELETE flag while loading library using dlopen. the global and global static variables persist in memory when we use this flag. Even if the reference count of the library becomes zero.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.