Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

operations fail if arguments go out of scope #123

Open
marcusb opened this issue Dec 26, 2024 · 3 comments
Open

operations fail if arguments go out of scope #123

marcusb opened this issue Dec 26, 2024 · 3 comments

Comments

@marcusb
Copy link

marcusb commented Dec 26, 2024

The MQTTAgent_Publish() method requires that the pPublishInfo argument (carrying the topic name) remains in scope after the call returns. This is not documented in the API doc, which only says:

The context passed to the callback through pCmdContext member of pCommandInfo parameter MUST remain in scope at least until the callback has been executed by the agent task.

I am allocating the parameters to the Publish call on the stack, and do not want to wait for an ack, just send a fire-and-forget request. I can do this by setting the callback pointer to NULL. But that still doesn't work, because the library retains the pPublishInfo pointer. But that memory is deallocated when my method returns, leading to undefined behavior.

Perhaps the library should make a copy of the supplied MQTTPublishInfo_t instead.

@jasonpcarroll
Copy link
Member

Hi @marcusb,
I will take a deeper look at this. Thank you for bringing it to attention!
Best,
Jason Carroll

@rawalexe
Copy link
Member

rawalexe commented Jan 7, 2025

Hello @marcusb,
Increasing the priority of the mqtt task can help out with solving this issue. We will also update the doc to reflect this requirement. Please let us know if you have more questions

@AniruddhaKanhere
Copy link
Member

AniruddhaKanhere commented Jan 7, 2025

Hello @marcusb,

Firstly, apologies for the delay on our part.

Secondly, I see your point. Thank you for reporting this issue.
We will update the documentation. But the behavior of the library cannot be easily changed.

For your application to work, as @rawalexe mentioned above, can you raise the priority of the coreMQTT-Agent? That will cause the agent to wake up as soon as your task adds any data to the agent queue and process it. This will cause your task to be preempted and sleep while the Agent uses the passed arguments to send publish packet. When it is done, your task will wake up and it will be okay to free the memory used by the arguments.

Let us know if this works.

Thanks,
Aniruddha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants