0

Im trying to write a scritable plugin and I am using mozilla's example below as my guide, as well as looking at firebreath to see how it wraps the code. I am getting stuck on the return value to javascript.

Mozilla scriptable example

When javascript calls my function the Allocate,HasProperty,HasMethod,Invoke all get called. I return back the result in Invoke and the javascript variable is undefined or crashes the browser when modifying the result.

    STRINGZ_TO_NPVARIANT(_strdup("Hello World"), *result);
hapyfishrmn
  • 177
  • 2
  • 21

1 Answers1

1

STRINGZ_TO_NPVARIANT is actually a bit dangerous; when you put a string into an NPVariant object you give ownership of that memory to the browser. However, if you didn't allocate that memory with NPN_MemAlloc things may explode when it tries to release that memory (possibly the source of your crash).

Look at what STRINGZ_TO_NPVARIANT is actually doing and don't use it 'til you understand how it works; until then, you may try performing the steps by hand so you have a better understanding. Allocate memory using NPN_MemAlloc and then strcpy your string to it. I bet this fixes your problem; after you've got it figured out build your own inline functions or whatnot to clean up the code again.

taxilian
  • 14,229
  • 4
  • 34
  • 73
  • Thanks for the response, I had already looked into the macro and I dont see anything wrong with how the macro is done problematically. But after reading here: [link](http://colonelpanic.net/2009/12/memory-management-in-npapi/)[/link]and with your comments I didnt use NPN_MemAlloc and copy the data into a temporary string. `(temp = NPN_MemAlloc(strlen("Hello World")); memcpy(temp, "Hello World", strlen("Hello World")); STRINGZ_TO_NPVARIANT(temp, *result);)` – hapyfishrmn Jan 04 '12 at 14:26