CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Threaded View

  1. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: A question about deleting a native pointer

    Quote Originally Posted by AKRichard View Post
    The native class works fine if deleted from anywhere else, this is the only function in the class that tries to delete a seperate instance of itself. The reason I did it this way was because the pointer which gets passed into the function is a return value from a function in the managed class, so there is no physical pointer that I can delete outside of it.
    I have no idea what "interop" is, or what it has to do with properly handling the C++ free-store operators, new and delete.

    There is nothing new in your issue -- you have corrupted the heap or the pointer value is invalid.

    First, new returns a value. Is it the same value you're deleting? Not pointer, but value.

    All you're showing in your code is that you're using the same variable. That doesn't really mean anything -- you should be looking at the value that is being returned by new, and whether it is the same value you're using in the delete.

    If it is the same value, then you either corrupted the memory, the pointer was already deleted and you deleted twice, or you allocated in one heap and deallocated using another heap. In any event, usage of smart pointers removes this unnecessary back-and-forth you're doing now, since a smart pointer always cleans itself up automatically when there are no more references to it and goes out of scope.
    I tried looking through the docs, but I am not understanding why it is erroring out, am I not allowed to delete a seperate instance of the class from within the class?
    You can look through all the docs you want, but you won't find the issue until you debug your program. Again, your problem is not unique, it isn't a fault of the system so that it is documented somewhere, or anything of the sort. It is a programming error caused by mismanaging pointers and the heap.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 22nd, 2013 at 07:41 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured