Hi All,
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.

Then in my ECLibOpen function i allocated  memory as shown below.

Err ECLibOpen(UInt16 refNum, UInt32 *clientContextP)
{
        ECLibGlobalsType *gP;
        Err err = errNone;
        Int16 originalOpenCount = 0;

        /* Error-check our parameters */
        ErrFatalDisplayIf(
                clientContextP == NULL, 
                "null context variable pointer");

        /* Initialize return variable */
        *clientContextP = 0;

        /* Get library globals */
        gP = PrvLockGlobals(refNum);

        /* Check if already open */
        if (!gP)
        {
                /* Allocate and initialize our library globals. */
                gP = PrvMakeGlobals(refNum);
                if ( !gP )
                        err = memErrNotEnoughSpace;
        }

        /* If we have globals, create a client context, increment open
         * count, and unlock our globals */
        if ( gP )
        {
                originalOpenCount = gP->openCount;
                
                
                globaltest=MemPtrNew(sizeof(GLOBALTEST));  //Here 
                

                err = PrvCreateClientContext(gP, clientContextP);
                if ( !err )
                        gP->openCount++;

                PrvUnlockGlobals(gP);

                /* If there was an error creating a client context and there  */
                /* are no other clients, free our globals */
                if ( err && (originalOpenCount == 0) )
                        PrvFreeGlobals(refNum);
        }

        return( err );
}


And EClibClose i am freeing


Err ECLibClose(UInt16 refNum, UInt32 clientContext)
{
        ECLibGlobalsType * gP;
        Int16 openCount;
        Int16 contextCount;
        Err err = errNone;

        gP = PrvLockGlobals(refNum);

        /* If not open, return */
        if (!gP)
        {
                /* MUST return zero here to get around a bug in system v1.x that
                 * would cause SysLibRemove to fail. */
                 
                 MemPtrFree(globaltest);
                return errNone;
        }

        /* Destroy the client context (we ignore the return code in this 
implementation) */
        PrvDestroyClientContext(gP, clientContext);

        /* Decrement our library open count */
        gP->openCount--;

        /* Error check for open count underflow */
        ErrFatalDisplayIf(gP->openCount < 0, "ECLib open count underflow");

        /* Save the new open count and the context count */
        openCount = gP->openCount;
        contextCount = gP->contextCount;

        PrvUnlockGlobals(gP);

        /* If open count reached zero, free our library globals */
        if ( openCount <= 0 )
        {
                /* Error check to make sure that all client contexts were 
destroyed */
                ErrFatalDisplayIf(contextCount != 0, "not all client contexts 
were destroyed");

                /* Free our library globals */
                PrvFreeGlobals(refNum);
        }
        else
        {
                /* return this error code to inform the caller
                 * that others are still using this library */
                err = ECLibErrStillOpen;
        }

        return err;
}


Can any body help me where i am doing wrong.


Thanks in advance,

Regards,
Harsha







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

Reply via email to