Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

custom string class: memory leak

Featured Replies

Posted

hi guys,

i was trying to implement a custom string class for practicing purposes,

but i hit a problem regarding memory leaks...

to be more precise, the problem is the substring-method:

substr(int start, int length);

to be used like this:

mystring a = "deepzero";
mystring b = a.substr(4,4);//b == "zero"

or

MessageBox(0,a.subtr(4,4),"test",0);

the problem is: when i create the substring, where do i store it?

When i allocate space on the heap, it wont be freed anymore...

So i figured i`d have .substr() return a mystring object (containing the substring):

mystring mystring::substr(int start,int len)
{
//ptrtostr == pointer to the actual char-array which stores the text
//mystring-constructor takes a pointer and assigns the number of bytes in len to the new text string
return mystring((this->ptrtostr + start),len);//
}

That way, the constructor would allocate the memory for the substring and the destructor would free it again.

Or so I thought...

i was using this to test it:

int main()
{
mystring test = "deepzero";
mystring test2 = test.substr(1,3);//==eep
}

and it works fine, but unfortunately the mystring-object i return from the substr() function

never calls its destructor... :(

why not? :(

how would you solve the substring-problem ?

br,

dp0 :)

Edited by deepzero

unless your compiler is broken (which I doubt), it should call its destructor.

maybe it's because you don't have a copy constructor?

  • Author

forgot to mention that i am using MSVC++ 2008. :)

what do you mean by "copy constructor"?

edit:

*no, wrong*

Edited by deepzero

"no, wrong" as in: you do have a copy constructor?

how do you know it doesn't call the destructor?

i mean, if you have a copy constructor, this should work fine without memory leaks. if you don't, the default one performs shallow copies on the heap pointers, making them all share the same memory, and only that one chunk gets freed.

otherwise, i dont see a problem with your code :s

  • Author

hi killboy,

after cleaning the code a little bit & sleeping over it, it works now.

No idea what exactly went wrong...but i love it when my problems dissolve into thin air over night... :sweat:

thanks for your time. :)

谢谢你的吖!这东西不错

Create an account or sign in to comment

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.