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

Memory leak in Dispatch.cpp #36

Closed
EJP286CRSKW opened this issue Aug 24, 2023 · 5 comments
Closed

Memory leak in Dispatch.cpp #36

EJP286CRSKW opened this issue Aug 24, 2023 · 5 comments

Comments

@EJP286CRSKW
Copy link

There is a memory leak around the call to name2ID(), in the case where it fails. Code reads:

  if (name != NULL) 
  {
    const char *nm = env->GetStringUTFChars(name, NULL);
    HRESULT hr;
    if (FAILED(hr = name2ID(pIDispatch, nm, (long *)&dispID, lcid))) {
      char buf[1024];
      sprintf_s(buf, 1024, "Can't map name to dispid: %s", nm);
      ThrowComFail(env, buf, -1);
      return NULL;
    }
    env->ReleaseStringUTFChars(name, nm);
  }

It should read:

  if (name != NULL) 
  {
    const char *nm = env->GetStringUTFChars(name, NULL);
    HRESULT hr = name2ID(pIDispatch, nm, (long *)&dispID, lcid));
    env->ReleaseStringUTFChars(name, nm);
    if (FAILED(hr)) {
      char buf[1024];
      sprintf_s(buf, 1024, "Can't map name to dispid: %s", nm);
      ThrowComFail(env, buf, -1);
      return NULL;
    }
  }
@freemansoft
Copy link
Owner

You are saying that the leak is because the release string is skipped if it fails because the throw() exits the function before ReleaseString is called?

Ugh. I'll have to look at putting my dev environment back together.

@freemansoft
Copy link
Owner

How did you find this?

@EJP286CRSKW
Copy link
Author

EJP286CRSKW commented Sep 4, 2023

The ReleaseStringUTFChars() is skipped because of the premature return NULL; in the failure block.

I found it by reading the code.

@freemansoft
Copy link
Owner

I think

    HRESULT hr = name2ID(pIDispatch, nm, (long *)&dispID, lcid));

Should be

    HRESULT hr = name2ID(pIDispatch, nm, (long *)&dispID, lcid);

5853381

@EJP286CRSKW
Copy link
Author

Correct, well spotted.

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

2 participants