Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: Should I release the reader in the destructor? (Read 2572 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Should I release the reader in the destructor?

Foobar2000 0.8.3.

I have a special input plugin that manufactures its own reader_limited from the input reader. This works as expected. Once the class is destructed, I do what the SDK does and call reader_release_safe() on the reader. However, when I call this function, Foobar2000 then proceeds to crash. The crash bears all the hallmarks of performing a double delete.

Code: [Select]
reader_limited *rLimited;
virtual ~input_wad()
{
 if(this->rLimited != NULL) this->rLimited->reader_release_safe(); /* Commenting this out makes everything fine. */
 delete this->rLimited; /* Crashes here */
}


If I comment out the line that says reader_release_safe, everything works as normal. That indicates that I shouldn't be calling reader_release_safe(). However, I'm concerned that by not calling release, I'm leaking resources. Is this worry unfounded?

What's the proper procedure for creating and using a reader?

Should I release the reader in the destructor?

Reply #1
Quote
Foobar2000 0.8.3.

I have a special input plugin that manufactures its own reader_limited from the input reader. This works as expected. Once the class is destructed, I do what the SDK does and call reader_release_safe() on the reader. However, when I call this function, Foobar2000 then proceeds to crash. The crash bears all the hallmarks of performing a double delete.

Code: [Select]
reader_limited *rLimited;
virtual ~input_wad()
{
 if(this->rLimited != NULL) this->rLimited->reader_release_safe(); /* Commenting this out makes everything fine. */
 delete this->rLimited; /* Crashes here */
}


If I comment out the line that says reader_release_safe, everything works as normal. That indicates that I shouldn't be calling reader_release_safe(). However, I'm concerned that by not calling release, I'm leaking resources. Is this worry unfounded?

What's the proper procedure for creating and using a reader?
[a href="index.php?act=findpost&pid=322840"][{POST_SNAPBACK}][/a]


I think it should be

Code: [Select]
reader_limited *rLimited;
virtual ~input_wad()
{
 if (this->rLimited != NULL)
    this->rLimited->reader_release_safe();
}


Since the reader has a reference counter, and it delete itself when the reference counter reaches zero.

 

Should I release the reader in the destructor?

Reply #2
Now this is interesting... I've been initializing the reader with operator new. However, since my DLL uses a different heap to Foobar2000 (as explained here), mixing of new/deletes between different modules will cause crashes. Does this mean my use of new is wrong?

My next question is then: how do I correctly create a reader?

Code: [Select]
/* My code is currently something like this: */
this->rLimited = new service_impl_p3_t<reader_limited, reader *, __int64, __int64>(rIn, start, end);