• Resolved ceophoetography

    (@ceophoetography)


    If a media attachment in the media library is unattached and the Media Library Assistant plugin is active, something about the call to the ***_image_link() function in the image or attachment template produces an error:

    Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 3 bytes) in ….[host]/wordpress/wp-includes/wp-db.php on line 1780

    That kicks the WP loop out of the code early, lopping off whatever code should have been processed afterward. I’ve read that it may be a memory leak in the code or an infinite loop. Over a year ago (Jan. 15, 2015), when I addressed the problem with you, David, you concluded:

    WordPress just stopped processing everything when it hit the PHP code for the nav-links section of your page. Without access to the server and the PHP template for your attachment page it is hard to be more specific, but it is a server-side code problem, not an HTML formatting problem. You said that W3 Total Cache is not running; are there other plugins active at this point?

    If you can’t solve this problem, I could look at your template file. Don’t post the template here. You can give me your contact information and I will respond with an address where you can e-mail the template file to me.

    You later wrote, “It has been a month since my last post in this topic. I assume you have found a solution to your truncated HTML output issue,” before marking the topic “resolved.” However, I had never found a solution to the issue. I either had partially satisfactory workarounds or I just accepted the issue.

    The problem is not in the template, because when the MLA plugin is deactivated, all attachment pages display properly with no error, and files that are attached to a post also display properly even with MLA active. It doesn’t matter which theme is being used: the loop breaks upon reaching the image_link() function. But I would like to have gallery pages of images that aren’t attached and need for the WP loop to complete for those images’ attachment pages. Moreover, when MLA is active and I configure the (Photo) Gallery plugin (by WebDorado [https://www.ads-software.com/plugins/photo-gallery/]) to “Import from Media Library” and then try to add images, the pop-up frame indicates an Internal Server error that does not happen when MLA is deactivated.

    Is there any way you can investigate the portion of the code that breaks the Loop when MLA queries the database? The line in the WordPress DB class (wp-db.php) where the error occurs is in the public function “query($query),” which begins on Line 1677.

    When MLA is active, attachment tag classes are added to the article html tag, but not when it is not active. But the attachment tag classes are no problem when the attachment has a parent.

    I’ve done some digging for when WP calls anything with “_image_link(” and found four relevant files: An example image.php in the MLA directory, my theme’s image.php, class-simplepie.php, and media.php. The latter two are in the wp-includes directory, which I think makes them core files.

    I guess when the Loop gets to the nav-links in the attachment template file, it goes to the previous_image_link() function in wp-includes/media.php (Line 2597), which then calls the adjacent_image_link() function (Line 2629).

    I can’t replicate the problem on my localhost. I downgraded my MAMP application so that I could simulate PHP 5.3, which is what is installed on my live remote server. I even tried to copy the contents of my remote htaccess file to localhost. Nothing makes any difference. On my localhost, the attachment pages render perfectly even for unattached files and the media library option in the Gallery plugin works with MLA active. It doesn’t appear to be a problem with the template; if it’s a server-side problem, I sure would like to know what it is so that I can get it fixed.

    Thank you, in advance, for your efforts.

    This unattached image attachment page demonstrates the appearance of the problem. But when the image is attached to a post, there is no problem performing the query and completing the page. If I deactivate MLA, even the former attachment page is complete.

    https://www.ads-software.com/plugins/media-library-assistant/

Viewing 15 replies - 1 through 15 (of 17 total)
  • Plugin Author David Lingren

    (@dglingren)

    Good to hear from you, although I regret you are still having trouble with the issue you first raised in this earlier topic:

    Active MLA plugin causes 500 error

    Thanks for all the work you’ve put into tracking the problem down and for the detailed information you’ve provided.

    It won’t surprise you to read that I am unable to reproduce your symptoms on my system. I have set up a completely new WordPress site, installed both MLA and the Photo Gallery plugin you mentioned and run tests using the WordPress Twenty Sixteen and Twenty Fourteen themes (The Twenty Sixteen theme uses new WordPress functions added in WP 4.1).

    You wrote “When MLA is active, attachment tag classes are added to the article html tag, but not when it is not active. But the attachment tag classes are no problem when the attachment has a parent.” The tag- classes are generated by Photo Gallery; they do not appear when Photo Gallery is inactive. You could try inactivating Photo Gallery and running a test, but I don’t believe this is the problem.

    You wrote “I guess when the Loop gets to the nav-links in the attachment template file, it goes to the previous_image_link() function in wp-includes/media.php (Line 2597), which then calls the adjacent_image_link() function (Line 2629).” That looks right and it may be significant.

    The adjacent_image_link() function queries the database to find all of the images with a common parent. It then does two additional queries to prime the object_term_cache and the postmeta_cache. For all the “attached” images, these queries will yield a small number of items and associated metadata. For the “unattached” images with a “parent” of zero, the queries will process ALL of the unattached images in the site, which can be a large number. Here’s the example from my test site:

    SELECT wpml_posts.*
    FROM wpml_posts
    WHERE 1=1
    AND wpml_posts.post_parent = 0
    AND (wpml_posts.post_mime_type LIKE 'image/%')
    AND wpml_posts.post_type = 'attachment'
    AND ((wpml_posts.post_status = 'inherit'))
    ORDER BY wpml_posts.menu_order ASC, wpml_posts.ID ASC
    
    SELECT t.*, tt.*, tr.object_id
    FROM wpml_terms AS t
    INNER JOIN wpml_term_taxonomy AS tt
    ON tt.term_id = t.term_id
    INNER JOIN wpml_term_relationships AS tr
    ON tr.term_taxonomy_id = tt.term_taxonomy_id
    WHERE tt.taxonomy IN ('attachment_category', 'attachment_tag')
    AND tr.object_id IN (4, 5, 9, 11)
    ORDER BY t.name ASC
    
    SELECT post_id, meta_key, meta_value
    FROM wpml_postmeta
    WHERE post_id IN (4,5,9,11)
    ORDER BY meta_id ASC

    As you can see, I have just 4 unattached images on my site (each assigned to just one attachment_tag term), so the memory requirements are modest. Your remote live server probably has a much larger number of images and assigned taxonomy terms. Your localhost/MAMP application may not.

    Deactivating MLA will remove support for the attachment_category and attachment_tag taxonomies, which might reduce the memory required for the above database queries significantly. At this point, that is the only contribution to the problem I can trace back to MLA. As far as I know, MLA does not directly participate in the adjacent_image_link() processing or the navigation linkage for standard WordPress Attachment/Media pages.

    Here are some next steps you can consider:

    • It would be interesting to know how large your live remote server database is, especially the number of unattached images and ther assigned term counts. The error message you posted suggests you have allocated 128MB of PHP/WordPress memory; try increasing that to something like 256MB or whatever your remote server will allow.
    • If you have time and ability, you could also try creating a dummy post/page and temporarily attaching a large number of your unattached images to it, then running some tests. That may show how many unattached images it takes to break the server.
    • You can reduce the memory allocation on your localhost/MAMP application to see if that can reproduce the problem, or add images and term assignments to make it closer to the remote live server.
    • You can use a plugin such as the (excellent) Query Monitor to see the memory requirements of different numbers of images with a common parent.

    I hope that gives you more insight into the problem and tools to identify the cause. I will leave this topic unresolved until I hear back from you. Any additional information you can provide will be helpful.

    Thread Starter ceophoetography

    (@ceophoetography)

    David, thank you for the very thorough response. I’ll have to do some experimenting based on your remarks and get back to you.

    You’re certainly correct that the number of images on my localhost test installation is very small compared to the number on my live server.

    Also, perhaps you could help in another way. What I’m trying to do is create a page or two that would display a number of my photos without a dedicated post full of text for possible visitors who want to see my portfolio or just see all of my photos with a particular attachment tag. That’s what I’ve been trying to use Photo Gallery for; is MLA able to do something like that?

    Eventually I hope to have all of my images attached to a post not too long after uploading, but I’m way behind. If I wanted to show up to, say, 2,000 images on a single page, which a user could narrow down via searching for tags, would that be possible with MLA? Would I run into the same database query problem?

    EDIT: Another question: if MLA has nothing to do with nav-links problem, then why does the same database query succeed when MLA is not active? That’s what I can’t understand. With MLA inactive, the same attachment page, querying what I suppose is the same database information succeeds. What’s different when MLA is active and when it is not?

    Thread Starter ceophoetography

    (@ceophoetography)

    Oh, never mind the question in the “Edit” portion of the previous post. I reread your reply and found the answer: the added support for attachment categories and attachment tags — which must add greatly to the database query drag.

    Is there any way to prevent the query from including those data in the nav-links while still having them available for MLA itself?

    Plugin Author David Lingren

    (@dglingren)

    Thanks for your updates; I look forward to the results of your experiments.

    In general, WordPress is moving away from the “media attached to a post/page” model. With the taxonomy support MLA provides you can build your application around standard WordPress pages/posts that have shortcodes like [mla_gallery], [mla_tag_cloud] and the new [mla_term_list] to display single images, image galleries and pagination controls.

    You asked about an application to “see all of my photos with a particular attachment tag; that is exactly the type of application [mla_gallery] is designed to support. I encourage you to read through the “MLA Gallery Examples” section of the Settings/Media Library Assistant Documentation tab. There are also earlier support topics along similar lines:

    Dynamic search and filters

    filter and using attachment meta

    How do I provide a front-end search of my media items using Custom Fields?

    Front-end filtering not working out

    As you can see, this is a lively area of MLA design and discussion. I would be happy to work with you on the specifics of your application.

    You asked “Is there any way to prevent the query from including those data in the nav-links while still having them available for MLA itself?” There’s no way to alter the queries used by the WordPress navigation link functions, but you can replace them entirely and substitute MLA’s pagination controls. Here’s a topic that gives an example:

    Link to next image in MLA category?

    Again, I can help you adapt those ideas to your application.

    Thread Starter ceophoetography

    (@ceophoetography)

    Now I’m ready to try to fix the nav links in my attachment pages. I suspect image.php is the template file I’ll need to edit. I would have to replace the two PHP functions, previous_image_link and next_image_link with mla_output, right? How would I wrap the code for the template file? And wouldn’t I need a conditional (“IF”) in case MLA isn’t active (! mla_...), in which case the “ELSE” would lead to the default nav links?

    Plugin Author David Lingren

    (@dglingren)

    Thanks for your patience in awaiting my response. I’ve got some time to look into this issue and develop something to improve your performance.

    You’re right, the image.php file and its previous_image_link and next_image_link function calls are the place to start. I will develop something that works for my own theme, but it would be helpful if you posted the source code for your image.php template or at least the navigation section of the template. That way I can give you a more specific solution.

    Thanks for your help.

    Thread Starter ceophoetography

    (@ceophoetography)

    Following is the conclusion of the Image template:

    </article><!-- #post-## -->
    
    			<nav id="image-navigation" class="navigation image-navigation">
    				<div class="nav-links">
    				<?php previous_image_link( 'thumbnail', '<div class="previous-image">' . __( 'Previous Image', 'ceophoetography-look' ) . '</div>' ); ?>
    				<?php next_image_link( 'thumbnail', '<div class="next-image">' . __( 'Next Image', 'ceophoetography-look' ) . '</div>' ); ?>
    				</div><!-- .nav-links -->
    			</nav><!-- #image-navigation -->
    
    			<?php comments_template(); ?>
    
    		<?php endwhile; // end of the loop. ?>
    
    		</main><!-- .site-main -->
    	</div><!-- .content-area -->
    
    <?php
    get_sidebar();
    get_footer();
    Plugin Author David Lingren

    (@dglingren)

    Thanks for posting the excerpt of your image.php file. I have good news and bad news.

    The good news is that you can use the [mla_gallery] pagination controls to replace the existing WordPress function calls and get much better database performance for the “unattached” case. The bad news is that I have uploaded a new MLA Development Version dated 20160706, and you must first install this new version to get the solution to work. I regret the inconvenience.

    With the new Development Version in place you can replace your “image-navigation” with this new version:

    <nav id="image-navigation" class="navigation image-navigation">
    	<div class="nav-links">
    <?php if ( class_exists( 'MLAShortcodes' ) ): ?>
    	<?php $previous_image_text = '<div class="previous-image">' . __( 'Previous Image', 'ceophoetography-look' ) . '</div>'; ?>
    	<?php echo do_shortcode('[mla_gallery post_parent="{+page_parent+}" id="{+page_ID+}" mla_output="previous_link" mla_link_text="' . $previous_image_text . '" mla_link_href="{+pagelink_url+}" update_post_meta_cache=false update_post_term_cache=false]' ); ?>
    	<?php $next_image_text = '<div class="next-image">' . __( 'Next Image', 'ceophoetography-look' ) . '</div>'; ?>
    	<?php echo do_shortcode('[mla_gallery post_parent="{+page_parent+}" id="{+page_ID+}" mla_output="next_link" mla_link_text="' . $next_image_text . '" mla_link_href="{+pagelink_url+}" update_post_meta_cache=false update_post_term_cache=false]' ); ?>
    <?php else: ?>
    	<?php previous_image_link( 'thumbnail', '<div class="previous-image">' . __( 'Previous Image', 'ceophoetography-look' ) . '</div>' ); ?>
    	<?php next_image_link( 'thumbnail', '<div class="next-image">' . __( 'Next Image', 'ceophoetography-look' ) . '</div>' ); ?>
    	</div><!-- .nav-links -->
    <?php endif; ?>
    </nav><!-- #image-navigation -->

    This code should work with or without MLA active. When MLA is active the [mla_gallery] shortcodes provide navigation links. The parameters are:

    • post_parent="{+page_parent+}" – selects all items sharing a parent post/page (or unattached) with the current item. The [+page_parent+] value has been added to the 20160706 Development Version.
    • id="{+page_ID+}" – names the current item for pagination calculations.
    • mla_output="next_link" – (or “previous_link”) selects the appropriate pagination control. You can add the optional “,wrap” qualifier to create a circular sequence, e.g., mla_output="next_link,wrap".
    • mla_link_text – provides the text for the control. I used an intermediate variable to make things a bit more readable.
    • mla_link_href="{+pagelink_url+}" – links to the appropriate page for the next/previous item.
    • update_post_meta_cache=false – prevents database queries to fill the meta data cache, which isn’t needed.
    • update_post_term_cache=false – prevents database queries to fill the taxonomy terms cache, which isn’t needed.

    I hope you will be able to update your template file with the above code and see an improvement. I will leave the topic unresolved until I hear back from you.

    Thread Starter ceophoetography

    (@ceophoetography)

    Thank you for all of the work you’ve put in. I installed the latest development version on my localhost test site and tried an image with the code you supplied.

    It almost works. The only problem is that instead of the link text, the output is the thumbnail of the image. The mla_output parameter appears to be ignored.

    Plugin Author David Lingren

    (@dglingren)

    Thanks for the good news regarding your progress. I made a small mistake in the code I posted. The double quotes in the class attributes of the div tags, e.g., class="previous-image" are messing things up and the mla_link_text is being ignored.

    Try changing the two assignment statements to these:

    <?php $previous_image_text = "<div class='previous-image'>" . __( 'Previous Image', 'ceophoetography-look' ) . '</div>'; ?>
    
    <?php $next_image_text = "<div class='next-image'>" . __( 'Next Image', 'ceophoetography-look' ) . '</div>'; ?>

    That should insert single quotes in the shortcode and clear up the problem.

    Thread Starter ceophoetography

    (@ceophoetography)

    It works! Yay! Thanks so much.

    No navigation appears beneath unattached images (viewed from the media library), but the main thing is that the page is whole.

    Thread Starter ceophoetography

    (@ceophoetography)

    Edit to the previous post:

    My mistake: It works completely. I had forgotten to update the MLA version on the server when I tested the live version, and that’s why the links didn’t show.

    Many thanks, David!

    Plugin Author David Lingren

    (@dglingren)

    Thanks for the good news – you are most welcome. Thanks for working with me on a topic that inspired an MLA enhancement.

    The memory leak is back, since I installed 2.40.
    Backing down to 2.33 makes the leak go away.
    -jsq

    Typical error with 2.40:
    Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 32 bytes) in /home/wwalsnet/public_html/wp-includes/meta.php on line 830

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘MLA attachment page memory leak?’ is closed to new replies.