liboai: Memory access violation on "A connection error occurred" exception
Describe the bug
This simple example leads to a crash with some memory access violation in xmemory.h ( I am on windows)
#include "liboai.h"
using namespace liboai;
int main() {
OpenAI oai;
if (oai.auth.SetKeyEnv("OPENAI_API_KEY")) {
try {
Response response = oai.Image->create(
"a siamese cat!"
);
}
catch (std::exception& e) {
std::cout << e.what() << std::endl;
}
...
}
}
The lib crashes somewhere after
throw liboai::exception::OpenAIException(
"A connection error occurred",
liboai::exception::EType::E_CONNECTIONERROR,
"liboai::Response::CheckResponse()"
);
is called. I can’t see the exact error location on the library side in call stack, but eventually it leads to a crash in xmemory.h.
MSVS spits this:
Exception thrown at 0x00007FF8D13ECD29 in TestApp.exe: Microsoft C++ exception: liboai::exception::OpenAIException at memory location 0x00000046F09DBFC0. Exception thrown at 0x00007FF7DD1C82C5 in TestApp.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
In my case I have no credits left in OpenAI API. Have you ever tested that case?
To Reproduce
Run the code I provided below. But maybe it is also a special case related to the fact the account has no credits left.
Code snippets
int main()
{
OpenAI oai;
if (oai.auth.SetKey("my key goes here"))
{
try {
/* Response res = oai.Image->create(
"A snake in the grass!",
1,
"256x256"
);*/
Response response = oai.Image->create(
"a siamese cat!"
);
std::cout << response["data"][0]["url"] << std::endl;
}
catch (std::exception& e)
{
std::cout << e.what() << std::endl;
std::quick_exit(EXIT_FAILURE);
}
}
std::cout << "Hello World!\n";
return 0;
}
OS
Windows
Library version
3.0.0
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 24 (23 by maintainers)
Good idea.
I’ll push a change to
authorization.hand.cppto allow users to dynamically set their timeout values in seconds.All of these improvements should be released to main once this PR and its related issue are merged and closed.
Making PR + moved that header into includes. Pushed another small update.Added a check for nullptr to make sure we don’t put null url string into Response.
Yeah, that’s a pretty obvious culprit for an error–passing the address of a C++ type to a C API method.
If this is the cause of the crashes, I would assume an easy fix would just be passing a
char*and then doing a deep copy to astringafterwards.I’ll look into it, but feel free to make a PR if you have done so already.
Okay, I can confirm I have isolated this issue to Debug mode builds.