0

Have a problem when try to get a value of a property via Javascript using a NPAPI plugin; During the debugging I see that all the chain of functions (HasProperty, HasMethod, and GetProperty) are called. More over I see that during calling GetProperty I set the new values into the result parameter. But after leaving GetProperty I get an exception and can't understand what the reason of it.

May FireFox call some additional functions which I forgot to initialize?

Thanks in advance

My code is:

// static function which calls Get_Property for the instance of CScriptableNPObject
bool CScriptableNPObject::NP_GetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result)
{
    m_Logs.WriteLogs(10, _T("Enter the CScriptableNPObject::NP_GetProperty()"));

    return ((CScriptableNPObject *)npobj)->GetProperty(name, result);
}

// just converter name from NPIdentifier to char *
bool CScriptableNPObject::GetProperty(NPIdentifier name, NPVariant *result)
{
    NPUTF8 *pszProperty = m_pNPNFuncs->utf8fromidentifier(name);

    return GetProperty(pszProperty, result);
}

// checking the dictionary of properties, if property exists put its value into the result
bool CScriptableNPObject::GetProperty(NPUTF8 *pszProperty, NPVariant *result)
{
    VOID_TO_NPVARIANT(*result);

    JSPropertiesMap::iterator it = m_JSProperties.find(pszProperty);

    if (it == m_JSProperties.end())
        return false;

    NPUTF8 *pszNewPropertyValue = new NPUTF8[it->second->value.stringValue.UTF8Length + 1];
    sprintf(pszNewPropertyValue, it->second->value.stringValue.UTF8Characters);

    STRINGZ_TO_NPVARIANT(pszNewPropertyValue, *result);

    return true;
}
pimanych
  • 21
  • 3
  • 1
    No code sample, no details on what the exception is, no indication of what has been tried... nope, I don't think we can help you. Perhaps if you added some more detailed information? – taxilian Dec 11 '12 at 02:36
  • I have provided a chain of functions – pimanych Dec 11 '12 at 04:33

1 Answers1

1

You need to use NPN_MemAlloc():

NPUTF8* newValue = NPN_MemAlloc(length + 1);
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
  • Heh. Curse you, my friend, I was just writing that up :-P You win again this round... =] – taxilian Dec 11 '12 at 04:37
  • 1
    @tax: sorry, would i have known... ;) – Georg Fritzsche Dec 11 '12 at 04:43
  • Yep, all works great. So it's harmful to allocate memory for browser using not in the browser's heap? – pimanych Dec 11 '12 at 04:56
  • 1
    @pim: the problem is that you pass the ownership to the browser, so the browser has to free it. The runtimes for browser and plugins (and hence heap management) could differ, hence the requirement. Also, there are optimization possibilities as mentioned in the article. – Georg Fritzsche Dec 11 '12 at 05:35