-
Notifications
You must be signed in to change notification settings - Fork 709
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
Crash instancing BLEServer #33
Comments
Can you pastebin the complete failing sketch? |
I studied the code for a while and then a big smile came over my face. If we look in your code ... it reads (loosely as follows)
The way to read this is "We enter the function called setup and create an instance of the BLEServer object. We then do some other stuff and then we complete the setup function. Now, in C or C++ when we end a function, any local variables created in that function are deleted. In this code fragment, there is a local variable called We need to change the nature of this Github issue to: When we have created a BLEServer instance, when we delete that BLEServer, make sure that any and all initialization that may have been done by it is un-done. This will include (but not be limited to) stopping the BLE server that this BLEServer models and stopping any advertising of this server and preventing any clients from attaching to this server ... simply put, if the object that represents the server goes out of scope or is otherwise deleted, we must also delete the BLE server within ESP-IDF. |
Oh I didn't realize that, nice find! One more thing: have to call ble.initServer("MyESP32"); before it, prevents of create BLEServer as a global thing What do you think about initServer inside BLEServer creation if still not exist yet? Or there is some situation which people can initServer but doesn't create BLEServer? |
@copercini And that's about as far as I have taken it/thought it through. Since a BLE Device is a singleton, there should be nothing to prevent us from performing "initServer" when a BLEServer() is created ... but the reason I have separated it is that the name of the BLE device is a singleton and needs to be set before we create the BLEServer(). We don't want to pass the BLE device name through to BLEServer as we can have multiple servers but only one BLE device name. This will require us both to think about this a bit more. |
@nkolban, If I may interject, I think the problem may be that the code in this method currently is a mixture of device initialization and server initialization? Forgive me if im speaking out of turn, but I had thought something similar to copercini myself. Regards, |
@shcreasey How does the following "feel":
Lets think of this notion ... and see if we can see any pros or cons with (1) and (2) here as compared to what we already have or what might else be possible. And again, all comments welcomed. If you say something that makes no sense (and I'm not expecting that), then that is an opportunity for the rest of us to explain better. |
@nkolban You could (as microsoft do in .net sometimes) have your singleton BLE_Device then have .createServer() and .createClient() methods which then return those objects? Thereby circumventing the need to pass references back to the BLE_Device after server or client instantiation. As for your 'new territory' comments: I was under the impression that: a 'peripheral' could only have a single server with multiple services, and connect with only one central device. And a 'central' could connect to one or multiple different peripherals. Although having said that, I do seem to recall that either the peripheral or the central can be client or server (or both?) so if, as I said above, the central is connected to multiple peripherals AND the central is running a server, perhaps it would need to expose a server for each connected peripheral? (or perhaps not?) i'm not sure about that. I think maybe a bit of spec. digging is going to be required for that. For the time being though, I'd be tempted to limit it to a single server per device, unless someone requires otherwise. I think advertising should be a function of a (peripheral) 'device' rather than a 'server' and perhaps we could have an advertisingData or 'advertisement' object that we could populate and pass in to the device, before calling advertise. or even pass it into the advertise method.? Does any of that make sense? Regards, |
Howdy Steve, |
Okly dokly ... 1st part done. The BLE class has been renamed to BLEDevice() and separate intialization functions called BLEDevice::initClient() and BLEDevice::initServer() have been consolidated into BLEDevice::init(std::string deviceName). |
Steve,
The implementation of createServer could be as simple as:
Do you have any thoughts on how to prevent a user from constructing an instance of BLEServer directly? |
@nkolban |
Thank you sir. New issue created to track and resolve. |
I think it was solved, thx @nkolban |
In BLE server for Arduino, creating a BLEServer like this
BLEServer *pServer = new BLEServer();
everything works fine, but instancingBLEServer pServer;
it compiles, create the server but crash in the client connection:The text was updated successfully, but these errors were encountered: