Yes, malloc the space, and memcpy the contents to that space.

On Tue, Oct 20, 2015 at 8:20 PM, Pau Ballart <[email protected]> wrote:

> I did some tries with no success.
> If I have this declaration:
> *int p3stuff(char *imgUrl, unsigned char *wrappedkey);*
> How can I make sure the contents of imgUrl will persist even out of the
> scope?
> Because it's a pointer so maybe I need to malloc first the space and then
> memcpy the contents?
> Suppose I have a global var like this *char *imageUrl;* I'd be very
> thankful if you can provide me an example.
>
> 2015-10-20 15:57 GMT-07:00 Alon Zakai <[email protected]>:
>
>> Yes, if you want something to live beyond the current function call, you
>> should use malloc(). That will only be reused after you call free() on it.
>>
>> On Tue, Oct 20, 2015 at 3:51 PM, Pau Ballart <[email protected]> wrote:
>>
>>> Updated to 1.35 but same problem...
>>> Maybe the question to formulate would be: How can I save in a
>>> global/static variable some information so that I can acces it later in the
>>> callback function and make sure it's not overwritten?
>>> What do you think about using malloc() somehow? Would it work?
>>>
>>> 2015-10-20 14:18 GMT-07:00 Alon Zakai <[email protected]>:
>>>
>>>> Might be worth trying current master (or incoming) then, which is now
>>>> 1.35.0.
>>>>
>>>> On Tue, Oct 20, 2015 at 2:16 PM, Pau Ballart <[email protected]>
>>>> wrote:
>>>>
>>>>> $ emcc -v
>>>>> emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld)
>>>>> 1.34.8
>>>>> clang version 3.7.0
>>>>> Target: x86_64-apple-darwin15.0.0
>>>>> Thread model: posix
>>>>> INFO     root: (Emscripten: Running sanity checks)
>>>>>
>>>>> 2015-10-20 14:05 GMT-07:00 Alon Zakai <[email protected]>:
>>>>>
>>>>>> One possible issue is that if those values are on the stack, and the
>>>>>> scope exits, then that stack memory might be reused. For example this is
>>>>>> unsafe:
>>>>>>
>>>>>> {
>>>>>>   char buffer[100];
>>>>>>   // write into buffer
>>>>>>   emscripten_async_wget(..., (void*)buffer);
>>>>>> }
>>>>>>
>>>>>> The buffer will be released before the async callback occurs, and
>>>>>> other data might be written into it before the callback reads from it.
>>>>>>
>>>>>> Which version of emscripten are you using, btw? I remember a while
>>>>>> back we had a bug around this.
>>>>>>
>>>>>> On Tue, Oct 20, 2015 at 1:36 PM, Pau Ballart <[email protected]>
>>>>>> wrote:
>>>>>>
>>>>>>> Hello,
>>>>>>>
>>>>>>> After some research and some refactor I did the following:
>>>>>>>
>>>>>>>    - In my p3stuff function I
>>>>>>>    call emscripten_set_main_loop(wait_wgets, 0, 0); so the application 
>>>>>>> is not
>>>>>>>    terminated.
>>>>>>>    - In wait_wgets I call the emscripten_async_wget(imageUrl, path,
>>>>>>>    onLoaded, onError); only the first time the method is called.
>>>>>>>    - In the onLoaded callback I call emscripten_cancel_main_loop();
>>>>>>>    and then I call my c code that perform the jpeg stuff.
>>>>>>>
>>>>>>> Problem is:
>>>>>>> the p3stuff function has 2 arguments: int p3stuff(char *imgUrl,
>>>>>>> unsigned char *wrappedkey)
>>>>>>> When it's called i have two global variables unsigned char
>>>>>>> *wrappedKey; and char *imageUrl; which are assigned with the argument 
>>>>>>> like
>>>>>>> that:
>>>>>>>
>>>>>>> wrappedKey = wrappedkey;
>>>>>>> imageUrl = imgUrl;
>>>>>>>
>>>>>>> I do that in order to access the image url from the wait_wgets() or
>>>>>>> the wrapped key from the onLoaded callback but I think there is some 
>>>>>>> memory
>>>>>>> management problems cause if I print the url just before calling
>>>>>>> emscripten_async_wget the imageUrl variable content is completely 
>>>>>>> rubbish
>>>>>>> but id I don't print anything it works.
>>>>>>>
>>>>>>> So how can I use the global variables in C in order to not get
>>>>>>> overwritten? Maybe using malloc, or memcopy?
>>>>>>>
>>>>>>> Thanks,
>>>>>>>
>>>>>>> Pau
>>>>>>>
>>>>>>> 2015-10-19 10:53 GMT-07:00 Pau Ballart <[email protected]>:
>>>>>>>
>>>>>>>> Okay I will try the async version and let you know.
>>>>>>>>
>>>>>>>> Thanks
>>>>>>>>
>>>>>>>> 2015-10-17 19:37 GMT-07:00 Alon Zakai <[email protected]>:
>>>>>>>>
>>>>>>>>> The only tricky thing with emscripten_wget is that as mentioned in
>>>>>>>>> the docs,
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> http://kripken.github.io/emscripten-site/docs/api_reference/emscripten.h.html#c.emscripten_wget
>>>>>>>>>
>>>>>>>>> it requires the ASYNCIFY option. It's easier in general to use the
>>>>>>>>> async version, emscripten_async_wget, which does not require ASYNCIFY.
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> On Sat, Oct 17, 2015 at 10:47 AM, Pau Ballart <[email protected]>
>>>>>>>>> wrote:
>>>>>>>>>
>>>>>>>>>> I think the php code is correct so I'm just trying to download an
>>>>>>>>>> image and process it's data but I think I'm missing something about 
>>>>>>>>>> the
>>>>>>>>>> lifecycle or wget function because I get unexpected exit errors.
>>>>>>>>>>
>>>>>>>>>> So I'd like to know if the  emscripten_wget(imgUrl, path); is
>>>>>>>>>> correct because I have the weirdest behaviour ever that depending on 
>>>>>>>>>> the
>>>>>>>>>> p3stuff function param "wrappedkey" the download succeeds or not.
>>>>>>>>>>
>>>>>>>>>> 2015-10-15 17:07 GMT-07:00 Alon Zakai <[email protected]>:
>>>>>>>>>>
>>>>>>>>>>> I see you have
>>>>>>>>>>>
>>>>>>>>>>> P3Func = Module.cwrap('p3stuff', 'number', ['string', 'string']);
>>>>>>>>>>>
>>>>>>>>>>> which looks good.  You can then call it with something like
>>>>>>>>>>>
>>>>>>>>>>> result = P3Func('first', 'second');
>>>>>>>>>>>
>>>>>>>>>>> which I see you are doing. What part of that are you trying to
>>>>>>>>>>> improve?
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> On Thu, Oct 15, 2015 at 12:59 PM, Pau Ballart <
>>>>>>>>>>> [email protected]> wrote:
>>>>>>>>>>>
>>>>>>>>>>>> Okay so the php is just calling a function I declared. Because
>>>>>>>>>>>> I need to pass the image url as an argument and a key, I don't 
>>>>>>>>>>>> know how to
>>>>>>>>>>>> do it using the main function so I changed the main function for 
>>>>>>>>>>>> this one
>>>>>>>>>>>> with the two arguments and then called this function from my php 
>>>>>>>>>>>> code.
>>>>>>>>>>>> Is there a better way to call emscripten passing parameters?
>>>>>>>>>>>>
>>>>>>>>>>>> Thanks.
>>>>>>>>>>>>
>>>>>>>>>>>> 2015-10-15 12:55 GMT-07:00 Alon Zakai <[email protected]>:
>>>>>>>>>>>>
>>>>>>>>>>>>> It would be good to focus the question more, it's hard to tell
>>>>>>>>>>>>> what the core issue is. Try to reduce the problem to just one 
>>>>>>>>>>>>> concrete
>>>>>>>>>>>>> thing, like just loading a file, and doing it without that custom 
>>>>>>>>>>>>> PHP
>>>>>>>>>>>>> script, for example.
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>> On Thu, Oct 15, 2015 at 12:27 PM, Pau Ballart <
>>>>>>>>>>>>> [email protected]> wrote:
>>>>>>>>>>>>>
>>>>>>>>>>>>>> Hello developers,
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> I'm starting a project and I'm new at Javascript and
>>>>>>>>>>>>>> emscripten. I'm trying to download some image data from a server 
>>>>>>>>>>>>>> and then
>>>>>>>>>>>>>> process it using some c functions I already have working. I 
>>>>>>>>>>>>>> asked this
>>>>>>>>>>>>>> question in Stackoverflow so I share you the link and maybe one 
>>>>>>>>>>>>>> of you can
>>>>>>>>>>>>>> help me.
>>>>>>>>>>>>>>
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> http://stackoverflow.com/questions/33087205/emscripten-code-for-image-data-download-using-emscripten-wget-or-emscripten-asyn
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> Thank you,
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> Pau B
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> --
>>>>>>>>>>>>>> You received this message because you are subscribed to the
>>>>>>>>>>>>>> Google Groups "emscripten-discuss" group.
>>>>>>>>>>>>>> To unsubscribe from this group and stop receiving emails from
>>>>>>>>>>>>>> it, send an email to
>>>>>>>>>>>>>> [email protected].
>>>>>>>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>> --
>>>>>>>>>>>>> You received this message because you are subscribed to a
>>>>>>>>>>>>> topic in the Google Groups "emscripten-discuss" group.
>>>>>>>>>>>>> To unsubscribe from this topic, visit
>>>>>>>>>>>>> https://groups.google.com/d/topic/emscripten-discuss/BEb-1mjDvZI/unsubscribe
>>>>>>>>>>>>> .
>>>>>>>>>>>>> To unsubscribe from this group and all its topics, send an
>>>>>>>>>>>>> email to [email protected].
>>>>>>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>> --
>>>>>>>>>>>> Pau Ballart
>>>>>>>>>>>>
>>>>>>>>>>>> --
>>>>>>>>>>>> You received this message because you are subscribed to the
>>>>>>>>>>>> Google Groups "emscripten-discuss" group.
>>>>>>>>>>>> To unsubscribe from this group and stop receiving emails from
>>>>>>>>>>>> it, send an email to
>>>>>>>>>>>> [email protected].
>>>>>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> --
>>>>>>>>>>> You received this message because you are subscribed to a topic
>>>>>>>>>>> in the Google Groups "emscripten-discuss" group.
>>>>>>>>>>> To unsubscribe from this topic, visit
>>>>>>>>>>> https://groups.google.com/d/topic/emscripten-discuss/BEb-1mjDvZI/unsubscribe
>>>>>>>>>>> .
>>>>>>>>>>> To unsubscribe from this group and all its topics, send an email
>>>>>>>>>>> to [email protected].
>>>>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> --
>>>>>>>>>> Pau Ballart
>>>>>>>>>>
>>>>>>>>>> --
>>>>>>>>>> You received this message because you are subscribed to the
>>>>>>>>>> Google Groups "emscripten-discuss" group.
>>>>>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>>>>>> send an email to [email protected].
>>>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>> --
>>>>>>>>> You received this message because you are subscribed to a topic in
>>>>>>>>> the Google Groups "emscripten-discuss" group.
>>>>>>>>> To unsubscribe from this topic, visit
>>>>>>>>> https://groups.google.com/d/topic/emscripten-discuss/BEb-1mjDvZI/unsubscribe
>>>>>>>>> .
>>>>>>>>> To unsubscribe from this group and all its topics, send an email
>>>>>>>>> to [email protected].
>>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> --
>>>>>>>> Pau Ballart
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> Pau Ballart
>>>>>>>
>>>>>>> --
>>>>>>> You received this message because you are subscribed to the Google
>>>>>>> Groups "emscripten-discuss" group.
>>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>>> send an email to [email protected].
>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>
>>>>>>
>>>>>> --
>>>>>> You received this message because you are subscribed to a topic in
>>>>>> the Google Groups "emscripten-discuss" group.
>>>>>> To unsubscribe from this topic, visit
>>>>>> https://groups.google.com/d/topic/emscripten-discuss/BEb-1mjDvZI/unsubscribe
>>>>>> .
>>>>>> To unsubscribe from this group and all its topics, send an email to
>>>>>> [email protected].
>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Pau Ballart
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "emscripten-discuss" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to [email protected].
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>
>>>> --
>>>> You received this message because you are subscribed to a topic in the
>>>> Google Groups "emscripten-discuss" group.
>>>> To unsubscribe from this topic, visit
>>>> https://groups.google.com/d/topic/emscripten-discuss/BEb-1mjDvZI/unsubscribe
>>>> .
>>>> To unsubscribe from this group and all its topics, send an email to
>>>> [email protected].
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>>
>>>
>>> --
>>> Pau Ballart
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "emscripten-discuss" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "emscripten-discuss" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/emscripten-discuss/BEb-1mjDvZI/unsubscribe
>> .
>> To unsubscribe from this group and all its topics, send an email to
>> [email protected].
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
> Pau Ballart
>
> --
> You received this message because you are subscribed to the Google Groups
> "emscripten-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to