• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 2, 文章 - 73, 評論 - 60, 引用 - 0
            數據加載中……

            [S60]ARM平臺獨有問題 Writable Static Data in DLLs

            [S60] ARM平臺獨有問題 Writable Static Data in DLLs
            2007-07-08 16:59
            在編譯arm平臺程序的時候,出現如下錯誤提示:
            ERROR: Dll 'AppName[UID].APP' has initialised data.
            或者:
            ERROR: Dll 'AppName[UID].APP' has uninitialised data.
            (擴展名APP的應用程序其實也是一個DLL。)

            而在為模擬器編譯的時候,這個問題不會出現。這曾經導致我在完成完整的設計,編碼和調試后,
            被迫放棄原有設計。

            從這條錯誤信息的字面意思是什么也看不出來的。
            initialised 和 uninitialised都一樣有問題。
            其實真正的含義是Dll里存在可寫的全局變量。

            大家知道在程序運行的時候,DLL只會被裝載一次。在Windows平臺,每個進程都有自己獨立的DLL空間。也就是說,不同進程裝載同一個DLL,互相之間是獨立的。只有在一個進程內,才是共享的。但是S60平臺的設計是所有進程都共享同一個DLL空間。這樣的設計顯然是出于節約內存的目的,是很有必要的。但是這樣就帶來一個問題,那就是DLL里不可以有可寫的全局變量,否則就要造成混亂。A進程對變量的改寫會直接影響到B進程,這是程序設計者所不愿意看到的。所以,S60平臺的編譯器就禁止了在DLL內申明可寫全局變量。但是全局變量還是可以用的,只要加上const申明即可。

            一般來說,在做DLL設計的時候,的確不鼓勵使用可寫全局變量。即使是windows平臺,DLL的可寫全局變量也會在不同模塊之間帶來問題。當遇到這個編譯器錯誤的時候,應該設法修改設計,回避使用全局變量。

            但是因為APP實際上也是DLL,這就導致連S60的主程序也不能使用可寫的全局變量,這個在某些時候就成了問題,全局變量畢竟是一個重要的實現手段。對此,S60提供了線程局部存儲(
            thread local storage)來解決問題。
            TLS的關鍵是兩個函數:
            void Dll::SetTls(void*)和void* Dll::Tls()
            SetTls用于將任意類型的指針保存到線程局部存儲中,而Tls()則取出該指針。
            指針指向在堆上分配的一塊內存。一個線程只能有一個局部存儲變量。所以,如果你有很多全局變量,就要定義一個結構,把所有的全局變量封裝在其中。這是挺別扭的,不過S60 3rd據說就支持dll的可寫全局變量了。

            tls樣例代碼:

            設置
            GlobalData* p = new GlobalData();
            if ( p )
            {
               Dll::SetTls( p );
            }

            使用
            GlobalData* p = (GlobalData*) Dll::Tls();

             

            在Symbian上如何定義全局變量

            方法1(推薦)把這個變量定義成AppUi類的私有成員,在創建view時將這個變量傳引用(或傳指針)到view中,這樣view就能隨時訪問它了。

            方法2.把這個變量定義成AppUi類的私有成員,并為它寫公共的訪問函數
            // CMyAppUi
            public: // new methods
               TInt Share(); // return iShare
            private:
            JAVA手機網[www.cnjm.net]   TInt iShare;
            在View里通過下面的方式訪問這個變量:
            // 如果View繼承自CAknView
            CMyAppUi* appUi = static_cast<CMyAppUi*>(AppUi());
            appUi->Share(); // :)
            // 如果View是其它類型
            CMyAppUi* appUi = static_cast<CMyAppUi*>(CCoeEnv::Static()->AppUi());
            appUi->Share(); // :)
            方法3.使用單態類,參考諾基亞論壇上的文檔:
            Tip Of The Month: How To Implement A Singleton Class In Symbian OS



             

            How to implement a singleton class in Symbian OS        ID: TTS000222

            Version 1.1
            Published at www.forum.nokia.com on October 19, 2006.

            Overview

            The singleton pattern is one of the best-known patterns in software engineering. Essentially, a singleton is a class which only allows a single instance of itself to be created, and usually gives simple access to that instance.

            How to use thread local storage (TLS) to implement a singleton class

            In Symbian OS, each DLL that is loaded in each thread has a machine word of thread-specific memory that can be written to and read — but no other static memory, which is why you can't have static class member variables. Since static class member variables are usually used to implement the singleton pattern, in Symbian OS we have to get around this, for instance by using TLS.

            The following code demonstrates a singleton object whose NewL function uses TLS to test whether an object of its own type has been created. If it has, it simply returns the pointer stored in TLS, converted to its own type. If not, it instantiates an object of its own type, stores it in TLS, and then returns it.

            Note that this assumes that no other class in the DLL that includes this class uses TLS. If this is not the case, you must write a singleton manager class, which uses TLS to store a pointer to a structure of pointers to all the singleton classes that the program needs.

            Example 1: Singleton implementation based on TLS

              ==============

              CMySingleton.h

              ==============

             

              class CMySingleton : public CBase

                  {

              public: // constructor and destructor

                  static CMySingleton* NewL();

                  virtual ~CMySingleton();

              private: // constructors

                  CMySingleton(); // private because of the singleton pattern; it is

                                  // guaranteed that only NewL will call it

                  void ConstructL();

              public: // other functions

                  ...

              private: // other functions

                  ...

              private: // data

                  ...

                  }

             

              ================

              CMySingleton.cpp

              ================

             

              CMySingleton::CMySingleton* NewL()

                  {

                  CMySingleton* singleton;

                  // Check thread local storage:

                  if ( Dll::Tls() == NULL )

                      {

                      // TLS is still null, which means that no CMySingleton has

                      // been instantiated yet.  Do so now, and return that

                      // instance:

                      singleton = new ( ELeave ) CMySingleton();

                      CleanupStack::PushL( singleton );

                      singleton->ConstructL();

                      CleanupStack::Pop( singleton );

                      // Store a pointer to the new instance in thread local storage:

                      TInt err = Dll::SetTls( static_cast<TAny*>( singleton ) );

                      if ( err == KErrNone )

                          {

                          return singleton;

                          }

                      else

                          {

                          delete instance;

                          User::Leave( err );

                          return NULL;

                          }

                      }

                  else

                      {

                      // CMySingleton has been instantiated once already, so return

                      // that instance:

                      singleton = static_cast<CMySingleton*>( Dll::Tls() );

                      return singleton;

                      }

                  }

             

            TLS on S60 3rd Edition

            Since applications from S60 3rd Edition onwards are implemented as EXE programs, Dll::Tls() is not available anymore. Instead, TLS functionality is implemented in UserSvr class (e32svr.h):

            static TInt DllSetTls(TInt aHandle, TAny *aPtr);

            static TAny *DllTls(TInt aHandle);

            static void DllFreeTls(TInt aHandle);

            Note that EXE programs can contain writeable static data, but this is not recommended to be used except as a last resort.

            Using class CCoeStatic to implement a singleton class

            A simpler way of implementing singletons than using TLS is possible for those classes which use the CCoeEnv class. Since CCoeEnv is a part of the UI control framework, this concerns only applications, not application engines.

            This applies also to S60 3rd Edition and later editions.

            Example 2: Singleton implementation based on CCoeStatic

              ==============

              CMySingleton.h

              ==============

             

              /**

               * Example implementation of a singleton class by means of inheriting

               * from CCoeStatic.

               */

              class CMySingleton : public CCoeStatic

                  {

             

              public: // constructors and destructor   

             

                  /**   

                   * Returns an instance of this class. When called for the first

                   * time, a new instance is created and returned.  After that,

                   * calling InstanceL returns the same instance that was created

                   * earlier.

                   *  

                   * @return A pointer to a CMySingleton object   

                   */   

                  static CMySingleton* InstanceL();   

             

              private: // constructor

             

                  /**   

                   * Default constructor is private because we are using the

                   * singleton design pattern.

                   */   

                  CMySingleton();   

             

                  ...

             

                  }

             

             

              ================

              CMySingleton.cpp

              ================

             

              // -------------------------------------------------------------------------

              // CMySingleton::CMySingleton

              // C++ default constructor. It is private because we are using the

              // singleton design pattern.

              // -------------------------------------------------------------------------

              CMySingleton::CMySingleton()

                  : CCoeStatic( KUidMySingleton )

                  {

                  }

             

              // -------------------------------------------------------------------------

              // CMySingleton::InstanceL

              // Returns an instance of this class. When called for the first time,

              // a new instance is created and returned.  After that, calling

              // InstanceL returns the same instance that was created earlier.

              // Note that the UID passed to CCoeEnv::Static needs to be unique.

              // -------------------------------------------------------------------------

              CMySingleton* CMySingleton::InstanceL()

                  {

                  CMySingleton* instance = static_cast<CMySingleton*>

                      ( CCoeEnv::Static( KUidMySingleton ) );

                  if ( !instance )

                      {

                      instance = new ( ELeave ) CMySingleton;

                      CleanupStack::PushL( instance );

                      instance->ConstructL();

                      CleanupStack::Pop();

                      }

                  return instance;

                 }

            posted on 2008-06-02 20:23 郭天文 閱讀(965) 評論(0)  編輯 收藏 引用 所屬分類: S60

            国产精品日韩深夜福利久久| 波多野结衣久久| 久久久久人妻精品一区二区三区| 久久精品国产亚洲AV影院 | 99久久国产综合精品成人影院| 欧美久久久久久午夜精品| 久久久久夜夜夜精品国产| 一级a性色生活片久久无| 国产激情久久久久影院| 四虎影视久久久免费| 久久99精品久久只有精品| 久久久这里有精品| 久久久久国产精品三级网 | 欧美久久亚洲精品| 国产三级久久久精品麻豆三级 | 久久综合亚洲色一区二区三区| 久久久亚洲欧洲日产国码二区 | 人妻精品久久久久中文字幕一冢本| 日本精品久久久久影院日本| 浪潮AV色综合久久天堂| 日韩影院久久| 99久久精品免费看国产一区二区三区 | 久久综合国产乱子伦精品免费| 久久青青草原精品国产不卡| 久久精品国产91久久麻豆自制 | 99久久精品国产毛片| 久久精品人成免费| 色欲综合久久躁天天躁蜜桃| 亚洲中文字幕久久精品无码喷水| 伊人色综合九久久天天蜜桃| 国产精品热久久无码av| 久久91精品国产91久久麻豆| 久久久久久亚洲精品成人 | 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 久久人人爽人人爽人人片AV麻豆| 久久国产免费观看精品| 国产精品青草久久久久婷婷 | 久久久久99精品成人片三人毛片| 情人伊人久久综合亚洲| 亚洲伊人久久大香线蕉苏妲己| 久久国产成人精品麻豆|