Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8218291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:44:36+00:00 2026-06-07T12:44:36+00:00

I am having a problem understanding why this part of my code is causing

  • 0

I am having a problem understanding why this part of my code is causing a memory leak:

for($i=0; $i<count($values); $i++){
        $values[$i] = addslashes($values[$i]);
}

To put the code in context, i have a previously built array called values, which has all the values to be inserted into a database. all the fields are strings so i need to escape all of them and for this application addslashes or mysql_real_escape_string are a good choice imo.

Now the strange thing is that as soon as i added the part shown above, i get a message like this:

PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 24 bytes)

I understood that this generated a memory leak but i don’t know why.

Digging in, i commented out the only line in the for loop, leaving the for statement just for curiosity, and the leak is gone. Any ideas what could this possibly mean?

PS: The strings are all UTF8 encoded, could that be a problem?

EDIT:

The array contains something like this :

Array ( 
    [dossier] => 002A 
    [permis] => 
    [adresse] => 18, rue Bellevue
    [ville] => Ste-Anne-des-Lacs (Québec)
    [province] =>
    [code_postal] => J0R 1B0
    [numero_centrale] => N/A
    [routes] => De la Gare, droite chemin Avila jusqu'au bout et droite chemin Ste-Anne-des-lacs sur 1,8 km et droite sur Bellevue.
) 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:44:38+00:00Added an answer on June 7, 2026 at 12:44 pm

    Your array has string keys, but you are checking/assigning numeric keys. As @nickb notes, each time you add a numeric key, count($values) increases by one, so you have an infinite loop. Hence the memory exhaustion.

    Check with a debugger, or better yet, switch to a foreach loop or one of the array_* functions (array_walk(), array_map(), etc. depending on what you are trying to do).

    @Jeremy correctly points out that you should not be using addslashes() to escape your strings. Look at mysqli_real_escape_string() (as he suggests), or better yet, consider using PDO if you can.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to JavaScript, and am having a problem understanding this code: function addProperty(o)
I have the following problem, and am having trouble understanding part of the equation:
I'm having trouble understanding some basic memory management principles in C++. This code is
I am having problem understanding this part of Testing Class Equality which is defined
I am having a problem regarding a jquery dropdownmenu. I have put a slidedown
I'm having trouble understanding some web part communication code, and how to make it
i am having problem in understanding the behaviour of this programme below is simple
I'm having a problem understanding Java generics and I've simplified to this example class
I'm having a little problem understanding variance of methods when overloading. While this perfectly
Im having problems understanding the app logic to this password reset code i found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.