×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    44

    Unhappy Use global variable in s3rd

    Hi, guys. I have to use the global variable in s3rd. And I use the static class UserSvr to implete this. But now i find a problem : some time it work, some not.

    My code is (typ_IdMapGlobal is my globals):
    Code:
    //globals.h
    typedef struct GlobalStruct
    {
    	typ_IdMapGlobal* iIdMapGlobals;
    }typ_Globals;
    
    class Globals
    {
    public:
    	static void InitGlobal();
    	static void DestroyGlobal();
    	static typ_Globals* GetGlobal();
    };
    
    #include <e32std.h>
    #include <e32svr.h>
    
    const TInt KGlobalHandle = 0x005167;
    
    void Globals::InitGlobal()
    {
    	typ_Globals* globals = static_cast<typ_Globals*>(UserSvr::DllTls(KGlobalHandle));
    
    	if(globals)
    		return;
    
    	globals = (typ_Globals *)smlLibMalloc(sizeof(typ_Globals));
    	if(globals == NULL)
    		return;
    	smlLibMemset(globals, 0, sizeof(typ_Globals));
    	
    	/*Add Id Map Global*/
    	globals->iIdMapGlobals = (typ_IdMapGlobal *)smlLibMalloc(sizeof(typ_IdMapGlobal));
    	if(globals->iIdMapGlobals != NULL)
    	{
    		smlLibMemset(globals->iIdMapGlobals, 0, sizeof(typ_IdMapGlobal));
    	}
    	
    	User::LeaveIfError(UserSvr::DllSetTls(KGlobalHandle, globals));
    
    	return;
    }
    
    void Globals::DestroyGlobal()
    {
    	typ_Globals* globals = static_cast<typ_Globals*>(UserSvr::DllTls(KGlobalHandle));
    
    	if(globals)
    		return;
    
    	smlLibFree(globals->iIdMapGlobals);
    	globals->iIdMapGlobals = NULL;
    
    	smlLibFree(globals);
    	globals = NULL;
    	
    	UserSvr::DllFreeTls(KGlobalHandle);
    
    	return;
    }
    
    typ_Globals* Globals::GetGlobal()
    {
    	return static_cast<typ_Globals*>(UserSvr::DllTls(KGlobalHandle));
    }
    I want to reuse this code when i need globals once more, at that time i just need to modify some code.

    I put the code InitGlobal and DestroyGlobal in the C**Document, when i need the globals, i just malloc the memory and put assign the value iIdMapGlobals, after usage, i release this memory.

    This code run successfully yesterday(i just test the code), but today i find it not works. I trace and find the method Globals::GetGlobal() return NULL!!!! And i can't find the reason why it happen. Most important it happened at the same place.

    Please give me a hand, thank you!

  2. #2
    Regular Contributor
    Join Date
    Aug 2004
    Posts
    295

    Re: Use global variable in s3rd

    Why would you need to use TLS on 3rd ed?

  3. #3
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,748

    Re: Use global variable in s3rd

    Quote Originally Posted by simo.salminen View Post
    Why would you need to use TLS on 3rd ed?
    Note that the original purpose of TLS is not just providing workaround for global variables on platforms where they are not allowed to use: http://en.wikipedia.org/wiki/Thread-local_storage

  4. #4
    Super Contributor
    Join Date
    Nov 2005
    Location
    London
    Posts
    584

    Re: Use global variable in s3rd

    Have you tried using the Dll class (http://www.symbian.com/developer/tec...html#%3a%3aDll) for TLS rather than UserSvr?

    S60 documentation points you at UserSvr for some reason, but Dll hasn't gone away and works on 3rd Edition (at least it did last time I tried it).

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    44

    Re: Use global variable in s3rd

    Quote Originally Posted by stichbury View Post
    Have you tried using the Dll class (http://www.symbian.com/developer/tec...html#%3a%3aDll) for TLS rather than UserSvr?

    S60 documentation points you at UserSvr for some reason, but Dll hasn't gone away and works on 3rd Edition (at least it did last time I tried it).
    First, thank you for your opinion. But in some document point out that DLL does not work in s60 3rd. And some guys also tell me that. Of course, i test it in s60 3rd, and the compiler explain. May be i use it in the wrong way, can you tell me how to use DLL.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    44

    Re: Use global variable in s3rd

    Actually, now i know the reason may be "DLL code running on behalf of one thread does not see the same word when running on behalf of another thread", actually in the code use another thread in some place. But i am also confuse that why sometime the another thread can also visit the globals? And I am afraid I have to rewrite the code.

  7. #7
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,748

    Re: Use global variable in s3rd

    Global variables and Thread-Local Storage are two very different things, that is the cause.
    Every thread sees global variables, since they are global variables.
    Thread-Local Storage is related to a given thread, that is the purpose. SetTls-Tls stores-retrieves a per-thread (and per-dll) value, intentionally.

Similar Threads

  1. quick 'n urgent: howto set a global variable?
    By chall3ng3r in forum Symbian
    Replies: 3
    Last Post: 2006-12-07, 21:54
  2. Global Variable problem in porting C code
    By avinniva in forum Symbian
    Replies: 2
    Last Post: 2006-08-10, 22:35
  3. accessing global variable from thread
    By Larry101 in forum Symbian
    Replies: 5
    Last Post: 2006-07-26, 16:05
  4. How to declare a global variable?
    By supertoync in forum Symbian
    Replies: 0
    Last Post: 2005-11-12, 21:34
  5. How to use global writable variable in app?
    By lanceking in forum Symbian
    Replies: 1
    Last Post: 2005-06-09, 09:27

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×