hvreddy wrote:
> 
> As every body knows we can't use global variables directly in the 
> shared library so i created a structure as a global variable and 
> used global variables inside structure.
> Then i tryed to allocate memory for the structure variable in 
> LibOpen finction using MemPtrNew and free in LibClose function.
> Library compiled fine and when i try to open it in my application 
> it is crashing.
> here is my code
> 
> typedef struct
> {
> 
> int x ;
> int y ;
> float z;
> }GLOBALTEST;
> 
> GLOBALTEST *globaltest;
> 
> It is there in the global scope.

And that's the problem: you can't use any globally-scoped variables.  That's 
what a global is.  That your global variable is a pointer to a structure 
allocated by MemPtrNew is irrelevant; the pointer you're using is stored in a 
global!

Shared libraries should retrieve the SysLibTblEntryType structure for the 
library's reference number and store a pointer to its pseudo-global data in it. 
 That's what the reference number is there for.  For example:

    Err LibOpen(UInt16 libRefNum)
    {
        GLOBALTEST* globaltest = MemPtrNew(sizeof *globaltest);
        SysLibTblEntryType* libEntryP = SysLibTblEntry(libRefNum);
        libEntryP->globalsP = globaltest;
        /* ... other opening code ... */
    }

See PalmSource's SampleLib sample code.

- James
-- 
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/

Reply via email to