Strange problem with new in a DLL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Al Newton

    Strange problem with new in a DLL

    I created a DLL that reads a pointer to a vector created in a
    console-mode test app as:
    vector<LineItem *>* pLineItems = new vector<LineItem *>;
    Everything seems to work fine (except for memory leaks) if I don't do
    a delete in the test app. But if I do, I get an exception on the test
    app's return 0 statement.

    BoundsChecker tells me that I am attempting to free memory in the DLL
    that was allocated in the test routine. I don't understand this
    because the DLL does not contain even a single delete.

    Can someone shed some light on this behavior? Thanks ... Al
  • Victor Bazarov

    #2
    Re: Strange problem with new in a DLL

    "Al Newton" <alnewton1@hotm ail.com> wrote...[color=blue]
    > I created a DLL that reads a pointer to a vector created in a
    > console-mode test app as:
    > vector<LineItem *>* pLineItems = new vector<LineItem *>;
    > Everything seems to work fine (except for memory leaks) if I don't do
    > a delete in the test app. But if I do, I get an exception on the test
    > app's return 0 statement.
    >
    > BoundsChecker tells me that I am attempting to free memory in the DLL
    > that was allocated in the test routine. I don't understand this
    > because the DLL does not contain even a single delete.
    >
    > Can someone shed some light on this behavior? Thanks ... Al[/color]

    You probably need to ask in a newsgroup where DLLs are on topic.
    As far as C++ is concerned, if you allocate memory in a program,
    you should be able to free it in the same program.

    <offtopic> You might want to make sure that both the App and the
    DLL use the run-time library as a DLL too</offtopic>

    Victor


    Comment

    • Gianni Mariani

      #3
      Re: Strange problem with new in a DLL

      Al Newton wrote:[color=blue]
      > I created a DLL that reads a pointer to a vector created in a
      > console-mode test app as:
      > vector<LineItem *>* pLineItems = new vector<LineItem *>;
      > Everything seems to work fine (except for memory leaks) if I don't do
      > a delete in the test app. But if I do, I get an exception on the test
      > app's return 0 statement.
      >
      > BoundsChecker tells me that I am attempting to free memory in the DLL
      > that was allocated in the test routine. I don't understand this
      > because the DLL does not contain even a single delete.
      >
      > Can someone shed some light on this behavior? Thanks ... Al[/color]

      I don't know how bounds checker works but it's likely that it is not
      reporting correctly.

      The problem is that the new() operator is inlined and bounds checker
      probably only knows about malloc and free. Hence what bounds checker is
      reporting is totally bogus.

      How to fix this in win32 apps is still a mystery to me, however with gcc
      there are ways to make it so that checkers can work correctly.


      Comment

      Working...